- 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
5 months ago 4:33pm 18 June 2024