2

Is it possible to access and change a form's display mode in hook_form_alter()? I'd like to use some context to say "use this display mode, not the other one."

My specific use case is selecting a form display mode for a paragraph type nested (in multiple layers) inside one of several node forms. Based on details of the parent layers, I want to control which form display mode is used.

I've looked at hook_field_widget_entity_reference_paragraphs_form_alter() and hook_entity_form_display_alter() as well. While I could, it seems in principle, change the display in that last one, there's no information I could use to make the decision. The widget function doesn't seem to provide anything that looks potentially useful.

I would expect some sort of connection from either the $form or $form_state in hook_form_alter() to the display being used, but can't find one. Either I haven't dug down the right rabbit hole, or that connection doesn't exist there, and I need to look at some other processing step, but don't know which.

1 Answer 1

0

There ought to be a method to get this analogous to $form_state->getFormObject()->getOperation() but I am not aware of one. You can fallback to getting the form display mode from the query string in the URL:

$form_display = \Drupal::request()->get('display');

This would not allow changing the form mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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