diff --git a/core/modules/node/node.api.php b/core/modules/node/node.api.php index f1a1a37..45a02bf 100644 --- a/core/modules/node/node.api.php +++ b/core/modules/node/node.api.php @@ -350,24 +350,6 @@ function hook_node_access(\Drupal\node\NodeInterface $node, $op, $account, $lang } /** - * Act on a node object about to be shown on the add/edit form. - * - * This hook is invoked from NodeForm::prepareEntity(). - * - * @param \Drupal\node\NodeInterface $node - * The node that is about to be shown on the form. - * @param $operation - * The current operation. - * @param array $form_state - * An associative array containing the current state of the form. - */ -function hook_node_prepare_form(\Drupal\node\NodeInterface $node, $operation, array &$form_state) { - if (!isset($node->my_rating)) { - $node->my_rating = \Drupal::config("my_rating_{$node->bundle()}")->get('enabled'); - } -} - -/** * Act on a node being displayed as a search result. * * This hook is invoked from the node search plugin during search execution, @@ -440,6 +422,8 @@ function hook_node_update_index(\Drupal\node\NodeInterface $node, $langcode) { * The form being used to edit the node. * @param $form_state * The form state array. + * + * @ingroup entity_crud */ function hook_node_validate(\Drupal\node\NodeInterface $node, $form, &$form_state) { if (isset($node->end) && isset($node->start)) { @@ -465,6 +449,8 @@ function hook_node_validate(\Drupal\node\NodeInterface $node, $form, &$form_stat * The form being used to edit the node. * @param $form_state * The form state array. + * + * @ingroup entity_crud */ function hook_node_submit(\Drupal\node\NodeInterface $node, $form, &$form_state) { // Decompose the selected menu parent option into 'menu_name' and 'plid', if diff --git a/core/modules/system/core.api.php b/core/modules/system/core.api.php index 4eb5fe0..16f1434 100644 --- a/core/modules/system/core.api.php +++ b/core/modules/system/core.api.php @@ -287,7 +287,8 @@ * entities), to provide an administrative overview for your entities. * - add and edit forms, or default form: Define a class (or two) that * extend(s) \Drupal\Core\Entity\EntityForm to provide add and edit forms - * for your entities. + * for your entities. For content entities, base class + * \Drupal\Core\Entity\ContentEntityForm is a better starting point. * - delete form: Define a class that extends * \Drupal\Core\Entity\EntityConfirmFormBase to provide a delete * confirmation form for your entities. @@ -426,7 +427,7 @@ * @code * // If the entity supports translation, load the translation. * $entity = $entity->getTranslation($language->id); - * $build = $renderer->view($entity, 'view_mode_name', $language->id); + * $build = $viewer->view($entity, 'view_mode_name', $language->id); * // $build is a render array. * $rendered = drupal_render($build); * @endcode diff --git a/core/modules/system/entity.api.php b/core/modules/system/entity.api.php index b7a1913..c3e6669 100644 --- a/core/modules/system/entity.api.php +++ b/core/modules/system/entity.api.php @@ -11,9 +11,9 @@ use Drupal\Core\Render\Element; /** - * @defgroup entity_crud Entity CRUD and view hooks + * @defgroup entity_crud Entity CRUD, editing, and view hooks * @{ - * Hooks used in entity view, create, read, update, and delete operations. + * Hooks used in various entity operations. * * Entity create, read, update, and delete (CRUD) operations are performed by * entity storage controllers; see the @@ -31,6 +31,11 @@ * information. Most view builders extend or use the default class * \Drupal\Core\Entity\EntityViewBuilder. * + * Entity editing (including adding new entities) is handled by entity form + * controllers; see the @link entity_api Entity API topic @endlink for more + * information. Most entity editing forms extend base classes + * \Drupal\Core\Entity\EntityForm or \Drupal\Core\Entity\ContentEntityForm. + * * This topic lists all of the entity CRUD and view operations, and the hooks * and other operations that are invoked (in order) for each operation. Some * notes: @@ -116,6 +121,17 @@ * - Comment: hook_comment_publish() and hook_comment_unpublish() as * appropriate. * + * @section edit Editing operations + * When an entity's add/edit form is used to add or edit an entity, there + * are several hooks that are invoked: + * - hook_entity_prepare_form() + * - hook_ENTITY_TYPE_prepare_form() + * - hook_entity_form_display_alter() + * + * Some specific entity types have additional hooks that are run during + * various steps in the process: + * - Node entities: hook_node_validate() and hook_submit(). + * * @section delete Delete operations * To delete one or more entities, load them and then delete them: * @code @@ -155,7 +171,7 @@ * @code * // If the entity supports translation, load the translation. * $entity = $entity->getTranslation($language->id); - * $build = $renderer->view($entity, 'view_mode_name', $language->id); + * $build = $viewer->view($entity, 'view_mode_name', $language->id); * @endcode * You can also use the viewMultiple() method to view multiple entities. * @@ -172,6 +188,7 @@ * * During the rendering operation, the default entity viewer runs the following * hooks and operations in the pre-render step: + * - hook_entity_view_display_alter() * - hook_entity_prepare_view() * - Entity fields are loaded, and render arrays are built for them using * their formatters. @@ -1136,6 +1153,8 @@ function hook_entity_build_defaults_alter(array &$build, \Drupal\Core\Entity\Ent * - entity_type: The entity type, e.g., 'node' or 'user'. * - bundle: The bundle, e.g., 'page' or 'article'. * - view_mode: The view mode, e.g. 'full', 'teaser'... + * + * @ingroup entity_crud */ function hook_entity_view_display_alter(\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, array $context) { // Leave field labels out of the search index. @@ -1195,6 +1214,9 @@ function hook_entity_display_build_alter(&$build, $context) { * An associative array containing the current state of the form. * * @see \Drupal\Core\Entity\EntityForm::prepareEntity() + * @see hook_ENTITY_TYPE_prepare_form() + * + * @ingroup entity_crud */ function hook_entity_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, array &$form_state) { if ($operation == 'edit') { @@ -1204,6 +1226,32 @@ function hook_entity_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $ } /** + * 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. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity that is about to be shown on the form. + * @param $operation + * The current operation. + * @param array $form_state + * An associative array containing the current state of the form. + * + * @see \Drupal\Core\Entity\EntityForm::prepareEntity() + * @see hook_entity_prepare_form() + * + * @ingroup entity_crud + */ +function hook_ENTITY_TYPE_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, array &$form_state) { + if ($operation == 'edit') { + $entity->label->value = 'Altered label'; + $form_state['mymodule']['label_altered'] = TRUE; + } +} + +/** * Alter the settings used for displaying an entity form. * * @param \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display @@ -1214,6 +1262,8 @@ function hook_entity_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $ * - entity_type: The entity type, e.g., 'node' or 'user'. * - bundle: The bundle, e.g., 'page' or 'article'. * - form_mode: The form mode, e.g. 'default', 'profile', 'register'... + * + * @ingroup entity_crud */ function hook_entity_form_display_alter(\Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display, array $context) { // Hide the 'user_picture' field from the register form.