- Issue created by @jumoke
- 🇺🇸United States jcandan
Updated the description to take into account how the https://www.drupal.org/project/address and https://www.drupal.org/project/name modules let site-builders manage which sub-fields are required.
- 🇺🇸United States jcandan
Updated title to clarify that this isn't about cardinality, but fields types that include multiple fields--compound fields.
- 🇺🇸United States jcandan
Original report
The original report proposed that all subfields should be marked optional when reproducing this issue. It then suggested that the field could be subsequently submitted as Published without the desired required fields. This makes sense because there was no field marked as required. ROP validation kicks in if the field is left completely empty.
For the Address field, this means that to not have selected a Country, it would not pass Require on Publish validation. And it correctly does not.
For the Name field, it does not even allow you to set all subfields as optional, at least Given or Family must be required:
If I were to fill in the Given and Family fields, and Select a Country, this is able to be saved as Published.
- 🇺🇸United States jcandan
Gave this a shot.
/** * Implements hook_field_widget_complete_form_alter(). */ function require_on_publish_field_widget_complete_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) { $field_config = $context['items']->getFieldDefinition(); $field_name = $field_widget_complete_form['widget']['#field_name']; /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */ $form_object = $form_state->getFormObject(); if ($form_object instanceof EntityFormInterface) { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $form_object->getEntity(); if (_require_on_publish_entity_is_publishable(get_class($entity))) { // $field = $entity->getFields()[$field_name]; // $field_config = $field->getFieldDefinition(); if (($field_config instanceof FieldConfigInterface)) { if ($field_config->getThirdPartySetting('require_on_publish', 'require_on_publish', FALSE)) { $place_breakpoint_here = 'Analyze the available Widget'; // Loop the widget sub-fields and determine if they're required. Then, // treat each required subfield as if it were required on publish. } } } } }
Tried similar attempts from preprocess hooks, like
require_on_publish_preprocess_form_element()
. The problem is that I cannot get a combination of render array and the entity context necessary to alter the render array to:- Determine the subfield's parent is required on publish.
- Remove the required attribute.
- Somehow implement the require_on_publish logic.
I am open to suggestions and willing to accept DMs in #slack.
- 🇺🇸United States jcandan
Just had an idea, noting it here so I don't forget.
What if I tackle this from a new Constraint?
- 🇺🇸United States jcandan
I was able to Disable HTML5 validation → to get around the Address module's required attribute. So, similar to Name, I am getting the required subfield validation.
- 🇺🇸United States jcandan
Using the existing constraint, if I could just get it to run the
AddressFormatConstraintValidator
beforeRequireOnPublishValidator
, I think I would then be able to alter the$this->context->getViolations
.Tried increasing the module weight with
module_set_weight()
to no avail. - Status changed to Needs work
about 1 year ago 4:33pm 18 June 2024 - 🇺🇸United States jcandan
Needs work: Incorrectly replacing other field validation error messages with
@field_label also requires the following parts when publishing: <em>@components</em>
. - 🇺🇸United States jcandan
When a simple field is required on publish, and the compound (e.g. name) field error validation is ordered before the simple field's messages, the messages are all replaced by the
@field_label also requires the following parts when publishing: <em>@components</em>
. - 🇺🇸United States jcandan
Fixed issue where other field validation messages were being overridden.
- 🇺🇸United States jcandan
As per #13, updating the scope of this ticket to target the name field only. Address field already has a ticket in the queue: 📌 Add support for Address field Active .
- 🇺🇸United States jcandan
Pipelines fail. Needs fixes. Would like to shoot for a 2.1.0 release.
- 🇺🇸United States jcandan
@anirudhsingh19, thank you for contributing! We're close. We need test coverage. I got us part of the way there by adding Name as a part of the CI pipeline.
However, I recognized a problem while writing tests for this. We're currently blocked by 📌 Ensure require_on_publish setting is applied when FieldConfig is created programmatically Active .
- 🇺🇸United States jcandan
Never mind. #3539138-2: Ensure require_on_publish setting is applied when FieldConfig is created programmatically → explains why.
But, there is another issue. 📌 Fix EditPageTest not actually ever hitting assertions Active needs to be resolved so we can effectively test the Name Field support.
- 🇺🇸United States jcandan
Blocked by 📌 Add test to ensure require on publish after saving Active .