3

I have created a custom form display mode my_mode for nodes and wish to display node form using this mode instead of default one at a custom page. How can I do it?

$this->entityFormBuilder()->getForm() accepts an operation as a parameter, but not form mode.

2 Answers 2

4

Form modes and form operations are a bit strange. You also need to define a form operation through hook_entity_type_alter() to be able to use a custom form mode.

Which makes the UI to create form modes pretty useless, but ssshh, don't tell anyone :)

For an example, see views_ui_entity_type_build(): $entity_type->setFormClass($operation, $class_name)

3
  • Where did you read it? Is there any documentation with details how it should be done? Commented Nov 15, 2014 at 3:11
  • I didn't read it, I know it as I participated in the relevant issues. Updated the answer with references to an example.
    – Berdir
    Commented Nov 16, 2014 at 19:26
  • lobsterr.me/post/… provides a comprehensive explanation how it can be done. Commented Nov 5, 2020 at 14:34
3

Indeed it is quite strange I find to have both operation and form display, but you do not especially need to have a form operation to use your display. You have to use hook_entity_type_alter though...

Here is the way for example to load the "super" form mode (if exists and is activated) :

/**
 * Implements hook_entity_form_display_alter().
 */
function my_module_entity_form_display_alter(&$form_display, $context) {

  // Load the right entity form display.
  $id = $context['entity_type'] . '.' . $context['bundle'] . '.super';
  $storage = \Drupal::entityManager()->getStorage('entity_form_display');
  $change_display = $storage->load($id);

  // If form mode is activated, replace the given one with ours.
  if ($change_display) {
    $form_display = $change_display;
  }
}

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.