0

I have a node form using an inline entity form widget for either selecting or creating taxonomy terms to be referenced from the node. The field should be required at a certain moderation state, but in the early stages of moderation the field does not need to be required. I'm trying to make this change in hook_form_alter

I tried comparing the values of $form in the alter function when the field had been set to required, and when it hadn't. The only differences were within $form['field_name']['widget']:

$form['field_name']['widget']['#required'] => 0

vs.

$form['field_name']['widget']['#required'] => 1

and

$form['field_name']['widget'][#element_validate] => Array
                (
                    [0] => Array
                        (
                            [0] => Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex
                            [1] => updateRowWeights
                        )

                )

vs.

$form['field_name']['widget'][#element_validate] => Array
                (
                    [0] => Array
                        (
                            [0] => Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex
                            [1] => updateRowWeights
                        )

                    [1] => Array
                        (
                            [0] => Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex
                            [1] => requiredField
                        )

                )

Just adding $form["field_name"]["widget"]["#required"] = TRUE; to the alter didn't change anything, and trying to add the #element_validate bit lead to an error:

User error: "0" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).

How can I programmatically require an inline entity form field in a hook form alter?

1

I'm not sure if this will work out of the box for you, but I wrote this module https://www.drupal.org/project/require_on_publish to solve a similar problem. You might be able to borrow some of the ideas in there around adding constraints to custom fields to achieve your desired functionality.

0

I'm using the complex widget for Inline Entity Form. It may be that the normal form alter approach works for the simple form, I haven't tested, that but could not find the method I'm using to accomplish my solution in the simpleWidget code.

To get an admittedly ugly by default required indicator, and to actually have the validation happen, I needed to call the same validation method the complex widget uses when the field is marked as required within the structure -> content type -> manage fields interface.

Here is the code I used:

array_push($form['field_name']['#attributes']['class'],
          'form-required',
          'js-form-required');
$form['field_name']['widget']['#element_validate'][] = [
          '\Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex', 
          'requiredField'
        ];;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.