function hook_entity_form_mode_alter

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_form_mode_alter()
  2. 10 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_form_mode_alter()

Change the form mode used to build an entity form.

Parameters

string $form_mode: The form_mode that is to be used to build the entity form.

\Drupal\Core\Entity\EntityInterface $entity: The entity for which the form is being built.

Related topics

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1906

Code

function hook_entity_form_mode_alter(&$form_mode, \Drupal\Core\Entity\EntityInterface $entity) : void {
    // Change the form mode for users with Administrator role.
    if ($entity->getEntityTypeId() == 'user' && $entity->hasRole('administrator')) {
        $form_mode = 'my_custom_form_mode';
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.