Drupal 8  8.0.2
Entity CRUD, editing, and view hooks

Functions

 hook_entity_bundle_delete ($entity_type_id, $bundle)
 
 hook_entity_create (\Drupal\Core\Entity\EntityInterface $entity)
 
 hook_ENTITY_TYPE_create (\Drupal\Core\Entity\EntityInterface $entity)
 
 hook_entity_load (array $entities, $entity_type_id)
 
 hook_ENTITY_TYPE_load ($entities)
 
 hook_entity_presave (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_ENTITY_TYPE_presave (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_entity_insert (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_ENTITY_TYPE_insert (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_entity_update (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_ENTITY_TYPE_update (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_entity_translation_create (\Drupal\Core\Entity\EntityInterface $translation)
 
 hook_ENTITY_TYPE_translation_create (\Drupal\Core\Entity\EntityInterface $translation)
 
 hook_entity_translation_insert (\Drupal\Core\Entity\EntityInterface $translation)
 
 hook_ENTITY_TYPE_translation_insert (\Drupal\Core\Entity\EntityInterface $translation)
 
 hook_entity_translation_delete (\Drupal\Core\Entity\EntityInterface $translation)
 
 hook_ENTITY_TYPE_translation_delete (\Drupal\Core\Entity\EntityInterface $translation)
 
 hook_entity_predelete (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_ENTITY_TYPE_predelete (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_entity_delete (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_ENTITY_TYPE_delete (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_entity_revision_delete (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_ENTITY_TYPE_revision_delete (Drupal\Core\Entity\EntityInterface $entity)
 
 hook_entity_view (array &$build,\Drupal\Core\Entity\EntityInterface $entity,\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode)
 
 hook_ENTITY_TYPE_view (array &$build,\Drupal\Core\Entity\EntityInterface $entity,\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode)
 
 hook_entity_view_alter (array &$build, Drupal\Core\Entity\EntityInterface $entity,\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display)
 
 hook_ENTITY_TYPE_view_alter (array &$build, Drupal\Core\Entity\EntityInterface $entity,\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display)
 
 hook_entity_prepare_view ($entity_type_id, array $entities, array $displays, $view_mode)
 
 hook_entity_view_mode_alter (&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context)
 
 hook_ENTITY_TYPE_build_defaults_alter (array &$build,\Drupal\Core\Entity\EntityInterface $entity, $view_mode)
 
 hook_entity_build_defaults_alter (array &$build,\Drupal\Core\Entity\EntityInterface $entity, $view_mode)
 
 hook_entity_view_display_alter (\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, array $context)
 
 hook_entity_display_build_alter (&$build, $context)
 
 hook_entity_prepare_form (\Drupal\Core\Entity\EntityInterface $entity, $operation,\Drupal\Core\Form\FormStateInterface $form_state)
 
 hook_ENTITY_TYPE_prepare_form (\Drupal\Core\Entity\EntityInterface $entity, $operation,\Drupal\Core\Form\FormStateInterface $form_state)
 
 hook_entity_form_display_alter (\Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display, array $context)
 
 hook_entity_field_values_init (\Drupal\Core\Entity\FieldableEntityInterface $entity)
 
 hook_ENTITY_TYPE_field_values_init (\Drupal\Core\Entity\FieldableEntityInterface $entity)
 
 hook_node_search_result (\Drupal\node\NodeInterface $node)
 
 hook_node_update_index (\Drupal\node\NodeInterface $node)
 
 hook_ranking ()
 

Detailed Description

Hooks used in various entity operations.

Entity create, read, update, and delete (CRUD) operations are performed by entity storage classes; see the Entity API topic for more information. Most entities use or extend the default classes: for content entities, and for configuration entities. For these entities, there is a set of hooks that is invoked for each CRUD operation, which module developers can implement to affect these operations; these hooks are actually invoked from methods on .

For content entities, viewing and rendering are handled by a view builder class; see the Entity API topic for more information. Most view builders extend or use the default class .

Entity editing (including adding new entities) is handled by entity form classes; see the Entity API topic for more information. Most entity editing forms extend base classes or . Note that many other operations, such as confirming deletion of entities, also use entity form classes.

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:

Create operations

To create an entity:

$entity = $storage->create();
// Add code here to set properties on the entity.
// Until you call save(), the entity is just in memory.
$entity->save();

There is also a shortcut method on entity classes, which creates an entity with an array of provided property values: ::create().

Hooks invoked during the create operation:

See Save operations below for the save portion of the operation.

Read/Load operations

To load (read) a single entity:

$entity = $storage->load($id);

To load multiple entities:

$entities = $storage->loadMultiple($ids);

Since load() calls loadMultiple(), these are really the same operation. Here is the order of hooks and other operations that take place during entity loading:

When an entity is loaded, normally the default entity revision is loaded. It is also possible to load a different revision, for entities that support revisions, with this code:

$entity = $storage->loadRevision($revision_id);

This involves the same hooks and operations as regular entity loading.

Save operations

To update an existing entity, you will need to load it, change properties, and then save; as described above, when creating a new entity, you will also need to save it. Here is the order of hooks and other events that happen during an entity save:

Some specific entity types invoke hooks during preSave() or postSave() operations. Examples:

Editing operations

When an entity's add/edit form is used to add or edit an entity, there are several hooks that are invoked:

Delete operations

To delete one or more entities, load them and then delete them:

$entities = $storage->loadMultiple($ids);
$storage->delete($entities);

During the delete operation, the following hooks and other events happen:

Some specific entity types invoke hooks during the delete process. Examples:

Individual revisions of an entity can also be deleted:

$storage->deleteRevision($revision_id);

This operation invokes the following operations and hooks:

View/render operations

To make a render array for a loaded entity:

// You can omit the language ID if the default language is being used.
$build = $view_builder->view($entity, 'view_mode_name', $language->getId());

You can also use the viewMultiple() method to view multiple entities.

Hooks invoked during the operation of building a render array:

View builders for some types override these hooks, notably:

During the rendering operation, the default entity viewer runs the following hooks and operations in the pre-render step:

Some specific builders have specific hooks:

After this point in rendering, the theme system takes over. See the Theme system and render API topic for more information.

Other entity hooks

Some types of entities invoke hooks for specific operations:

Function Documentation

hook_entity_build_defaults_alter ( array &  $build,
\Drupal\Core\Entity\EntityInterface  $entity,
  $view_mode 
)

Alter entity renderable values before cache checking in drupal_render().

The values in the #cache key of the renderable array are used to determine if a cache entry exists for the entity's rendered output. Ideally only values that pertain to caching should be altered in this hook.

Parameters
array&$buildA renderable array containing the entity's caching and view mode values.
\Drupal\Core\Entity\EntityInterface$entityThe entity that is being viewed.
string$view_modeThe view_mode that is to be used to display the entity.
See Also
drupal_render()
hook_ENTITY_TYPE_build_defaults_alter()
hook_entity_bundle_delete (   $entity_type_id,
  $bundle 
)

Act on entity_bundle_delete().

This hook is invoked after the operation has been performed.

Parameters
string$entity_type_idThe type of entity; for example, 'node' or 'user'.
string$bundleThe bundle that was just deleted.

References Drupal\config().

Here is the call graph for this function:

hook_entity_create ( \Drupal\Core\Entity\EntityInterface  $entity)

Acts when creating a new entity.

This hook runs after a new entity object has just been instantiated.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object.
See Also
hook_ENTITY_TYPE_create()

References Drupal\logger().

Here is the call graph for this function:

hook_entity_delete ( Drupal\Core\Entity\EntityInterface  $entity)

Respond to entity deletion.

This hook runs once the entity has been deleted from the storage.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object for the entity that has been deleted.
See Also
hook_ENTITY_TYPE_delete()

References db_delete().

Here is the call graph for this function:

hook_entity_display_build_alter ( $build,
  $context 
)

Alter the render array generated by an EntityDisplay for an entity.

Parameters
array$buildThe renderable array generated by the EntityDisplay.
array$contextAn associative array containing:
  • entity: The entity being rendered.
  • view_mode: The view mode; for example, 'full' or 'teaser'.
  • display: The EntityDisplay holding the display options.
hook_entity_field_values_init ( \Drupal\Core\Entity\FieldableEntityInterface  $entity)

Acts when initializing a fieldable entity object.

This hook runs after a new entity object or a new entity translation object has just been instantiated. It can be used to set initial values, e.g. to provide defaults.

Parameters
\Drupal\Core\Entity\FieldableEntityInterface$entityThe entity object.
See Also
hook_ENTITY_TYPE_field_values_init()
hook_entity_form_display_alter ( \Drupal\Core\Entity\Display\EntityFormDisplayInterface  $form_display,
array  $context 
)

Alter the settings used for displaying an entity form.

Parameters
\Drupal\Core\Entity\Display\EntityFormDisplayInterface$form_displayThe entity_form_display object that will be used to display the entity form components.
array$contextAn associative array containing:
  • 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', etc.
hook_entity_insert ( Drupal\Core\Entity\EntityInterface  $entity)

Respond to creation of a new entity.

This hook runs once the entity has been stored. Note that hook implementations may not alter the stored entity data.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object.
See Also
hook_ENTITY_TYPE_insert()

References db_insert().

Here is the call graph for this function:

hook_entity_load ( array  $entities,
  $entity_type_id 
)

Act on entities when loaded.

This is a generic load hook called for all entity types loaded via the entity API.

hook_entity_storage_load() should be used to load additional data for content entities.

Parameters
\Drupal\Core\Entity\EntityInterface[]$entities The entities keyed by entity ID.
string$entity_type_idThe type of entities being loaded (i.e. node, user, comment).
See Also
hook_ENTITY_TYPE_load()
hook_entity_predelete ( Drupal\Core\Entity\EntityInterface  $entity)

Act before entity deletion.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object for the entity that is about to be deleted.
See Also
hook_ENTITY_TYPE_predelete()

References db_merge(), and db_select().

Here is the call graph for this function:

hook_entity_prepare_form ( \Drupal\Core\Entity\EntityInterface  $entity,
  $operation,
\Drupal\Core\Form\FormStateInterface  $form_state 
)

Acts on an entity object about to be shown on 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$entityThe entity that is about to be shown on the form.
$operationThe current operation.
\Drupal\Core\Form\FormStateInterface$form_stateThe current state of the form.
See Also
::prepareEntity()
hook_ENTITY_TYPE_prepare_form()
hook_entity_prepare_view (   $entity_type_id,
array  $entities,
array  $displays,
  $view_mode 
)

Act on entities as they are being prepared for view.

Allows you to operate on multiple entities as they are being prepared for view. Only use this if attaching the data during the entity loading phase is not appropriate, for example when attaching other 'entity' style objects.

Parameters
string$entity_type_idThe type of entities being viewed (i.e. node, user, comment).
array$entitiesThe entities keyed by entity ID.
\Drupal\Core\Entity\Display\EntityViewDisplayInterface[]$displays The array of entity view displays holding the display options configured for the entity components, keyed by bundle name.
string$view_modeThe view mode.
hook_entity_presave ( Drupal\Core\Entity\EntityInterface  $entity)

Act on an entity before it is created or updated.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object.
See Also
hook_ENTITY_TYPE_presave()

References TranslatableInterface\isTranslatable(), Drupal\routeMatch(), and Drupal\service().

Here is the call graph for this function:

hook_entity_revision_delete ( Drupal\Core\Entity\EntityInterface  $entity)

Respond to entity revision deletion.

This hook runs once the entity revision has been deleted from the storage.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object for the entity revision that has been deleted.
See Also
hook_ENTITY_TYPE_revision_delete()
hook_entity_translation_create ( \Drupal\Core\Entity\EntityInterface  $translation)

Acts when creating a new entity translation.

This hook runs after a new entity translation object has just been instantiated.

Parameters
\Drupal\Core\Entity\EntityInterface$translationThe entity object.
See Also
hook_ENTITY_TYPE_translation_create()

References Drupal\logger().

Here is the call graph for this function:

hook_entity_translation_delete ( \Drupal\Core\Entity\EntityInterface  $translation)

Respond to entity translation deletion.

This hook runs once the entity translation has been deleted from storage.

Parameters
\Drupal\Core\Entity\EntityInterface$translationThe original entity object.
See Also
hook_ENTITY_TYPE_translation_delete()

References Drupal\logger().

Here is the call graph for this function:

hook_entity_translation_insert ( \Drupal\Core\Entity\EntityInterface  $translation)

Respond to creation of a new entity translation.

This hook runs once the entity translation has been stored. Note that hook implementations may not alter the stored entity translation data.

Parameters
\Drupal\Core\Entity\EntityInterface$translationThe entity object of the translation just stored.
See Also
hook_ENTITY_TYPE_translation_insert()

References Drupal\logger().

Here is the call graph for this function:

hook_ENTITY_TYPE_build_defaults_alter ( array &  $build,
\Drupal\Core\Entity\EntityInterface  $entity,
  $view_mode 
)

Alter entity renderable values before cache checking in drupal_render().

Invoked for a specific entity type.

The values in the #cache key of the renderable array are used to determine if a cache entry exists for the entity's rendered output. Ideally only values that pertain to caching should be altered in this hook.

Parameters
array&$buildA renderable array containing the entity's caching and view mode values.
\Drupal\Core\Entity\EntityInterface$entityThe entity that is being viewed.
string$view_modeThe view_mode that is to be used to display the entity.
See Also
drupal_render()
hook_entity_build_defaults_alter()
hook_ENTITY_TYPE_create ( \Drupal\Core\Entity\EntityInterface  $entity)

Acts when creating a new entity of a specific type.

This hook runs after a new entity object has just been instantiated.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object.
See Also
hook_entity_create()

References Drupal\logger().

Here is the call graph for this function:

hook_ENTITY_TYPE_delete ( Drupal\Core\Entity\EntityInterface  $entity)

Respond to entity deletion of a particular type.

This hook runs once the entity has been deleted from the storage.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object for the entity that has been deleted.
See Also
hook_entity_delete()

References db_delete().

Here is the call graph for this function:

hook_ENTITY_TYPE_field_values_init ( \Drupal\Core\Entity\FieldableEntityInterface  $entity)

Acts when initializing a fieldable entity object.

This hook runs after a new entity object or a new entity translation object has just been instantiated. It can be used to set initial values, e.g. to provide defaults.

Parameters
\Drupal\Core\Entity\FieldableEntityInterface$entityThe entity object.
See Also
hook_entity_field_values_init()
hook_ENTITY_TYPE_insert ( Drupal\Core\Entity\EntityInterface  $entity)

Respond to creation of a new entity of a particular type.

This hook runs once the entity has been stored. Note that hook implementations may not alter the stored entity data.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object.
See Also
hook_entity_insert()

References db_insert().

Here is the call graph for this function:

hook_ENTITY_TYPE_load (   $entities)

Act on entities of a specific type when loaded.

Parameters
array$entitiesThe entities keyed by entity ID.
See Also
hook_entity_load()
hook_ENTITY_TYPE_predelete ( Drupal\Core\Entity\EntityInterface  $entity)

Act before entity deletion of a particular entity type.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object for the entity that is about to be deleted.
See Also
hook_entity_predelete()

References db_merge(), and db_select().

Here is the call graph for this function:

hook_ENTITY_TYPE_prepare_form ( \Drupal\Core\Entity\EntityInterface  $entity,
  $operation,
\Drupal\Core\Form\FormStateInterface  $form_state 
)

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$entityThe entity that is about to be shown on the form.
$operationThe current operation.
\Drupal\Core\Form\FormStateInterface$form_stateThe current state of the form.
See Also
::prepareEntity()
hook_entity_prepare_form()
hook_ENTITY_TYPE_presave ( Drupal\Core\Entity\EntityInterface  $entity)

Act on a specific type of entity before it is created or updated.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object.
See Also
hook_entity_presave()

References Drupal\routeMatch(), and Drupal\service().

Here is the call graph for this function:

hook_ENTITY_TYPE_revision_delete ( Drupal\Core\Entity\EntityInterface  $entity)

Respond to entity revision deletion of a particular type.

This hook runs once the entity revision has been deleted from the storage.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object for the entity revision that has been deleted.
See Also
hook_entity_revision_delete()
hook_ENTITY_TYPE_translation_create ( \Drupal\Core\Entity\EntityInterface  $translation)

Acts when creating a new entity translation of a specific type.

This hook runs after a new entity translation object has just been instantiated.

Parameters
\Drupal\Core\Entity\EntityInterface$translationThe entity object.
See Also
hook_entity_translation_create()

References Drupal\logger().

Here is the call graph for this function:

hook_ENTITY_TYPE_translation_delete ( \Drupal\Core\Entity\EntityInterface  $translation)

Respond to entity translation deletion of a particular type.

This hook runs once the entity translation has been deleted from storage.

Parameters
\Drupal\Core\Entity\EntityInterface$translationThe original entity object.
See Also
hook_entity_translation_delete()

References Drupal\logger().

Here is the call graph for this function:

hook_ENTITY_TYPE_translation_insert ( \Drupal\Core\Entity\EntityInterface  $translation)

Respond to creation of a new entity translation of a particular type.

This hook runs once the entity translation has been stored. Note that hook implementations may not alter the stored entity translation data.

Parameters
\Drupal\Core\Entity\EntityInterface$translationThe entity object of the translation just stored.
See Also
hook_entity_translation_insert()

References Drupal\logger().

Here is the call graph for this function:

hook_ENTITY_TYPE_update ( Drupal\Core\Entity\EntityInterface  $entity)

Respond to updates to an entity of a particular type.

This hook runs once the entity storage has been updated. Note that hook implementations may not alter the stored entity data.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object.
See Also
hook_entity_update()

References db_update().

Here is the call graph for this function:

hook_ENTITY_TYPE_view ( array &  $build,
\Drupal\Core\Entity\EntityInterface  $entity,
\Drupal\Core\Entity\Display\EntityViewDisplayInterface  $display,
  $view_mode 
)

Act on entities of a particular type being assembled before rendering.

Parameters
&$buildA renderable array representing the entity content.
\Drupal\Core\Entity\EntityInterface$entityThe entity object.
\Drupal\Core\Entity\Display\EntityViewDisplayInterface$displayThe entity view display holding the display options configured for the entity components.
$view_modeThe view mode the entity is rendered in.

The module may add elements to $build prior to rendering. The structure of $build is a renderable array as expected by drupal_render().

See Also
hook_ENTITY_TYPE_view_alter()
hook_entity_view()
hook_ENTITY_TYPE_view_alter ( array &  $build,
Drupal\Core\Entity\EntityInterface  $entity,
\Drupal\Core\Entity\Display\EntityViewDisplayInterface  $display 
)

Alter the results of the entity build array for a particular entity type.

This hook is called after the content has been assembled in a structured array and may be used for doing processing which requires that the complete entity content structure has been built.

If a module wishes to act on the rendered HTML of the entity rather than the structured content array, it may use this hook to add a #post_render callback. Alternatively, it could also implement hook_preprocess_HOOK() for the particular entity type template, if there is one (e.g., node.html.twig).

See the Default theme implementations topic and drupal_render() for details.

Parameters
array&$buildA renderable array representing the entity content.
\Drupal\Core\Entity\EntityInterface$entityThe entity object being rendered.
\Drupal\Core\Entity\Display\EntityViewDisplayInterface$displayThe entity view display holding the display options configured for the entity components.
See Also
hook_ENTITY_TYPE_view()
hook_entity_view_alter()
hook_entity_update ( Drupal\Core\Entity\EntityInterface  $entity)

Respond to updates to an entity.

This hook runs once the entity storage has been updated. Note that hook implementations may not alter the stored entity data.

Parameters
\Drupal\Core\Entity\EntityInterface$entityThe entity object.
See Also
hook_ENTITY_TYPE_update()

References db_update().

Here is the call graph for this function:

hook_entity_view ( array &  $build,
\Drupal\Core\Entity\EntityInterface  $entity,
\Drupal\Core\Entity\Display\EntityViewDisplayInterface  $display,
  $view_mode 
)

Act on entities being assembled before rendering.

Parameters
&$buildA renderable array representing the entity content.
\Drupal\Core\Entity\EntityInterface$entityThe entity object.
\Drupal\Core\Entity\Display\EntityViewDisplayInterface$displayThe entity view display holding the display options configured for the entity components.
$view_modeThe view mode the entity is rendered in.

The module may add elements to $build prior to rendering. The structure of $build is a renderable array as expected by drupal_render().

See Also
hook_entity_view_alter()
hook_ENTITY_TYPE_view()
hook_entity_view_alter ( array &  $build,
Drupal\Core\Entity\EntityInterface  $entity,
\Drupal\Core\Entity\Display\EntityViewDisplayInterface  $display 
)

Alter the results of the entity build array.

This hook is called after the content has been assembled in a structured array and may be used for doing processing which requires that the complete entity content structure has been built.

If a module wishes to act on the rendered HTML of the entity rather than the structured content array, it may use this hook to add a #post_render callback. Alternatively, it could also implement hook_preprocess_HOOK() for the particular entity type template, if there is one (e.g., node.html.twig).

See the Default theme implementations topic and drupal_render() for details.

Parameters
array&$buildA renderable array representing the entity content.
\Drupal\Core\Entity\EntityInterface$entityThe entity object being rendered.
\Drupal\Core\Entity\Display\EntityViewDisplayInterface$displayThe entity view display holding the display options configured for the entity components.
See Also
hook_entity_view()
hook_ENTITY_TYPE_view_alter()
hook_entity_view_display_alter ( \Drupal\Core\Entity\Display\EntityViewDisplayInterface  $display,
array  $context 
)

Alter the settings used for displaying an entity.

Parameters
\Drupal\Core\Entity\Display\EntityViewDisplayInterface$displayThe entity view display that will be used to display the entity components.
array$contextAn associative array containing:
  • 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', etc.
hook_entity_view_mode_alter ( $view_mode,
Drupal\Core\Entity\EntityInterface  $entity,
  $context 
)

Change the view mode of an entity that is being displayed.

Parameters
string$view_modeThe view_mode that is to be used to display the entity.
\Drupal\Core\Entity\EntityInterface$entityThe entity that is being viewed.
array$contextArray with additional context information, currently only contains the langcode the entity is viewed in.
hook_node_search_result ( \Drupal\node\NodeInterface  $node)

Act on a node being displayed as a search result.

This hook is invoked from the node search plugin during search execution, after loading and rendering the node.

Parameters
\Drupal\node\NodeInterface$nodeThe node being displayed in a search result.
Returns
array Extra information to be displayed with search result. This information should be presented as an associative array. It will be concatenated with the post information (last updated, author) in the default search result theming.
See Also
template_preprocess_search_result()
search-result.html.twig

References db_query(), and Drupal\translation().

Here is the call graph for this function:

hook_node_update_index ( \Drupal\node\NodeInterface  $node)

Act on a node being indexed for searching.

This hook is invoked during search indexing, after loading, and after the result of rendering is added as $node->rendered to the node object.

Parameters
\Drupal\node\NodeInterface$nodeThe node being indexed.
Returns
string Additional node information to be indexed.

References db_query().

Here is the call graph for this function:

hook_ranking ( )

Provide additional methods of scoring for core search results for nodes.

A node's search score is used to rank it among other nodes matched by the search, with the highest-ranked nodes appearing first in the search listing.

For example, a module allowing users to vote on content could expose an option to allow search results' rankings to be influenced by the average voting score of a node.

All scoring mechanisms are provided as options to site administrators, and may be tweaked based on individual sites or disabled altogether if they do not make sense. Individual scoring mechanisms, if enabled, are assigned a weight from 1 to 10. The weight represents the factor of magnification of the ranking mechanism, with higher-weighted ranking mechanisms having more influence. In order for the weight system to work, each scoring mechanism must return a value between 0 and 1 for every node. That value is then multiplied by the administrator-assigned weight for the ranking mechanism, and then the weighted scores from all ranking mechanisms are added, which brings about the same result as a weighted average.

Returns
array An associative array of ranking data. The keys should be strings, corresponding to the internal name of the ranking mechanism, such as 'recent', or 'comments'. The values should be arrays themselves, with the following keys available:
  • title: (required) The human readable name of the ranking mechanism.
  • join: (optional) An array with information to join any additional necessary table. This is not necessary if the table required is already joined to by the base query, such as for the {node} table. Other tables should use the full table name as an alias to avoid naming collisions.
  • score: (required) The part of a query string to calculate the score for the ranking mechanism based on values in the database. This does not need to be wrapped in parentheses, as it will be done automatically; it also does not need to take the weighted system into account, as it will be done automatically. It does, however, need to calculate a decimal between 0 and 1; be careful not to cast the entire score to an integer by inadvertently introducing a variable argument.
  • arguments: (optional) If any arguments are required for the score, they can be specified in an array here.

References Drupal\config(), and t().

Here is the call graph for this function: