Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/entity.api.php \hook_ENTITY_TYPE_prepare_form()
  2. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_ENTITY_TYPE_prepare_form()

Acts on a particular type of entity object about to be in an entity form.

This can be typically used to pre-fill entity values or change the form state before the entity form is built. It is invoked just once when first building the entity form. Rebuilds will not trigger a new invocation.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity that is about to be shown on the form.

$operation: The current operation.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

See also

\Drupal\Core\Entity\EntityForm::prepareEntity()

hook_entity_prepare_form()

Related topics

2 functions implement hook_ENTITY_TYPE_prepare_form()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

book_node_prepare_form in core/modules/book/book.module
Implements hook_ENTITY_TYPE_prepare_form() for node entities.
content_moderation_entity_prepare_form in core/modules/content_moderation/content_moderation.module
Implements hook_entity_prepare_form().

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1856
Hooks and documentation related to entities.

Code

function hook_ENTITY_TYPE_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Form\FormStateInterface $form_state) {
  if ($operation == 'edit') {
    $entity->label->value = 'Altered label';
    $form_state
      ->set('label_altered', TRUE);
  }
}