0

I'm attempting to make a page for users to edit data for a specific content type, but I only want them to have access to some of the fields not all. To do so I created a custom form display mode for content. I have a custom controller that is meant to display this form with a custom display mode. Here is the code meant to render it.

    public function showCustomForm() {
        $node = \Drupal::entityTypeManager()->getStorage('node')->load('123');
        $edit_form = \Drupal::entityTypeManager()->getFormObject('node', 'my_custom_form_display_mode_id')->setEntity($node);
        return \Drupal::formBuilder()->getForm($edit_form);
    }

And I get the error

Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException: The "node" entity type did not specify a "my_custom_form_display_mode_id" form class. in Drupal\Core\Entity\EntityTypeManager->getFormObject() (line 184 of /var/www/mydomain/http/web/core/lib/Drupal/Core/Entity/EntityTypeManager.php).

I was using this How do I load a form with a specific form display (form mode)? as an example. But still had no luck.

How would I programmatically load a node edit form with a custom form display mode?

4

The error message says you did not specify a form class, but what it really wants to say is, that you did not specify a form operation with the name of the form mode:

mymodule.module:

function mymodule_entity_type_build(array &$entity_types) {
  $entity_types['node']->setFormClass('my_custom_form_display_mode_id', 'Drupal\node\NodeForm');
}

The form operation needs a form class. This can be the same as for default, which you find in the handlers section of the annotations in the Node class.

  • I noticed that this was put in a separate hook. Is there no way to only do this in my specific controller? I don't want this to apply to all nodes, and I only want it to apply if you're on one specific page. The render array is being generated by a controller I made and the address was made in my routing file. – Matt Feb 8 '18 at 16:44
  • Nevermind I see now how your code needs to be added in order to make it available as an operation. Thanks! – Matt Feb 8 '18 at 16:50

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.