2

I have a custom entity type, property. In admin/structure/display-modes/form I created a new form mode for this entity type, machine name is property.property_status. It only shows one field on the form.

I am now trying to show that form mode on a custom page. Right now I'm using: \Drupal::service('entity.form_builder')->getForm($property, 'property.property_status') but I get the error: Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException: The "property" entity type did not specify a "property.property_status" form class.

Any ideas? Thanks!

2 Answers 2

4

You need to specify a form operation in the handler section of the custom entity:

/**
 * @ContentEntityType(
 *   id = "property",
 *   handlers = {
 *     "form" = {
 *       "default" = "Drupal\mymodule\Form\MyEntityForm",
 *       "status" = "Drupal\mymodule\Form\MyEntityForm",

You can define multiple operations for the same form class. In this example I used status for the form operation, because this name usually doesn't contain the entity type name.

Then you should be able to use the form mode status in the entity form builder:

\Drupal::service('entity.form_builder')->getForm($property, 'status')
6
  • Note that if the name of the form handler is "status" then the name of the form mode should be "status" as well.
    – Eyal
    Commented Feb 14, 2017 at 0:17
  • I tried this based on some feedback in the Slack channel before posting the question (was also directed to drupal.org/node/2530086). In my case, it still showed the whole form. I'll go back to it and try again. Commented Feb 17, 2017 at 4:26
  • Not sure what I did last time, but I created another form mode with a simple name ("blah") and it worked fine. Thanks! Commented Feb 17, 2017 at 4:35
  • That surely can't be right. Form modes are created in the admin UI. Having to then set up properties in the entity type info just to use it is crazy.
    – joachim
    Commented Sep 7, 2017 at 13:34
  • @joachim, this is for a custom entity type, which is defined in code and this is about what you need to add to this code. For UI you are right, in contrast to view modes you can't manage form modes in core. For now this is in contrib drupal.org/project/form_mode_manager
    – 4uk4
    Commented Sep 7, 2017 at 13:54
3

You could override the form display in a simple way. Or for non-admin users.

But this is not the best solution! since this will override the form display everywhere. You may want to add more if conditions to be selective.

/**
 * Implements hook_entity_form_display_alter().
 */
function HOOK_entity_form_display_alter(&$form_display, $context) {
  // You may want to add some conditions to ignore this override.
  // For example let's ignore this override if the current user has a specific permission.
  // if (\Drupal::currentUser()->hasPermission('access content')) {
  //   return;
  // }
  // Check the specific content type that we are targeting.
  if (isset($context['entity_type']) && isset($context['bundle'])
    && ($context['entity_type'] == 'node') && ($context['bundle'] == 'CONTENT_TYPE')) {
    $storage = \Drupal::service('entity_type.manager')->getStorage('entity_form_display');
    // $form_display = $storage->load('user.user.VIEW_MODE_MACHINE_NAME');
    // $form_display = $storage->load('node.CONTENT_TYPE.VIEW_MODE_MACHINE_NAME');
    $form_display = $storage->load('node.article.my_custom_view_mode_name');
  }
}

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.