How can I require and Inline Entity Form widget field programmatically?

Created on 29 March 2021, almost 4 years ago
Updated 14 February 2024, about 1 year ago

Problem/Motivation

I'm trying to set the field to required, once the node reaches a certain moderation state.

I've asked a question on Drupal Answers, but so far haven't been able to get an answer.

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?

πŸ’¬ Support request
Status

Closed: works as designed

Component

Miscellaneous

Created by

πŸ‡―πŸ‡΅Japan ultrabob Japan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States dcam
  • πŸ‡ΊπŸ‡ΈUnited States peter pulsifer

    Perhaps a more direct answer is at
    https://drupal.stackexchange.com/questions/229422/how-to-make-a-form-fie...

    It seems to depend on the type of field, and the only way I could track it down was to make the field required in the UI and then dump the $form_state array in my hook_form_alter function. For a taxonomy term entity reference field, all I needed was to set
    $form['fieldname']['widget']['#required'] = true;

    But for a text field, I needed
    $form['fieldname']['widget'][0]['value']['#required'] = true;

    Just to be careful I set
    $form[fieldname']['widget']['#required'] = $form[fieldname']['widget'][0]['#required'] = $form[fieldname']['widget'][0]['value']['#required'] = true;

    That's the way it comes in when I make the field required in the content type.

    Just keep shooting in the dark until you hit something!

Production build 0.71.5 2024