annotate core/lib/Drupal/Core/Entity/entity.api.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Hooks and documentation related to entities.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Core\Access\AccessResult;
Chris@0 9 use Drupal\Core\Entity\ContentEntityInterface;
Chris@0 10 use Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface;
Chris@0 11 use Drupal\Core\Field\BaseFieldDefinition;
Chris@0 12 use Drupal\Core\Render\Element;
Chris@0 13 use Drupal\language\Entity\ContentLanguageSettings;
Chris@0 14 use Drupal\node\Entity\NodeType;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * @defgroup entity_crud Entity CRUD, editing, and view hooks
Chris@0 18 * @{
Chris@0 19 * Hooks used in various entity operations.
Chris@0 20 *
Chris@0 21 * Entity create, read, update, and delete (CRUD) operations are performed by
Chris@0 22 * entity storage classes; see the
Chris@0 23 * @link entity_api Entity API topic @endlink for more information. Most
Chris@0 24 * entities use or extend the default classes:
Chris@0 25 * \Drupal\Core\Entity\Sql\SqlContentEntityStorage for content entities, and
Chris@0 26 * \Drupal\Core\Config\Entity\ConfigEntityStorage for configuration entities.
Chris@0 27 * For these entities, there is a set of hooks that is invoked for each
Chris@0 28 * CRUD operation, which module developers can implement to affect these
Chris@0 29 * operations; these hooks are actually invoked from methods on
Chris@0 30 * \Drupal\Core\Entity\EntityStorageBase.
Chris@0 31 *
Chris@0 32 * For content entities, viewing and rendering are handled by a view builder
Chris@0 33 * class; see the @link entity_api Entity API topic @endlink for more
Chris@0 34 * information. Most view builders extend or use the default class
Chris@0 35 * \Drupal\Core\Entity\EntityViewBuilder.
Chris@0 36 *
Chris@0 37 * Entity editing (including adding new entities) is handled by entity form
Chris@0 38 * classes; see the @link entity_api Entity API topic @endlink for more
Chris@0 39 * information. Most entity editing forms extend base classes
Chris@0 40 * \Drupal\Core\Entity\EntityForm or \Drupal\Core\Entity\ContentEntityForm.
Chris@0 41 * Note that many other operations, such as confirming deletion of entities,
Chris@0 42 * also use entity form classes.
Chris@0 43 *
Chris@0 44 * This topic lists all of the entity CRUD and view operations, and the hooks
Chris@0 45 * and other operations that are invoked (in order) for each operation. Some
Chris@0 46 * notes:
Chris@0 47 * - Whenever an entity hook is invoked, there is both a type-specific entity
Chris@0 48 * hook, and a generic entity hook. For instance, during a create operation on
Chris@0 49 * a node, first hook_node_create() and then hook_entity_create() would be
Chris@0 50 * invoked.
Chris@0 51 * - The entity-type-specific hooks are represented in the list below as
Chris@0 52 * hook_ENTITY_TYPE_... (hook_ENTITY_TYPE_create() in this example). To
Chris@0 53 * implement one of these hooks for an entity whose machine name is "foo",
Chris@0 54 * define a function called mymodule_foo_create(), for instance. Also note
Chris@0 55 * that the entity or array of entities that are passed into a specific-type
Chris@0 56 * hook are of the specific entity class, not the generic Entity class, so in
Chris@0 57 * your implementation, you can make the $entity argument something like $node
Chris@0 58 * and give it a specific type hint (which should normally be to the specific
Chris@0 59 * interface, such as \Drupal\Node\NodeInterface for nodes).
Chris@0 60 * - $storage in the code examples is assumed to be an entity storage
Chris@0 61 * class. See the @link entity_api Entity API topic @endlink for
Chris@0 62 * information on how to instantiate the correct storage class for an
Chris@0 63 * entity type.
Chris@0 64 * - $view_builder in the code examples is assumed to be an entity view builder
Chris@0 65 * class. See the @link entity_api Entity API topic @endlink for
Chris@0 66 * information on how to instantiate the correct view builder class for
Chris@0 67 * an entity type.
Chris@0 68 * - During many operations, static methods are called on the entity class,
Chris@0 69 * which implements \Drupal\Entity\EntityInterface.
Chris@0 70 *
Chris@0 71 * @section create Create operations
Chris@0 72 * To create an entity:
Chris@0 73 * @code
Chris@0 74 * $entity = $storage->create();
Chris@0 75 *
Chris@0 76 * // Add code here to set properties on the entity.
Chris@0 77 *
Chris@0 78 * // Until you call save(), the entity is just in memory.
Chris@0 79 * $entity->save();
Chris@0 80 * @endcode
Chris@0 81 * There is also a shortcut method on entity classes, which creates an entity
Chris@0 82 * with an array of provided property values: \Drupal\Core\Entity::create().
Chris@0 83 *
Chris@0 84 * Hooks invoked during the create operation:
Chris@0 85 * - hook_ENTITY_TYPE_create()
Chris@0 86 * - hook_entity_create()
Chris@0 87 *
Chris@0 88 * See @ref save below for the save portion of the operation.
Chris@0 89 *
Chris@0 90 * @section load Read/Load operations
Chris@0 91 * To load (read) a single entity:
Chris@0 92 * @code
Chris@0 93 * $entity = $storage->load($id);
Chris@0 94 * @endcode
Chris@0 95 * To load multiple entities:
Chris@0 96 * @code
Chris@0 97 * $entities = $storage->loadMultiple($ids);
Chris@0 98 * @endcode
Chris@0 99 * Since load() calls loadMultiple(), these are really the same operation.
Chris@0 100 * Here is the order of hooks and other operations that take place during
Chris@0 101 * entity loading:
Chris@0 102 * - Entity is loaded from storage.
Chris@0 103 * - postLoad() is called on the entity class, passing in all of the loaded
Chris@0 104 * entities.
Chris@0 105 * - hook_entity_load()
Chris@0 106 * - hook_ENTITY_TYPE_load()
Chris@0 107 *
Chris@0 108 * When an entity is loaded, normally the default entity revision is loaded.
Chris@0 109 * It is also possible to load a different revision, for entities that support
Chris@0 110 * revisions, with this code:
Chris@0 111 * @code
Chris@0 112 * $entity = $storage->loadRevision($revision_id);
Chris@0 113 * @endcode
Chris@0 114 * This involves the same hooks and operations as regular entity loading.
Chris@0 115 *
Chris@0 116 * @section entities_revisions_translations Entities, revisions and translations
Chris@0 117 *
Chris@0 118 * A translation is not a revision and a revision is not necessarily a
Chris@0 119 * translation. Revisions and translations are the two axes on the "spreadsheet"
Chris@0 120 * of an entity. If you use the built-in UI and have revisions enabled, then a
Chris@0 121 * new translation change would create a new revision (with a copy of all data
Chris@0 122 * for other languages in that revision). If an entity does not use revisions or
Chris@0 123 * the entity is being modified via the API, then multiple translations can be
Chris@0 124 * modified in a single revision. Conceptually, the revisions are columns on the
Chris@0 125 * spreadsheet and translations are rows.
Chris@0 126 *
Chris@0 127 * @section save Save operations
Chris@0 128 * To update an existing entity, you will need to load it, change properties,
Chris@0 129 * and then save; as described above, when creating a new entity, you will also
Chris@0 130 * need to save it. Here is the order of hooks and other events that happen
Chris@0 131 * during an entity save:
Chris@0 132 * - preSave() is called on the entity object, and field objects.
Chris@0 133 * - hook_ENTITY_TYPE_presave()
Chris@0 134 * - hook_entity_presave()
Chris@0 135 * - Entity is saved to storage.
Chris@0 136 * - For updates on content entities, if there is a translation added that
Chris@0 137 * was not previously present:
Chris@0 138 * - hook_ENTITY_TYPE_translation_insert()
Chris@0 139 * - hook_entity_translation_insert()
Chris@0 140 * - For updates on content entities, if there was a translation removed:
Chris@0 141 * - hook_ENTITY_TYPE_translation_delete()
Chris@0 142 * - hook_entity_translation_delete()
Chris@0 143 * - postSave() is called on the entity object.
Chris@0 144 * - hook_ENTITY_TYPE_insert() (new) or hook_ENTITY_TYPE_update() (update)
Chris@0 145 * - hook_entity_insert() (new) or hook_entity_update() (update)
Chris@0 146 *
Chris@0 147 * Some specific entity types invoke hooks during preSave() or postSave()
Chris@0 148 * operations. Examples:
Chris@0 149 * - Field configuration preSave(): hook_field_storage_config_update_forbid()
Chris@0 150 * - Node postSave(): hook_node_access_records() and
Chris@0 151 * hook_node_access_records_alter()
Chris@0 152 * - Config entities that are acting as entity bundles in postSave():
Chris@0 153 * hook_entity_bundle_create()
Chris@0 154 * - Comment: hook_comment_publish() and hook_comment_unpublish() as
Chris@0 155 * appropriate.
Chris@0 156 *
Chris@0 157 * @section edit Editing operations
Chris@0 158 * When an entity's add/edit form is used to add or edit an entity, there
Chris@0 159 * are several hooks that are invoked:
Chris@0 160 * - hook_entity_prepare_form()
Chris@0 161 * - hook_ENTITY_TYPE_prepare_form()
Chris@0 162 * - hook_entity_form_display_alter() (for content entities only)
Chris@0 163 *
Chris@0 164 * @section delete Delete operations
Chris@0 165 * To delete one or more entities, load them and then delete them:
Chris@0 166 * @code
Chris@0 167 * $entities = $storage->loadMultiple($ids);
Chris@0 168 * $storage->delete($entities);
Chris@0 169 * @endcode
Chris@0 170 *
Chris@0 171 * During the delete operation, the following hooks and other events happen:
Chris@0 172 * - preDelete() is called on the entity class.
Chris@0 173 * - hook_ENTITY_TYPE_predelete()
Chris@0 174 * - hook_entity_predelete()
Chris@0 175 * - Entity and field information is removed from storage.
Chris@0 176 * - postDelete() is called on the entity class.
Chris@0 177 * - hook_ENTITY_TYPE_delete()
Chris@0 178 * - hook_entity_delete()
Chris@0 179 *
Chris@0 180 * Some specific entity types invoke hooks during the delete process. Examples:
Chris@0 181 * - Entity bundle postDelete(): hook_entity_bundle_delete()
Chris@0 182 *
Chris@0 183 * Individual revisions of an entity can also be deleted:
Chris@0 184 * @code
Chris@0 185 * $storage->deleteRevision($revision_id);
Chris@0 186 * @endcode
Chris@0 187 * This operation invokes the following operations and hooks:
Chris@0 188 * - Revision is loaded (see @ref load above).
Chris@0 189 * - Revision and field information is removed from the database.
Chris@0 190 * - hook_ENTITY_TYPE_revision_delete()
Chris@0 191 * - hook_entity_revision_delete()
Chris@0 192 *
Chris@0 193 * @section view View/render operations
Chris@0 194 * To make a render array for a loaded entity:
Chris@0 195 * @code
Chris@0 196 * // You can omit the language ID if the default language is being used.
Chris@0 197 * $build = $view_builder->view($entity, 'view_mode_name', $language->getId());
Chris@0 198 * @endcode
Chris@0 199 * You can also use the viewMultiple() method to view multiple entities.
Chris@0 200 *
Chris@0 201 * Hooks invoked during the operation of building a render array:
Chris@0 202 * - hook_entity_view_mode_alter()
Chris@0 203 * - hook_ENTITY_TYPE_build_defaults_alter()
Chris@0 204 * - hook_entity_build_defaults_alter()
Chris@0 205 *
Chris@0 206 * View builders for some types override these hooks, notably:
Chris@0 207 * - The Tour view builder does not invoke any hooks.
Chris@0 208 * - The Block view builder invokes hook_block_view_alter() and
Chris@0 209 * hook_block_view_BASE_BLOCK_ID_alter(). Note that in other view builders,
Chris@0 210 * the view alter hooks are run later in the process.
Chris@0 211 *
Chris@0 212 * During the rendering operation, the default entity viewer runs the following
Chris@0 213 * hooks and operations in the pre-render step:
Chris@0 214 * - hook_entity_view_display_alter()
Chris@0 215 * - hook_entity_prepare_view()
Chris@0 216 * - Entity fields are loaded, and render arrays are built for them using
Chris@0 217 * their formatters.
Chris@0 218 * - hook_entity_display_build_alter()
Chris@0 219 * - hook_ENTITY_TYPE_view()
Chris@0 220 * - hook_entity_view()
Chris@0 221 * - hook_ENTITY_TYPE_view_alter()
Chris@0 222 * - hook_entity_view_alter()
Chris@0 223 *
Chris@0 224 * Some specific builders have specific hooks:
Chris@0 225 * - The Node view builder invokes hook_node_links_alter().
Chris@0 226 * - The Comment view builder invokes hook_comment_links_alter().
Chris@0 227 *
Chris@0 228 * After this point in rendering, the theme system takes over. See the
Chris@0 229 * @link theme_render Theme system and render API topic @endlink for more
Chris@0 230 * information.
Chris@0 231 *
Chris@0 232 * @section misc Other entity hooks
Chris@0 233 * Some types of entities invoke hooks for specific operations:
Chris@0 234 * - Searching nodes:
Chris@0 235 * - hook_ranking()
Chris@0 236 * - Query is executed to find matching nodes
Chris@0 237 * - Resulting node is loaded
Chris@0 238 * - Node render array is built
Chris@0 239 * - comment_node_update_index() is called (this adds "N comments" text)
Chris@0 240 * - hook_node_search_result()
Chris@0 241 * - Search indexing nodes:
Chris@0 242 * - Node is loaded
Chris@0 243 * - Node render array is built
Chris@0 244 * - hook_node_update_index()
Chris@0 245 * @}
Chris@0 246 */
Chris@0 247
Chris@0 248 /**
Chris@0 249 * @defgroup entity_api Entity API
Chris@0 250 * @{
Chris@0 251 * Describes how to define and manipulate content and configuration entities.
Chris@0 252 *
Chris@0 253 * Entities, in Drupal, are objects that are used for persistent storage of
Chris@0 254 * content and configuration information. See the
Chris@0 255 * @link info_types Information types topic @endlink for an overview of the
Chris@0 256 * different types of information, and the
Chris@0 257 * @link config_api Configuration API topic @endlink for more about the
Chris@0 258 * configuration API.
Chris@0 259 *
Chris@0 260 * Each entity is an instance of a particular "entity type". Some content entity
Chris@0 261 * types have sub-types, which are known as "bundles", while for other entity
Chris@0 262 * types, there is only a single bundle. For example, the Node content entity
Chris@0 263 * type, which is used for the main content pages in Drupal, has bundles that
Chris@0 264 * are known as "content types", while the User content type, which is used for
Chris@0 265 * user accounts, has only one bundle.
Chris@0 266 *
Chris@0 267 * The sections below have more information about entities and the Entity API;
Chris@0 268 * for more detailed information, see
Chris@0 269 * https://www.drupal.org/developing/api/entity.
Chris@0 270 *
Chris@0 271 * @section define Defining an entity type
Chris@0 272 * Entity types are defined by modules, using Drupal's Plugin API (see the
Chris@0 273 * @link plugin_api Plugin API topic @endlink for more information about plugins
Chris@0 274 * in general). Here are the steps to follow to define a new entity type:
Chris@0 275 * - Choose a unique machine name, or ID, for your entity type. This normally
Chris@0 276 * starts with (or is the same as) your module's machine name. It should be
Chris@0 277 * as short as possible, and may not exceed 32 characters.
Chris@0 278 * - Define an interface for your entity's get/set methods, usually extending
Chris@0 279 * either \Drupal\Core\Config\Entity\ConfigEntityInterface or
Chris@0 280 * \Drupal\Core\Entity\ContentEntityInterface.
Chris@0 281 * - Define a class for your entity, implementing your interface and extending
Chris@0 282 * either \Drupal\Core\Config\Entity\ConfigEntityBase or
Chris@0 283 * \Drupal\Core\Entity\ContentEntityBase, with annotation for
Chris@0 284 * \@ConfigEntityType or \@ContentEntityType in its documentation block.
Chris@0 285 * If you are defining a content entity type, it is recommended to extend the
Chris@0 286 * \Drupal\Core\Entity\EditorialContentEntityBase base class in order to get
Chris@0 287 * out-of-the-box support for Entity API's revisioning and publishing
Chris@0 288 * features, which will allow your entity type to be used with Drupal's
Chris@0 289 * editorial workflow provided by the Content Moderation module.
Chris@12 290 * - In the annotation, the 'id' property gives the entity type ID, and the
Chris@12 291 * 'label' property gives the human-readable name of the entity type. If you
Chris@12 292 * are defining a content entity type that uses bundles, the 'bundle_label'
Chris@12 293 * property gives the human-readable name to use for a bundle of this entity
Chris@12 294 * type (for example, "Content type" for the Node entity).
Chris@0 295 * - The annotation will refer to several handler classes, which you will also
Chris@0 296 * need to define:
Chris@0 297 * - list_builder: Define a class that extends
Chris@0 298 * \Drupal\Core\Config\Entity\ConfigEntityListBuilder (for configuration
Chris@0 299 * entities) or \Drupal\Core\Entity\EntityListBuilder (for content
Chris@0 300 * entities), to provide an administrative overview for your entities.
Chris@0 301 * - add and edit forms, or default form: Define a class (or two) that
Chris@0 302 * extend(s) \Drupal\Core\Entity\EntityForm to provide add and edit forms
Chris@0 303 * for your entities. For content entities, base class
Chris@0 304 * \Drupal\Core\Entity\ContentEntityForm is a better starting point.
Chris@0 305 * - delete form: Define a class that extends
Chris@0 306 * \Drupal\Core\Entity\EntityConfirmFormBase to provide a delete
Chris@0 307 * confirmation form for your entities.
Chris@0 308 * - view_builder: For content entities and config entities that need to be
Chris@0 309 * viewed, define a class that implements
Chris@0 310 * \Drupal\Core\Entity\EntityViewBuilderInterface (usually extending
Chris@0 311 * \Drupal\Core\Entity\EntityViewBuilder), to display a single entity.
Chris@0 312 * - translation: For translatable content entities (if the 'translatable'
Chris@12 313 * annotation property has value TRUE), define a class that extends
Chris@0 314 * \Drupal\content_translation\ContentTranslationHandler, to translate
Chris@0 315 * the content. Configuration translation is handled automatically by the
Chris@0 316 * Configuration Translation module, without the need of a handler class.
Chris@0 317 * - access: If your configuration entity has complex permissions, you might
Chris@0 318 * need an access control handling, implementing
Chris@12 319 * \Drupal\Core\Entity\EntityAccessControlHandlerInterface, but most
Chris@12 320 * entities can just use the 'admin_permission' annotation property
Chris@12 321 * instead. Note that if you are creating your own access control handler,
Chris@12 322 * you should override the checkAccess() and checkCreateAccess() methods,
Chris@12 323 * not access().
Chris@0 324 * - storage: A class implementing
Chris@0 325 * \Drupal\Core\Entity\EntityStorageInterface. If not specified, content
Chris@0 326 * entities will use \Drupal\Core\Entity\Sql\SqlContentEntityStorage, and
Chris@0 327 * config entities will use \Drupal\Core\Config\Entity\ConfigEntityStorage.
Chris@0 328 * You can extend one of these classes to provide custom behavior.
Chris@0 329 * - views_data: A class implementing \Drupal\views\EntityViewsDataInterface
Chris@0 330 * to provide views data for the entity type. You can autogenerate most of
Chris@0 331 * the views data by extending \Drupal\views\EntityViewsData.
Chris@0 332 * - For content entities, the annotation will refer to a number of database
Chris@0 333 * tables and their fields. These annotation properties, such as 'base_table',
Chris@0 334 * 'data_table', 'entity_keys', etc., are documented on
Chris@0 335 * \Drupal\Core\Entity\EntityType.
Chris@0 336 * - For content entities that are displayed on their own pages, the annotation
Chris@0 337 * will refer to a 'uri_callback' function, which takes an object of the
Chris@0 338 * entity interface you have defined as its parameter, and returns routing
Chris@0 339 * information for the entity page; see node_uri() for an example. You will
Chris@0 340 * also need to add a corresponding route to your module's routing.yml file;
Chris@0 341 * see the entity.node.canonical route in node.routing.yml for an example, and see
Chris@0 342 * @ref sec_routes below for some notes.
Chris@0 343 * - Optionally, instead of defining routes, routes can be auto generated by
Chris@0 344 * providing a route handler. See @ref sec_routes. Otherwise, define routes
Chris@0 345 * and links for the various URLs associated with the entity.
Chris@0 346 * These go into the 'links' annotation, with the link type as the key, and
Chris@0 347 * the path of this link template as the value. The corresponding route
Chris@0 348 * requires the following route name:
Chris@0 349 * "entity.$entity_type_id.$link_template_type". See @ref sec_routes below for
Chris@0 350 * some routing notes. Typical link types are:
Chris@0 351 * - canonical: Default link, either to view (if entities are viewed on their
Chris@0 352 * own pages) or edit the entity.
Chris@0 353 * - delete-form: Confirmation form to delete the entity.
Chris@0 354 * - edit-form: Editing form.
Chris@0 355 * - Other link types specific to your entity type can also be defined.
Chris@12 356 * - If your content entity is fieldable, provide the 'field_ui_base_route'
Chris@12 357 * annotation property, giving the name of the route that the Manage Fields,
Chris@12 358 * Manage Display, and Manage Form Display pages from the Field UI module
Chris@12 359 * will be attached to. This is usually the bundle settings edit page, or an
Chris@12 360 * entity type settings page if there are no bundles.
Chris@0 361 * - If your content entity has bundles, you will also need to define a second
Chris@0 362 * plugin to handle the bundles. This plugin is itself a configuration entity
Chris@0 363 * type, so follow the steps here to define it. The machine name ('id'
Chris@12 364 * annotation property) of this configuration entity class goes into the
Chris@12 365 * 'bundle_entity_type' annotation property on the entity type class. For
Chris@12 366 * example, for the Node entity, the bundle class is
Chris@12 367 * \Drupal\node\Entity\NodeType, whose machine name is 'node_type'. This is
Chris@12 368 * the annotation property 'bundle_entity_type' on the
Chris@12 369 * \Drupal\node\Entity\Node class. Also, the
Chris@12 370 * bundle config entity type annotation must have a 'bundle_of' property,
Chris@0 371 * giving the machine name of the entity type it is acting as a bundle for.
Chris@0 372 * These machine names are considered permanent, they may not be renamed.
Chris@12 373 * - Additional annotation properties can be seen on entity class examples such
Chris@12 374 * as \Drupal\node\Entity\Node (content) and \Drupal\user\Entity\Role
Chris@12 375 * (configuration). These annotation properties are documented on
Chris@0 376 * \Drupal\Core\Entity\EntityType.
Chris@0 377 *
Chris@0 378 * @section sec_routes Entity routes
Chris@0 379 * Entity routes can be defined in *.routing.yml files, like any other route:
Chris@0 380 * see the @link routing Routing API @endlink topic for more information.
Chris@0 381 * Another option for entity routes is to use a route provider class, and
Chris@0 382 * reference it in the annotations on the entity class: see the end of this
Chris@0 383 * section for an example.
Chris@0 384 *
Chris@0 385 * It's possible to use both a YAML file and a provider class for entity
Chris@0 386 * routes, at the same time. Avoid duplicating route names between the two:
Chris@0 387 * if a duplicate route name is found in both locations, the one in the YAML
Chris@0 388 * file takes precedence; regardless, such duplication can be confusing.
Chris@0 389 *
Chris@0 390 * Here's an example YAML route specification, for the block configure form:
Chris@0 391 * @code
Chris@0 392 * entity.block.edit_form:
Chris@0 393 * path: '/admin/structure/block/manage/{block}'
Chris@0 394 * defaults:
Chris@0 395 * _entity_form: 'block.default'
Chris@0 396 * _title: 'Configure block'
Chris@0 397 * requirements:
Chris@0 398 * _entity_access: 'block.update'
Chris@0 399 * @endcode
Chris@0 400 * Some notes on this example:
Chris@0 401 * - path: The {block} in the path is a placeholder, which (for an entity) must
Chris@0 402 * always take the form of {machine_name_of_entity_type}. In the URL, the
Chris@0 403 * placeholder value will be the ID of an entity item. When the route is used,
Chris@0 404 * the entity system will load the corresponding entity item and pass it in as
Chris@0 405 * an object to the controller for the route.
Chris@0 406 * - defaults: For entity form routes, use _entity_form rather than the generic
Chris@0 407 * _controller or _form. The value is composed of the entity type machine name
Chris@0 408 * and a form handler type from the entity annotation (see @ref define above
Chris@0 409 * more more on handlers and annotation). So, in this example, block.default
Chris@0 410 * refers to the 'default' form handler on the block entity type, whose
Chris@0 411 * annotation contains:
Chris@0 412 * @code
Chris@0 413 * handlers = {
Chris@0 414 * "form" = {
Chris@0 415 * "default" = "Drupal\block\BlockForm",
Chris@0 416 * @endcode
Chris@0 417 * If instead of YAML you want to use a route provider class:
Chris@0 418 * - \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider provides canonical,
Chris@0 419 * edit-form, and delete-form routes.
Chris@0 420 * - \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider provides the same
Chris@0 421 * routes, set up to use the administrative theme for edit and delete pages.
Chris@0 422 * - You can also create your own class, extending one of these two classes if
Chris@0 423 * you only want to modify their behaviour slightly.
Chris@0 424 *
Chris@0 425 * To register any route provider class, add lines like the following to your
Chris@0 426 * entity class annotation:
Chris@0 427 * @code
Chris@0 428 * handlers = {
Chris@0 429 * "route_provider" = {
Chris@0 430 * "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
Chris@0 431 * @endcode
Chris@0 432 *
Chris@0 433 * @section bundle Defining a content entity bundle
Chris@0 434 * For entity types that use bundles, such as Node (bundles are content types)
Chris@0 435 * and Taxonomy (bundles are vocabularies), modules and install profiles can
Chris@0 436 * define bundles by supplying default configuration in their config/install
Chris@0 437 * directories. (See the @link config_api Configuration API topic @endlink for
Chris@0 438 * general information about configuration.)
Chris@0 439 *
Chris@0 440 * There are several good examples of this in Drupal Core:
Chris@0 441 * - The Forum module defines a content type in node.type.forum.yml and a
Chris@0 442 * vocabulary in taxonomy.vocabulary.forums.yml
Chris@0 443 * - The Book module defines a content type in node.type.book.yml
Chris@0 444 * - The Standard install profile defines Page and Article content types in
Chris@0 445 * node.type.page.yml and node.type.article.yml, a Tags vocabulary in
Chris@0 446 * taxonomy.vocabulary.tags.yml, and a Node comment type in
Chris@0 447 * comment.type.comment.yml. This profile's configuration is especially
Chris@0 448 * instructive, because it also adds several fields to the Article type, and
Chris@0 449 * it sets up view and form display modes for the node types.
Chris@0 450 *
Chris@0 451 * @section load_query Loading, querying, and rendering entities
Chris@0 452 * To load entities, use the entity storage manager, which is an object
Chris@0 453 * implementing \Drupal\Core\Entity\EntityStorageInterface that you can
Chris@0 454 * retrieve with:
Chris@0 455 * @code
Chris@0 456 * $storage = \Drupal::entityManager()->getStorage('your_entity_type');
Chris@0 457 * // Or if you have a $container variable:
Chris@0 458 * $storage = $container->get('entity.manager')->getStorage('your_entity_type');
Chris@0 459 * @endcode
Chris@0 460 * Here, 'your_entity_type' is the machine name of your entity type ('id'
Chris@12 461 * annotation property on the entity class), and note that you should use
Chris@12 462 * dependency injection to retrieve this object if possible. See the
Chris@0 463 * @link container Services and Dependency Injection topic @endlink for more
Chris@0 464 * about how to properly retrieve services.
Chris@0 465 *
Chris@0 466 * To query to find entities to load, use an entity query, which is a object
Chris@0 467 * implementing \Drupal\Core\Entity\Query\QueryInterface that you can retrieve
Chris@0 468 * with:
Chris@0 469 * @code
Chris@0 470 * // Simple query:
Chris@0 471 * $query = \Drupal::entityQuery('your_entity_type');
Chris@0 472 * // Or, if you have a $container variable:
Chris@0 473 * $storage = $container->get('entity_type.manager')->getStorage('your_entity_type');
Chris@0 474 * $query = $storage->getQuery();
Chris@0 475 * @endcode
Chris@0 476 * If you need aggregation, there is an aggregate query available, which
Chris@0 477 * implements \Drupal\Core\Entity\Query\QueryAggregateInterface:
Chris@0 478 * @code
Chris@0 479 * $query \Drupal::entityQueryAggregate('your_entity_type');
Chris@0 480 * // Or:
Chris@0 481 * $query = $storage->getAggregateQuery('your_entity_type');
Chris@0 482 * @endcode
Chris@0 483 *
Chris@0 484 * In either case, you can then add conditions to your query, using methods
Chris@0 485 * like condition(), exists(), etc. on $query; add sorting, pager, and range
Chris@0 486 * if needed, and execute the query to return a list of entity IDs that match
Chris@0 487 * the query.
Chris@0 488 *
Chris@0 489 * Here is an example, using the core File entity:
Chris@0 490 * @code
Chris@0 491 * $fids = Drupal::entityQuery('file')
Chris@0 492 * ->condition('status', FILE_STATUS_PERMANENT, '<>')
Chris@0 493 * ->condition('changed', REQUEST_TIME - $age, '<')
Chris@0 494 * ->range(0, 100)
Chris@0 495 * ->execute();
Chris@0 496 * $files = $storage->loadMultiple($fids);
Chris@0 497 * @endcode
Chris@0 498 *
Chris@0 499 * The normal way of viewing entities is by using a route, as described in the
Chris@0 500 * sections above. If for some reason you need to render an entity in code in a
Chris@0 501 * particular view mode, you can use an entity view builder, which is an object
Chris@0 502 * implementing \Drupal\Core\Entity\EntityViewBuilderInterface that you can
Chris@0 503 * retrieve with:
Chris@0 504 * @code
Chris@0 505 * $view_builder = \Drupal::entityManager()->getViewBuilder('your_entity_type');
Chris@0 506 * // Or if you have a $container variable:
Chris@0 507 * $view_builder = $container->get('entity.manager')->getViewBuilder('your_entity_type');
Chris@0 508 * @endcode
Chris@0 509 * Then, to build and render the entity:
Chris@0 510 * @code
Chris@0 511 * // You can omit the language ID, by default the current content language will
Chris@0 512 * // be used. If no translation is available for the current language, fallback
Chris@0 513 * // rules will be used.
Chris@0 514 * $build = $view_builder->view($entity, 'view_mode_name', $language->getId());
Chris@0 515 * // $build is a render array.
Chris@0 516 * $rendered = drupal_render($build);
Chris@0 517 * @endcode
Chris@0 518 *
Chris@0 519 * @section sec_access Access checking on entities
Chris@0 520 * Entity types define their access permission scheme in their annotation.
Chris@0 521 * Access permissions can be quite complex, so you should not assume any
Chris@0 522 * particular permission scheme. Instead, once you have an entity object
Chris@0 523 * loaded, you can check for permission for a particular operation (such as
Chris@0 524 * 'view') at the entity or field level by calling:
Chris@0 525 * @code
Chris@0 526 * $entity->access($operation);
Chris@0 527 * $entity->nameOfField->access($operation);
Chris@0 528 * @endcode
Chris@0 529 * The interface related to access checking in entities and fields is
Chris@0 530 * \Drupal\Core\Access\AccessibleInterface.
Chris@0 531 *
Chris@0 532 * The default entity access control handler invokes two hooks while checking
Chris@0 533 * access on a single entity: hook_entity_access() is invoked first, and
Chris@0 534 * then hook_ENTITY_TYPE_access() (where ENTITY_TYPE is the machine name
Chris@0 535 * of the entity type). If no module returns a TRUE or FALSE value from
Chris@0 536 * either of these hooks, then the entity's default access checking takes
Chris@0 537 * place. For create operations (creating a new entity), the hooks that
Chris@0 538 * are invoked are hook_entity_create_access() and
Chris@0 539 * hook_ENTITY_TYPE_create_access() instead.
Chris@0 540 *
Chris@0 541 * The Node entity type has a complex system for determining access, which
Chris@0 542 * developers can interact with. This is described in the
Chris@0 543 * @link node_access Node access topic. @endlink
Chris@0 544 *
Chris@0 545 * @see i18n
Chris@0 546 * @see entity_crud
Chris@0 547 * @see \Drupal\Core\Entity\EntityManagerInterface::getTranslationFromContext()
Chris@0 548 * @}
Chris@0 549 */
Chris@0 550
Chris@0 551 /**
Chris@0 552 * @addtogroup hooks
Chris@0 553 * @{
Chris@0 554 */
Chris@0 555
Chris@0 556 /**
Chris@0 557 * Control entity operation access.
Chris@0 558 *
Chris@0 559 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 560 * The entity to check access to.
Chris@0 561 * @param string $operation
Chris@0 562 * The operation that is to be performed on $entity.
Chris@0 563 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 564 * The account trying to access the entity.
Chris@0 565 *
Chris@0 566 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 567 * The access result. The final result is calculated by using
Chris@0 568 * \Drupal\Core\Access\AccessResultInterface::orIf() on the result of every
Chris@0 569 * hook_entity_access() and hook_ENTITY_TYPE_access() implementation, and the
Chris@0 570 * result of the entity-specific checkAccess() method in the entity access
Chris@0 571 * control handler. Be careful when writing generalized access checks shared
Chris@0 572 * between routing and entity checks: routing uses the andIf() operator. So
Chris@0 573 * returning an isNeutral() does not determine entity access at all but it
Chris@0 574 * always ends up denying access while routing.
Chris@0 575 *
Chris@0 576 * @see \Drupal\Core\Entity\EntityAccessControlHandler
Chris@0 577 * @see hook_entity_create_access()
Chris@0 578 * @see hook_ENTITY_TYPE_access()
Chris@0 579 *
Chris@0 580 * @ingroup entity_api
Chris@0 581 */
Chris@0 582 function hook_entity_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) {
Chris@0 583 // No opinion.
Chris@0 584 return AccessResult::neutral();
Chris@0 585 }
Chris@0 586
Chris@0 587 /**
Chris@0 588 * Control entity operation access for a specific entity type.
Chris@0 589 *
Chris@0 590 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 591 * The entity to check access to.
Chris@0 592 * @param string $operation
Chris@0 593 * The operation that is to be performed on $entity.
Chris@0 594 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 595 * The account trying to access the entity.
Chris@0 596 *
Chris@0 597 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 598 * The access result. hook_entity_access() has detailed documentation.
Chris@0 599 *
Chris@0 600 * @see \Drupal\Core\Entity\EntityAccessControlHandler
Chris@0 601 * @see hook_ENTITY_TYPE_create_access()
Chris@0 602 * @see hook_entity_access()
Chris@0 603 *
Chris@0 604 * @ingroup entity_api
Chris@0 605 */
Chris@0 606 function hook_ENTITY_TYPE_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) {
Chris@0 607 // No opinion.
Chris@0 608 return AccessResult::neutral();
Chris@0 609 }
Chris@0 610
Chris@0 611 /**
Chris@0 612 * Control entity create access.
Chris@0 613 *
Chris@0 614 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 615 * The account trying to access the entity.
Chris@0 616 * @param array $context
Chris@0 617 * An associative array of additional context values. By default it contains
Chris@0 618 * language and the entity type ID:
Chris@0 619 * - entity_type_id - the entity type ID.
Chris@0 620 * - langcode - the current language code.
Chris@0 621 * @param string $entity_bundle
Chris@0 622 * The entity bundle name.
Chris@0 623 *
Chris@0 624 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 625 * The access result.
Chris@0 626 *
Chris@0 627 * @see \Drupal\Core\Entity\EntityAccessControlHandler
Chris@0 628 * @see hook_entity_access()
Chris@0 629 * @see hook_ENTITY_TYPE_create_access()
Chris@0 630 *
Chris@0 631 * @ingroup entity_api
Chris@0 632 */
Chris@0 633 function hook_entity_create_access(\Drupal\Core\Session\AccountInterface $account, array $context, $entity_bundle) {
Chris@0 634 // No opinion.
Chris@0 635 return AccessResult::neutral();
Chris@0 636 }
Chris@0 637
Chris@0 638 /**
Chris@0 639 * Control entity create access for a specific entity type.
Chris@0 640 *
Chris@0 641 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 642 * The account trying to access the entity.
Chris@0 643 * @param array $context
Chris@0 644 * An associative array of additional context values. By default it contains
Chris@0 645 * language:
Chris@0 646 * - langcode - the current language code.
Chris@0 647 * @param string $entity_bundle
Chris@0 648 * The entity bundle name.
Chris@0 649 *
Chris@0 650 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 651 * The access result.
Chris@0 652 *
Chris@0 653 * @see \Drupal\Core\Entity\EntityAccessControlHandler
Chris@0 654 * @see hook_ENTITY_TYPE_access()
Chris@0 655 * @see hook_entity_create_access()
Chris@0 656 *
Chris@0 657 * @ingroup entity_api
Chris@0 658 */
Chris@0 659 function hook_ENTITY_TYPE_create_access(\Drupal\Core\Session\AccountInterface $account, array $context, $entity_bundle) {
Chris@0 660 // No opinion.
Chris@0 661 return AccessResult::neutral();
Chris@0 662 }
Chris@0 663
Chris@0 664 /**
Chris@0 665 * Add to entity type definitions.
Chris@0 666 *
Chris@0 667 * Modules may implement this hook to add information to defined entity types,
Chris@0 668 * as defined in \Drupal\Core\Entity\EntityTypeInterface.
Chris@0 669 *
Chris@0 670 * To alter existing information or to add information dynamically, use
Chris@0 671 * hook_entity_type_alter().
Chris@0 672 *
Chris@0 673 * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
Chris@0 674 * An associative array of all entity type definitions, keyed by the entity
Chris@0 675 * type name. Passed by reference.
Chris@0 676 *
Chris@0 677 * @see \Drupal\Core\Entity\Entity
Chris@0 678 * @see \Drupal\Core\Entity\EntityTypeInterface
Chris@0 679 * @see hook_entity_type_alter()
Chris@0 680 */
Chris@0 681 function hook_entity_type_build(array &$entity_types) {
Chris@0 682 /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
Chris@0 683 // Add a form for a custom node form without overriding the default
Chris@0 684 // node form. To override the default node form, use hook_entity_type_alter().
Chris@0 685 $entity_types['node']->setFormClass('mymodule_foo', 'Drupal\mymodule\NodeFooForm');
Chris@0 686 }
Chris@0 687
Chris@0 688 /**
Chris@0 689 * Alter the entity type definitions.
Chris@0 690 *
Chris@0 691 * Modules may implement this hook to alter the information that defines an
Chris@0 692 * entity type. All properties that are available in
Chris@0 693 * \Drupal\Core\Entity\Annotation\EntityType and all the ones additionally
Chris@0 694 * provided by modules can be altered here.
Chris@0 695 *
Chris@0 696 * Do not use this hook to add information to entity types, unless one of the
Chris@0 697 * following is true:
Chris@0 698 * - You are filling in default values.
Chris@0 699 * - You need to dynamically add information only in certain circumstances.
Chris@0 700 * - Your hook needs to run after hook_entity_type_build() implementations.
Chris@0 701 * Use hook_entity_type_build() instead in all other cases.
Chris@0 702 *
Chris@0 703 * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
Chris@0 704 * An associative array of all entity type definitions, keyed by the entity
Chris@0 705 * type name. Passed by reference.
Chris@0 706 *
Chris@0 707 * @see \Drupal\Core\Entity\Entity
Chris@0 708 * @see \Drupal\Core\Entity\EntityTypeInterface
Chris@0 709 */
Chris@0 710 function hook_entity_type_alter(array &$entity_types) {
Chris@0 711 /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
Chris@0 712 // Set the controller class for nodes to an alternate implementation of the
Chris@0 713 // Drupal\Core\Entity\EntityStorageInterface interface.
Chris@0 714 $entity_types['node']->setStorageClass('Drupal\mymodule\MyCustomNodeStorage');
Chris@0 715 }
Chris@0 716
Chris@0 717 /**
Chris@0 718 * Alter the view modes for entity types.
Chris@0 719 *
Chris@0 720 * @param array $view_modes
Chris@0 721 * An array of view modes, keyed first by entity type, then by view mode name.
Chris@0 722 *
Chris@0 723 * @see \Drupal\Core\Entity\EntityManagerInterface::getAllViewModes()
Chris@0 724 * @see \Drupal\Core\Entity\EntityManagerInterface::getViewModes()
Chris@0 725 */
Chris@0 726 function hook_entity_view_mode_info_alter(&$view_modes) {
Chris@0 727 $view_modes['user']['full']['status'] = TRUE;
Chris@0 728 }
Chris@0 729
Chris@0 730 /**
Chris@0 731 * Describe the bundles for entity types.
Chris@0 732 *
Chris@0 733 * @return array
Chris@0 734 * An associative array of all entity bundles, keyed by the entity
Chris@0 735 * type name, and then the bundle name, with the following keys:
Chris@0 736 * - label: The human-readable name of the bundle.
Chris@0 737 * - uri_callback: The same as the 'uri_callback' key defined for the entity
Chris@0 738 * type in the EntityManager, but for the bundle only. When determining
Chris@0 739 * the URI of an entity, if a 'uri_callback' is defined for both the
Chris@0 740 * entity type and the bundle, the one for the bundle is used.
Chris@0 741 * - translatable: (optional) A boolean value specifying whether this bundle
Chris@0 742 * has translation support enabled. Defaults to FALSE.
Chris@0 743 *
Chris@0 744 * @see \Drupal\Core\Entity\EntityTypeBundleInfo::getBundleInfo()
Chris@0 745 * @see hook_entity_bundle_info_alter()
Chris@0 746 */
Chris@0 747 function hook_entity_bundle_info() {
Chris@0 748 $bundles['user']['user']['label'] = t('User');
Chris@0 749 return $bundles;
Chris@0 750 }
Chris@0 751
Chris@0 752 /**
Chris@0 753 * Alter the bundles for entity types.
Chris@0 754 *
Chris@0 755 * @param array $bundles
Chris@0 756 * An array of bundles, keyed first by entity type, then by bundle name.
Chris@0 757 *
Chris@0 758 * @see Drupal\Core\Entity\EntityTypeBundleInfo::getBundleInfo()
Chris@0 759 * @see hook_entity_bundle_info()
Chris@0 760 */
Chris@0 761 function hook_entity_bundle_info_alter(&$bundles) {
Chris@0 762 $bundles['user']['user']['label'] = t('Full account');
Chris@0 763 }
Chris@0 764
Chris@0 765 /**
Chris@0 766 * Act on entity_bundle_create().
Chris@0 767 *
Chris@0 768 * This hook is invoked after the operation has been performed.
Chris@0 769 *
Chris@0 770 * @param string $entity_type_id
Chris@0 771 * The type of $entity; e.g. 'node' or 'user'.
Chris@0 772 * @param string $bundle
Chris@0 773 * The name of the bundle.
Chris@0 774 *
Chris@0 775 * @see entity_crud
Chris@0 776 */
Chris@0 777 function hook_entity_bundle_create($entity_type_id, $bundle) {
Chris@0 778 // When a new bundle is created, the menu needs to be rebuilt to add the
Chris@0 779 // Field UI menu item tabs.
Chris@0 780 \Drupal::service('router.builder')->setRebuildNeeded();
Chris@0 781 }
Chris@0 782
Chris@0 783 /**
Chris@0 784 * Act on entity_bundle_delete().
Chris@0 785 *
Chris@0 786 * This hook is invoked after the operation has been performed.
Chris@0 787 *
Chris@0 788 * @param string $entity_type_id
Chris@0 789 * The type of entity; for example, 'node' or 'user'.
Chris@0 790 * @param string $bundle
Chris@0 791 * The bundle that was just deleted.
Chris@0 792 *
Chris@0 793 * @ingroup entity_crud
Chris@0 794 */
Chris@0 795 function hook_entity_bundle_delete($entity_type_id, $bundle) {
Chris@0 796 // Remove the settings associated with the bundle in my_module.settings.
Chris@0 797 $config = \Drupal::config('my_module.settings');
Chris@0 798 $bundle_settings = $config->get('bundle_settings');
Chris@0 799 if (isset($bundle_settings[$entity_type_id][$bundle])) {
Chris@0 800 unset($bundle_settings[$entity_type_id][$bundle]);
Chris@0 801 $config->set('bundle_settings', $bundle_settings);
Chris@0 802 }
Chris@0 803 }
Chris@0 804
Chris@0 805 /**
Chris@0 806 * Acts when creating a new entity.
Chris@0 807 *
Chris@0 808 * This hook runs after a new entity object has just been instantiated.
Chris@0 809 *
Chris@0 810 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 811 * The entity object.
Chris@0 812 *
Chris@0 813 * @ingroup entity_crud
Chris@0 814 * @see hook_ENTITY_TYPE_create()
Chris@0 815 */
Chris@0 816 function hook_entity_create(\Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 817 \Drupal::logger('example')->info('Entity created: @label', ['@label' => $entity->label()]);
Chris@0 818 }
Chris@0 819
Chris@0 820 /**
Chris@0 821 * Acts when creating a new entity of a specific type.
Chris@0 822 *
Chris@0 823 * This hook runs after a new entity object has just been instantiated.
Chris@0 824 *
Chris@0 825 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 826 * The entity object.
Chris@0 827 *
Chris@0 828 * @ingroup entity_crud
Chris@0 829 * @see hook_entity_create()
Chris@0 830 */
Chris@0 831 function hook_ENTITY_TYPE_create(\Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 832 \Drupal::logger('example')->info('ENTITY_TYPE created: @label', ['@label' => $entity->label()]);
Chris@0 833 }
Chris@0 834
Chris@0 835 /**
Chris@0 836 * Act on entities when loaded.
Chris@0 837 *
Chris@0 838 * This is a generic load hook called for all entity types loaded via the
Chris@0 839 * entity API.
Chris@0 840 *
Chris@0 841 * hook_entity_storage_load() should be used to load additional data for
Chris@0 842 * content entities.
Chris@0 843 *
Chris@0 844 * @param \Drupal\Core\Entity\EntityInterface[] $entities
Chris@0 845 * The entities keyed by entity ID.
Chris@0 846 * @param string $entity_type_id
Chris@0 847 * The type of entities being loaded (i.e. node, user, comment).
Chris@0 848 *
Chris@0 849 * @ingroup entity_crud
Chris@0 850 * @see hook_ENTITY_TYPE_load()
Chris@0 851 */
Chris@0 852 function hook_entity_load(array $entities, $entity_type_id) {
Chris@0 853 foreach ($entities as $entity) {
Chris@0 854 $entity->foo = mymodule_add_something($entity);
Chris@0 855 }
Chris@0 856 }
Chris@0 857
Chris@0 858 /**
Chris@0 859 * Act on entities of a specific type when loaded.
Chris@0 860 *
Chris@0 861 * @param array $entities
Chris@0 862 * The entities keyed by entity ID.
Chris@0 863 *
Chris@0 864 * @ingroup entity_crud
Chris@0 865 * @see hook_entity_load()
Chris@0 866 */
Chris@0 867 function hook_ENTITY_TYPE_load($entities) {
Chris@0 868 foreach ($entities as $entity) {
Chris@0 869 $entity->foo = mymodule_add_something($entity);
Chris@0 870 }
Chris@0 871 }
Chris@0 872
Chris@0 873 /**
Chris@0 874 * Act on content entities when loaded from the storage.
Chris@0 875 *
Chris@0 876 * The results of this hook will be cached.
Chris@0 877 *
Chris@0 878 * @param \Drupal\Core\Entity\EntityInterface[] $entities
Chris@0 879 * The entities keyed by entity ID.
Chris@0 880 * @param string $entity_type
Chris@0 881 * The type of entities being loaded (i.e. node, user, comment).
Chris@0 882 *
Chris@0 883 * @see hook_entity_load()
Chris@0 884 */
Chris@0 885 function hook_entity_storage_load(array $entities, $entity_type) {
Chris@0 886 foreach ($entities as $entity) {
Chris@0 887 $entity->foo = mymodule_add_something_uncached($entity);
Chris@0 888 }
Chris@0 889 }
Chris@0 890
Chris@0 891 /**
Chris@0 892 * Act on content entities of a given type when loaded from the storage.
Chris@0 893 *
Chris@0 894 * The results of this hook will be cached if the entity type supports it.
Chris@0 895 *
Chris@0 896 * @param \Drupal\Core\Entity\EntityInterface[] $entities
Chris@0 897 * The entities keyed by entity ID.
Chris@0 898 *
Chris@0 899 * @see hook_entity_storage_load()
Chris@0 900 */
Chris@0 901 function hook_ENTITY_TYPE_storage_load(array $entities) {
Chris@0 902 foreach ($entities as $entity) {
Chris@0 903 $entity->foo = mymodule_add_something_uncached($entity);
Chris@0 904 }
Chris@0 905 }
Chris@0 906
Chris@0 907 /**
Chris@0 908 * Act on an entity before it is created or updated.
Chris@0 909 *
Chris@0 910 * You can get the original entity object from $entity->original when it is an
Chris@0 911 * update of the entity.
Chris@0 912 *
Chris@0 913 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 914 * The entity object.
Chris@0 915 *
Chris@0 916 * @ingroup entity_crud
Chris@0 917 * @see hook_ENTITY_TYPE_presave()
Chris@0 918 */
Chris@0 919 function hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 920 if ($entity instanceof ContentEntityInterface && $entity->isTranslatable()) {
Chris@0 921 $route_match = \Drupal::routeMatch();
Chris@0 922 \Drupal::service('content_translation.synchronizer')->synchronizeFields($entity, $entity->language()->getId(), $route_match->getParameter('source_langcode'));
Chris@0 923 }
Chris@0 924 }
Chris@0 925
Chris@0 926 /**
Chris@0 927 * Act on a specific type of entity before it is created or updated.
Chris@0 928 *
Chris@0 929 * You can get the original entity object from $entity->original when it is an
Chris@0 930 * update of the entity.
Chris@0 931 *
Chris@0 932 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 933 * The entity object.
Chris@0 934 *
Chris@0 935 * @ingroup entity_crud
Chris@0 936 * @see hook_entity_presave()
Chris@0 937 */
Chris@0 938 function hook_ENTITY_TYPE_presave(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 939 if ($entity->isTranslatable()) {
Chris@0 940 $route_match = \Drupal::routeMatch();
Chris@0 941 \Drupal::service('content_translation.synchronizer')->synchronizeFields($entity, $entity->language()->getId(), $route_match->getParameter('source_langcode'));
Chris@0 942 }
Chris@0 943 }
Chris@0 944
Chris@0 945 /**
Chris@0 946 * Respond to creation of a new entity.
Chris@0 947 *
Chris@0 948 * This hook runs once the entity has been stored. Note that hook
Chris@0 949 * implementations may not alter the stored entity data.
Chris@0 950 *
Chris@0 951 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 952 * The entity object.
Chris@0 953 *
Chris@0 954 * @ingroup entity_crud
Chris@0 955 * @see hook_ENTITY_TYPE_insert()
Chris@0 956 */
Chris@0 957 function hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 958 // Insert the new entity into a fictional table of all entities.
Chris@0 959 db_insert('example_entity')
Chris@0 960 ->fields([
Chris@0 961 'type' => $entity->getEntityTypeId(),
Chris@0 962 'id' => $entity->id(),
Chris@0 963 'created' => REQUEST_TIME,
Chris@0 964 'updated' => REQUEST_TIME,
Chris@0 965 ])
Chris@0 966 ->execute();
Chris@0 967 }
Chris@0 968
Chris@0 969 /**
Chris@0 970 * Respond to creation of a new entity of a particular type.
Chris@0 971 *
Chris@0 972 * This hook runs once the entity has been stored. Note that hook
Chris@0 973 * implementations may not alter the stored entity data.
Chris@0 974 *
Chris@0 975 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 976 * The entity object.
Chris@0 977 *
Chris@0 978 * @ingroup entity_crud
Chris@0 979 * @see hook_entity_insert()
Chris@0 980 */
Chris@0 981 function hook_ENTITY_TYPE_insert(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 982 // Insert the new entity into a fictional table of this type of entity.
Chris@0 983 db_insert('example_entity')
Chris@0 984 ->fields([
Chris@0 985 'id' => $entity->id(),
Chris@0 986 'created' => REQUEST_TIME,
Chris@0 987 'updated' => REQUEST_TIME,
Chris@0 988 ])
Chris@0 989 ->execute();
Chris@0 990 }
Chris@0 991
Chris@0 992 /**
Chris@0 993 * Respond to updates to an entity.
Chris@0 994 *
Chris@0 995 * This hook runs once the entity storage has been updated. Note that hook
Chris@0 996 * implementations may not alter the stored entity data. Get the original entity
Chris@0 997 * object from $entity->original.
Chris@0 998 *
Chris@0 999 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1000 * The entity object.
Chris@0 1001 *
Chris@0 1002 * @ingroup entity_crud
Chris@0 1003 * @see hook_ENTITY_TYPE_update()
Chris@0 1004 */
Chris@0 1005 function hook_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 1006 // Update the entity's entry in a fictional table of all entities.
Chris@0 1007 db_update('example_entity')
Chris@0 1008 ->fields([
Chris@0 1009 'updated' => REQUEST_TIME,
Chris@0 1010 ])
Chris@0 1011 ->condition('type', $entity->getEntityTypeId())
Chris@0 1012 ->condition('id', $entity->id())
Chris@0 1013 ->execute();
Chris@0 1014 }
Chris@0 1015
Chris@0 1016 /**
Chris@0 1017 * Respond to updates to an entity of a particular type.
Chris@0 1018 *
Chris@0 1019 * This hook runs once the entity storage has been updated. Note that hook
Chris@0 1020 * implementations may not alter the stored entity data. Get the original entity
Chris@0 1021 * object from $entity->original.
Chris@0 1022 *
Chris@0 1023 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1024 * The entity object.
Chris@0 1025 *
Chris@0 1026 * @ingroup entity_crud
Chris@0 1027 * @see hook_entity_update()
Chris@0 1028 */
Chris@0 1029 function hook_ENTITY_TYPE_update(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 1030 // Update the entity's entry in a fictional table of this type of entity.
Chris@0 1031 db_update('example_entity')
Chris@0 1032 ->fields([
Chris@0 1033 'updated' => REQUEST_TIME,
Chris@0 1034 ])
Chris@0 1035 ->condition('id', $entity->id())
Chris@0 1036 ->execute();
Chris@0 1037 }
Chris@0 1038
Chris@0 1039 /**
Chris@0 1040 * Acts when creating a new entity translation.
Chris@0 1041 *
Chris@0 1042 * This hook runs after a new entity translation object has just been
Chris@0 1043 * instantiated.
Chris@0 1044 *
Chris@0 1045 * @param \Drupal\Core\Entity\EntityInterface $translation
Chris@0 1046 * The entity object.
Chris@0 1047 *
Chris@0 1048 * @ingroup entity_crud
Chris@0 1049 * @see hook_ENTITY_TYPE_translation_create()
Chris@0 1050 */
Chris@0 1051 function hook_entity_translation_create(\Drupal\Core\Entity\EntityInterface $translation) {
Chris@0 1052 \Drupal::logger('example')->info('Entity translation created: @label', ['@label' => $translation->label()]);
Chris@0 1053 }
Chris@0 1054
Chris@0 1055 /**
Chris@0 1056 * Acts when creating a new entity translation of a specific type.
Chris@0 1057 *
Chris@0 1058 * This hook runs after a new entity translation object has just been
Chris@0 1059 * instantiated.
Chris@0 1060 *
Chris@0 1061 * @param \Drupal\Core\Entity\EntityInterface $translation
Chris@0 1062 * The entity object.
Chris@0 1063 *
Chris@0 1064 * @ingroup entity_crud
Chris@0 1065 * @see hook_entity_translation_create()
Chris@0 1066 */
Chris@0 1067 function hook_ENTITY_TYPE_translation_create(\Drupal\Core\Entity\EntityInterface $translation) {
Chris@0 1068 \Drupal::logger('example')->info('ENTITY_TYPE translation created: @label', ['@label' => $translation->label()]);
Chris@0 1069 }
Chris@0 1070
Chris@0 1071 /**
Chris@0 1072 * Respond to creation of a new entity translation.
Chris@0 1073 *
Chris@0 1074 * This hook runs once the entity translation has been stored. Note that hook
Chris@0 1075 * implementations may not alter the stored entity translation data.
Chris@0 1076 *
Chris@0 1077 * @param \Drupal\Core\Entity\EntityInterface $translation
Chris@0 1078 * The entity object of the translation just stored.
Chris@0 1079 *
Chris@0 1080 * @ingroup entity_crud
Chris@0 1081 * @see hook_ENTITY_TYPE_translation_insert()
Chris@0 1082 */
Chris@0 1083 function hook_entity_translation_insert(\Drupal\Core\Entity\EntityInterface $translation) {
Chris@0 1084 $variables = [
Chris@0 1085 '@language' => $translation->language()->getName(),
Chris@0 1086 '@label' => $translation->getUntranslated()->label(),
Chris@0 1087 ];
Chris@0 1088 \Drupal::logger('example')->notice('The @language translation of @label has just been stored.', $variables);
Chris@0 1089 }
Chris@0 1090
Chris@0 1091 /**
Chris@0 1092 * Respond to creation of a new entity translation of a particular type.
Chris@0 1093 *
Chris@0 1094 * This hook runs once the entity translation has been stored. Note that hook
Chris@0 1095 * implementations may not alter the stored entity translation data.
Chris@0 1096 *
Chris@0 1097 * @param \Drupal\Core\Entity\EntityInterface $translation
Chris@0 1098 * The entity object of the translation just stored.
Chris@0 1099 *
Chris@0 1100 * @ingroup entity_crud
Chris@0 1101 * @see hook_entity_translation_insert()
Chris@0 1102 */
Chris@0 1103 function hook_ENTITY_TYPE_translation_insert(\Drupal\Core\Entity\EntityInterface $translation) {
Chris@0 1104 $variables = [
Chris@0 1105 '@language' => $translation->language()->getName(),
Chris@0 1106 '@label' => $translation->getUntranslated()->label(),
Chris@0 1107 ];
Chris@0 1108 \Drupal::logger('example')->notice('The @language translation of @label has just been stored.', $variables);
Chris@0 1109 }
Chris@0 1110
Chris@0 1111 /**
Chris@0 1112 * Respond to entity translation deletion.
Chris@0 1113 *
Chris@0 1114 * This hook runs once the entity translation has been deleted from storage.
Chris@0 1115 *
Chris@0 1116 * @param \Drupal\Core\Entity\EntityInterface $translation
Chris@0 1117 * The original entity object.
Chris@0 1118 *
Chris@0 1119 * @ingroup entity_crud
Chris@0 1120 * @see hook_ENTITY_TYPE_translation_delete()
Chris@0 1121 */
Chris@0 1122 function hook_entity_translation_delete(\Drupal\Core\Entity\EntityInterface $translation) {
Chris@0 1123 $variables = [
Chris@0 1124 '@language' => $translation->language()->getName(),
Chris@0 1125 '@label' => $translation->label(),
Chris@0 1126 ];
Chris@0 1127 \Drupal::logger('example')->notice('The @language translation of @label has just been deleted.', $variables);
Chris@0 1128 }
Chris@0 1129
Chris@0 1130 /**
Chris@0 1131 * Respond to entity translation deletion of a particular type.
Chris@0 1132 *
Chris@0 1133 * This hook runs once the entity translation has been deleted from storage.
Chris@0 1134 *
Chris@0 1135 * @param \Drupal\Core\Entity\EntityInterface $translation
Chris@0 1136 * The original entity object.
Chris@0 1137 *
Chris@0 1138 * @ingroup entity_crud
Chris@0 1139 * @see hook_entity_translation_delete()
Chris@0 1140 */
Chris@0 1141 function hook_ENTITY_TYPE_translation_delete(\Drupal\Core\Entity\EntityInterface $translation) {
Chris@0 1142 $variables = [
Chris@0 1143 '@language' => $translation->language()->getName(),
Chris@0 1144 '@label' => $translation->label(),
Chris@0 1145 ];
Chris@0 1146 \Drupal::logger('example')->notice('The @language translation of @label has just been deleted.', $variables);
Chris@0 1147 }
Chris@0 1148
Chris@0 1149 /**
Chris@0 1150 * Act before entity deletion.
Chris@0 1151 *
Chris@0 1152 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1153 * The entity object for the entity that is about to be deleted.
Chris@0 1154 *
Chris@0 1155 * @ingroup entity_crud
Chris@0 1156 * @see hook_ENTITY_TYPE_predelete()
Chris@0 1157 */
Chris@0 1158 function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 1159 // Count references to this entity in a custom table before they are removed
Chris@0 1160 // upon entity deletion.
Chris@0 1161 $id = $entity->id();
Chris@0 1162 $type = $entity->getEntityTypeId();
Chris@0 1163 $count = db_select('example_entity_data')
Chris@0 1164 ->condition('type', $type)
Chris@0 1165 ->condition('id', $id)
Chris@0 1166 ->countQuery()
Chris@0 1167 ->execute()
Chris@0 1168 ->fetchField();
Chris@0 1169
Chris@0 1170 // Log the count in a table that records this statistic for deleted entities.
Chris@0 1171 db_merge('example_deleted_entity_statistics')
Chris@0 1172 ->key(['type' => $type, 'id' => $id])
Chris@0 1173 ->fields(['count' => $count])
Chris@0 1174 ->execute();
Chris@0 1175 }
Chris@0 1176
Chris@0 1177 /**
Chris@0 1178 * Act before entity deletion of a particular entity type.
Chris@0 1179 *
Chris@0 1180 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1181 * The entity object for the entity that is about to be deleted.
Chris@0 1182 *
Chris@0 1183 * @ingroup entity_crud
Chris@0 1184 * @see hook_entity_predelete()
Chris@0 1185 */
Chris@0 1186 function hook_ENTITY_TYPE_predelete(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 1187 // Count references to this entity in a custom table before they are removed
Chris@0 1188 // upon entity deletion.
Chris@0 1189 $id = $entity->id();
Chris@0 1190 $type = $entity->getEntityTypeId();
Chris@0 1191 $count = db_select('example_entity_data')
Chris@0 1192 ->condition('type', $type)
Chris@0 1193 ->condition('id', $id)
Chris@0 1194 ->countQuery()
Chris@0 1195 ->execute()
Chris@0 1196 ->fetchField();
Chris@0 1197
Chris@0 1198 // Log the count in a table that records this statistic for deleted entities.
Chris@0 1199 db_merge('example_deleted_entity_statistics')
Chris@0 1200 ->key(['type' => $type, 'id' => $id])
Chris@0 1201 ->fields(['count' => $count])
Chris@0 1202 ->execute();
Chris@0 1203 }
Chris@0 1204
Chris@0 1205 /**
Chris@0 1206 * Respond to entity deletion.
Chris@0 1207 *
Chris@0 1208 * This hook runs once the entity has been deleted from the storage.
Chris@0 1209 *
Chris@0 1210 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1211 * The entity object for the entity that has been deleted.
Chris@0 1212 *
Chris@0 1213 * @ingroup entity_crud
Chris@0 1214 * @see hook_ENTITY_TYPE_delete()
Chris@0 1215 */
Chris@0 1216 function hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 1217 // Delete the entity's entry from a fictional table of all entities.
Chris@0 1218 db_delete('example_entity')
Chris@0 1219 ->condition('type', $entity->getEntityTypeId())
Chris@0 1220 ->condition('id', $entity->id())
Chris@0 1221 ->execute();
Chris@0 1222 }
Chris@0 1223
Chris@0 1224 /**
Chris@0 1225 * Respond to entity deletion of a particular type.
Chris@0 1226 *
Chris@0 1227 * This hook runs once the entity has been deleted from the storage.
Chris@0 1228 *
Chris@0 1229 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1230 * The entity object for the entity that has been deleted.
Chris@0 1231 *
Chris@0 1232 * @ingroup entity_crud
Chris@0 1233 * @see hook_entity_delete()
Chris@0 1234 */
Chris@0 1235 function hook_ENTITY_TYPE_delete(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 1236 // Delete the entity's entry from a fictional table of all entities.
Chris@0 1237 db_delete('example_entity')
Chris@0 1238 ->condition('type', $entity->getEntityTypeId())
Chris@0 1239 ->condition('id', $entity->id())
Chris@0 1240 ->execute();
Chris@0 1241 }
Chris@0 1242
Chris@0 1243 /**
Chris@0 1244 * Respond to entity revision deletion.
Chris@0 1245 *
Chris@0 1246 * This hook runs once the entity revision has been deleted from the storage.
Chris@0 1247 *
Chris@0 1248 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1249 * The entity object for the entity revision that has been deleted.
Chris@0 1250 *
Chris@0 1251 * @ingroup entity_crud
Chris@0 1252 * @see hook_ENTITY_TYPE_revision_delete()
Chris@0 1253 */
Chris@0 1254 function hook_entity_revision_delete(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 1255 $referenced_files_by_field = _editor_get_file_uuids_by_field($entity);
Chris@0 1256 foreach ($referenced_files_by_field as $field => $uuids) {
Chris@0 1257 _editor_delete_file_usage($uuids, $entity, 1);
Chris@0 1258 }
Chris@0 1259 }
Chris@0 1260
Chris@0 1261 /**
Chris@0 1262 * Respond to entity revision deletion of a particular type.
Chris@0 1263 *
Chris@0 1264 * This hook runs once the entity revision has been deleted from the storage.
Chris@0 1265 *
Chris@0 1266 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1267 * The entity object for the entity revision that has been deleted.
Chris@0 1268 *
Chris@0 1269 * @ingroup entity_crud
Chris@0 1270 * @see hook_entity_revision_delete()
Chris@0 1271 */
Chris@0 1272 function hook_ENTITY_TYPE_revision_delete(Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 1273 $referenced_files_by_field = _editor_get_file_uuids_by_field($entity);
Chris@0 1274 foreach ($referenced_files_by_field as $field => $uuids) {
Chris@0 1275 _editor_delete_file_usage($uuids, $entity, 1);
Chris@0 1276 }
Chris@0 1277 }
Chris@0 1278
Chris@0 1279 /**
Chris@0 1280 * Act on entities being assembled before rendering.
Chris@0 1281 *
Chris@0 1282 * @param &$build
Chris@0 1283 * A renderable array representing the entity content. The module may add
Chris@0 1284 * elements to $build prior to rendering. The structure of $build is a
Chris@0 1285 * renderable array as expected by drupal_render().
Chris@0 1286 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1287 * The entity object.
Chris@0 1288 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
Chris@0 1289 * The entity view display holding the display options configured for the
Chris@0 1290 * entity components.
Chris@0 1291 * @param $view_mode
Chris@0 1292 * The view mode the entity is rendered in.
Chris@0 1293 *
Chris@0 1294 * @see hook_entity_view_alter()
Chris@0 1295 * @see hook_ENTITY_TYPE_view()
Chris@0 1296 *
Chris@0 1297 * @ingroup entity_crud
Chris@0 1298 */
Chris@0 1299 function hook_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
Chris@0 1300 // Only do the extra work if the component is configured to be displayed.
Chris@0 1301 // This assumes a 'mymodule_addition' extra field has been defined for the
Chris@0 1302 // entity bundle in hook_entity_extra_field_info().
Chris@0 1303 if ($display->getComponent('mymodule_addition')) {
Chris@0 1304 $build['mymodule_addition'] = [
Chris@0 1305 '#markup' => mymodule_addition($entity),
Chris@0 1306 '#theme' => 'mymodule_my_additional_field',
Chris@0 1307 ];
Chris@0 1308 }
Chris@0 1309 }
Chris@0 1310
Chris@0 1311 /**
Chris@0 1312 * Act on entities of a particular type being assembled before rendering.
Chris@0 1313 *
Chris@0 1314 * @param &$build
Chris@0 1315 * A renderable array representing the entity content. The module may add
Chris@0 1316 * elements to $build prior to rendering. The structure of $build is a
Chris@0 1317 * renderable array as expected by drupal_render().
Chris@0 1318 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1319 * The entity object.
Chris@0 1320 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
Chris@0 1321 * The entity view display holding the display options configured for the
Chris@0 1322 * entity components.
Chris@0 1323 * @param $view_mode
Chris@0 1324 * The view mode the entity is rendered in.
Chris@0 1325 *
Chris@0 1326 * @see hook_ENTITY_TYPE_view_alter()
Chris@0 1327 * @see hook_entity_view()
Chris@0 1328 *
Chris@0 1329 * @ingroup entity_crud
Chris@0 1330 */
Chris@0 1331 function hook_ENTITY_TYPE_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
Chris@0 1332 // Only do the extra work if the component is configured to be displayed.
Chris@0 1333 // This assumes a 'mymodule_addition' extra field has been defined for the
Chris@0 1334 // entity bundle in hook_entity_extra_field_info().
Chris@0 1335 if ($display->getComponent('mymodule_addition')) {
Chris@0 1336 $build['mymodule_addition'] = [
Chris@0 1337 '#markup' => mymodule_addition($entity),
Chris@0 1338 '#theme' => 'mymodule_my_additional_field',
Chris@0 1339 ];
Chris@0 1340 }
Chris@0 1341 }
Chris@0 1342
Chris@0 1343 /**
Chris@0 1344 * Alter the results of the entity build array.
Chris@0 1345 *
Chris@0 1346 * This hook is called after the content has been assembled in a structured
Chris@0 1347 * array and may be used for doing processing which requires that the complete
Chris@0 1348 * entity content structure has been built.
Chris@0 1349 *
Chris@0 1350 * If a module wishes to act on the rendered HTML of the entity rather than the
Chris@0 1351 * structured content array, it may use this hook to add a #post_render
Chris@0 1352 * callback. Alternatively, it could also implement hook_preprocess_HOOK() for
Chris@0 1353 * the particular entity type template, if there is one (e.g., node.html.twig).
Chris@0 1354 *
Chris@0 1355 * See the @link themeable Default theme implementations topic @endlink and
Chris@0 1356 * drupal_render() for details.
Chris@0 1357 *
Chris@0 1358 * @param array &$build
Chris@0 1359 * A renderable array representing the entity content.
Chris@0 1360 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1361 * The entity object being rendered.
Chris@0 1362 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
Chris@0 1363 * The entity view display holding the display options configured for the
Chris@0 1364 * entity components.
Chris@0 1365 *
Chris@0 1366 * @ingroup entity_crud
Chris@0 1367 *
Chris@0 1368 * @see hook_entity_view()
Chris@0 1369 * @see hook_ENTITY_TYPE_view_alter()
Chris@0 1370 */
Chris@0 1371 function hook_entity_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
Chris@0 1372 if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
Chris@0 1373 // Change its weight.
Chris@0 1374 $build['an_additional_field']['#weight'] = -10;
Chris@0 1375
Chris@0 1376 // Add a #post_render callback to act on the rendered HTML of the entity.
Chris@0 1377 $build['#post_render'][] = 'my_module_node_post_render';
Chris@0 1378 }
Chris@0 1379 }
Chris@0 1380
Chris@0 1381 /**
Chris@0 1382 * Alter the results of the entity build array for a particular entity type.
Chris@0 1383 *
Chris@0 1384 * This hook is called after the content has been assembled in a structured
Chris@0 1385 * array and may be used for doing processing which requires that the complete
Chris@0 1386 * entity content structure has been built.
Chris@0 1387 *
Chris@0 1388 * If a module wishes to act on the rendered HTML of the entity rather than the
Chris@0 1389 * structured content array, it may use this hook to add a #post_render
Chris@0 1390 * callback. Alternatively, it could also implement hook_preprocess_HOOK() for
Chris@0 1391 * the particular entity type template, if there is one (e.g., node.html.twig).
Chris@0 1392 *
Chris@0 1393 * See the @link themeable Default theme implementations topic @endlink and
Chris@0 1394 * drupal_render() for details.
Chris@0 1395 *
Chris@0 1396 * @param array &$build
Chris@0 1397 * A renderable array representing the entity content.
Chris@0 1398 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1399 * The entity object being rendered.
Chris@0 1400 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
Chris@0 1401 * The entity view display holding the display options configured for the
Chris@0 1402 * entity components.
Chris@0 1403 *
Chris@0 1404 * @ingroup entity_crud
Chris@0 1405 *
Chris@0 1406 * @see hook_ENTITY_TYPE_view()
Chris@0 1407 * @see hook_entity_view_alter()
Chris@0 1408 */
Chris@0 1409 function hook_ENTITY_TYPE_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
Chris@0 1410 if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
Chris@0 1411 // Change its weight.
Chris@0 1412 $build['an_additional_field']['#weight'] = -10;
Chris@0 1413
Chris@0 1414 // Add a #post_render callback to act on the rendered HTML of the entity.
Chris@0 1415 $build['#post_render'][] = 'my_module_node_post_render';
Chris@0 1416 }
Chris@0 1417 }
Chris@0 1418
Chris@0 1419 /**
Chris@0 1420 * Act on entities as they are being prepared for view.
Chris@0 1421 *
Chris@0 1422 * Allows you to operate on multiple entities as they are being prepared for
Chris@0 1423 * view. Only use this if attaching the data during the entity loading phase
Chris@0 1424 * is not appropriate, for example when attaching other 'entity' style objects.
Chris@0 1425 *
Chris@0 1426 * @param string $entity_type_id
Chris@0 1427 * The type of entities being viewed (i.e. node, user, comment).
Chris@0 1428 * @param array $entities
Chris@0 1429 * The entities keyed by entity ID.
Chris@0 1430 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface[] $displays
Chris@0 1431 * The array of entity view displays holding the display options configured
Chris@0 1432 * for the entity components, keyed by bundle name.
Chris@0 1433 * @param string $view_mode
Chris@0 1434 * The view mode.
Chris@0 1435 *
Chris@0 1436 * @ingroup entity_crud
Chris@0 1437 */
Chris@0 1438 function hook_entity_prepare_view($entity_type_id, array $entities, array $displays, $view_mode) {
Chris@0 1439 // Load a specific node into the user object for later theming.
Chris@0 1440 if (!empty($entities) && $entity_type_id == 'user') {
Chris@0 1441 // Only do the extra work if the component is configured to be
Chris@0 1442 // displayed. This assumes a 'mymodule_addition' extra field has been
Chris@0 1443 // defined for the entity bundle in hook_entity_extra_field_info().
Chris@0 1444 $ids = [];
Chris@0 1445 foreach ($entities as $id => $entity) {
Chris@0 1446 if ($displays[$entity->bundle()]->getComponent('mymodule_addition')) {
Chris@0 1447 $ids[] = $id;
Chris@0 1448 }
Chris@0 1449 }
Chris@0 1450 if ($ids) {
Chris@0 1451 $nodes = mymodule_get_user_nodes($ids);
Chris@0 1452 foreach ($ids as $id) {
Chris@0 1453 $entities[$id]->user_node = $nodes[$id];
Chris@0 1454 }
Chris@0 1455 }
Chris@0 1456 }
Chris@0 1457 }
Chris@0 1458
Chris@0 1459 /**
Chris@0 1460 * Change the view mode of an entity that is being displayed.
Chris@0 1461 *
Chris@0 1462 * @param string $view_mode
Chris@0 1463 * The view_mode that is to be used to display the entity.
Chris@0 1464 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1465 * The entity that is being viewed.
Chris@0 1466 * @param array $context
Chris@0 1467 * Array with additional context information, currently only contains the
Chris@0 1468 * langcode the entity is viewed in.
Chris@0 1469 *
Chris@0 1470 * @ingroup entity_crud
Chris@0 1471 */
Chris@0 1472 function hook_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) {
Chris@0 1473 // For nodes, change the view mode when it is teaser.
Chris@0 1474 if ($entity->getEntityTypeId() == 'node' && $view_mode == 'teaser') {
Chris@0 1475 $view_mode = 'my_custom_view_mode';
Chris@0 1476 }
Chris@0 1477 }
Chris@0 1478
Chris@0 1479 /**
Chris@0 1480 * Alter entity renderable values before cache checking in drupal_render().
Chris@0 1481 *
Chris@0 1482 * Invoked for a specific entity type.
Chris@0 1483 *
Chris@0 1484 * The values in the #cache key of the renderable array are used to determine if
Chris@0 1485 * a cache entry exists for the entity's rendered output. Ideally only values
Chris@0 1486 * that pertain to caching should be altered in this hook.
Chris@0 1487 *
Chris@0 1488 * @param array &$build
Chris@0 1489 * A renderable array containing the entity's caching and view mode values.
Chris@0 1490 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1491 * The entity that is being viewed.
Chris@0 1492 * @param string $view_mode
Chris@0 1493 * The view_mode that is to be used to display the entity.
Chris@0 1494 *
Chris@0 1495 * @see drupal_render()
Chris@0 1496 * @see \Drupal\Core\Entity\EntityViewBuilder
Chris@0 1497 * @see hook_entity_build_defaults_alter()
Chris@0 1498 *
Chris@0 1499 * @ingroup entity_crud
Chris@0 1500 */
Chris@0 1501 function hook_ENTITY_TYPE_build_defaults_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, $view_mode) {
Chris@0 1502
Chris@0 1503 }
Chris@0 1504
Chris@0 1505 /**
Chris@0 1506 * Alter entity renderable values before cache checking in drupal_render().
Chris@0 1507 *
Chris@0 1508 * The values in the #cache key of the renderable array are used to determine if
Chris@0 1509 * a cache entry exists for the entity's rendered output. Ideally only values
Chris@0 1510 * that pertain to caching should be altered in this hook.
Chris@0 1511 *
Chris@0 1512 * @param array &$build
Chris@0 1513 * A renderable array containing the entity's caching and view mode values.
Chris@0 1514 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1515 * The entity that is being viewed.
Chris@0 1516 * @param string $view_mode
Chris@0 1517 * The view_mode that is to be used to display the entity.
Chris@0 1518 *
Chris@0 1519 * @see drupal_render()
Chris@0 1520 * @see \Drupal\Core\Entity\EntityViewBuilder
Chris@0 1521 * @see hook_ENTITY_TYPE_build_defaults_alter()
Chris@0 1522 *
Chris@0 1523 * @ingroup entity_crud
Chris@0 1524 */
Chris@0 1525 function hook_entity_build_defaults_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, $view_mode) {
Chris@0 1526
Chris@0 1527 }
Chris@0 1528
Chris@0 1529 /**
Chris@0 1530 * Alter the settings used for displaying an entity.
Chris@0 1531 *
Chris@0 1532 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
Chris@0 1533 * The entity view display that will be used to display the entity
Chris@0 1534 * components.
Chris@0 1535 * @param array $context
Chris@0 1536 * An associative array containing:
Chris@0 1537 * - entity_type: The entity type, e.g., 'node' or 'user'.
Chris@0 1538 * - bundle: The bundle, e.g., 'page' or 'article'.
Chris@0 1539 * - view_mode: The view mode, e.g., 'full', 'teaser', etc.
Chris@0 1540 *
Chris@0 1541 * @ingroup entity_crud
Chris@0 1542 */
Chris@0 1543 function hook_entity_view_display_alter(\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, array $context) {
Chris@0 1544 // Leave field labels out of the search index.
Chris@0 1545 if ($context['entity_type'] == 'node' && $context['view_mode'] == 'search_index') {
Chris@0 1546 foreach ($display->getComponents() as $name => $options) {
Chris@0 1547 if (isset($options['label'])) {
Chris@0 1548 $options['label'] = 'hidden';
Chris@0 1549 $display->setComponent($name, $options);
Chris@0 1550 }
Chris@0 1551 }
Chris@0 1552 }
Chris@0 1553 }
Chris@0 1554
Chris@0 1555 /**
Chris@0 1556 * Alter the render array generated by an EntityDisplay for an entity.
Chris@0 1557 *
Chris@0 1558 * @param array $build
Chris@0 1559 * The renderable array generated by the EntityDisplay.
Chris@0 1560 * @param array $context
Chris@0 1561 * An associative array containing:
Chris@0 1562 * - entity: The entity being rendered.
Chris@0 1563 * - view_mode: The view mode; for example, 'full' or 'teaser'.
Chris@0 1564 * - display: The EntityDisplay holding the display options.
Chris@0 1565 *
Chris@0 1566 * @ingroup entity_crud
Chris@0 1567 */
Chris@0 1568 function hook_entity_display_build_alter(&$build, $context) {
Chris@0 1569 // Append RDF term mappings on displayed taxonomy links.
Chris@0 1570 foreach (Element::children($build) as $field_name) {
Chris@0 1571 $element = &$build[$field_name];
Chris@0 1572 if ($element['#field_type'] == 'entity_reference' && $element['#formatter'] == 'entity_reference_label') {
Chris@0 1573 foreach ($element['#items'] as $delta => $item) {
Chris@0 1574 $term = $item->entity;
Chris@0 1575 if (!empty($term->rdf_mapping['rdftype'])) {
Chris@0 1576 $element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype'];
Chris@0 1577 }
Chris@0 1578 if (!empty($term->rdf_mapping['name']['predicates'])) {
Chris@0 1579 $element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates'];
Chris@0 1580 }
Chris@0 1581 }
Chris@0 1582 }
Chris@0 1583 }
Chris@0 1584 }
Chris@0 1585
Chris@0 1586 /**
Chris@0 1587 * Acts on an entity object about to be shown on an entity form.
Chris@0 1588 *
Chris@0 1589 * This can be typically used to pre-fill entity values or change the form state
Chris@0 1590 * before the entity form is built. It is invoked just once when first building
Chris@0 1591 * the entity form. Rebuilds will not trigger a new invocation.
Chris@0 1592 *
Chris@0 1593 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1594 * The entity that is about to be shown on the form.
Chris@0 1595 * @param $operation
Chris@0 1596 * The current operation.
Chris@0 1597 * @param \Drupal\Core\Form\FormStateInterface $form_state
Chris@0 1598 * The current state of the form.
Chris@0 1599 *
Chris@0 1600 * @see \Drupal\Core\Entity\EntityForm::prepareEntity()
Chris@0 1601 * @see hook_ENTITY_TYPE_prepare_form()
Chris@0 1602 *
Chris@0 1603 * @ingroup entity_crud
Chris@0 1604 */
Chris@0 1605 function hook_entity_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Form\FormStateInterface $form_state) {
Chris@0 1606 if ($operation == 'edit') {
Chris@0 1607 $entity->label->value = 'Altered label';
Chris@0 1608 $form_state->set('label_altered', TRUE);
Chris@0 1609 }
Chris@0 1610 }
Chris@0 1611
Chris@0 1612 /**
Chris@0 1613 * Acts on a particular type of entity object about to be in an entity form.
Chris@0 1614 *
Chris@0 1615 * This can be typically used to pre-fill entity values or change the form state
Chris@0 1616 * before the entity form is built. It is invoked just once when first building
Chris@0 1617 * the entity form. Rebuilds will not trigger a new invocation.
Chris@0 1618 *
Chris@0 1619 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1620 * The entity that is about to be shown on the form.
Chris@0 1621 * @param $operation
Chris@0 1622 * The current operation.
Chris@0 1623 * @param \Drupal\Core\Form\FormStateInterface $form_state
Chris@0 1624 * The current state of the form.
Chris@0 1625 *
Chris@0 1626 * @see \Drupal\Core\Entity\EntityForm::prepareEntity()
Chris@0 1627 * @see hook_entity_prepare_form()
Chris@0 1628 *
Chris@0 1629 * @ingroup entity_crud
Chris@0 1630 */
Chris@0 1631 function hook_ENTITY_TYPE_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Form\FormStateInterface $form_state) {
Chris@0 1632 if ($operation == 'edit') {
Chris@0 1633 $entity->label->value = 'Altered label';
Chris@0 1634 $form_state->set('label_altered', TRUE);
Chris@0 1635 }
Chris@0 1636 }
Chris@0 1637
Chris@0 1638 /**
Chris@0 1639 * Alter the settings used for displaying an entity form.
Chris@0 1640 *
Chris@0 1641 * @param \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display
Chris@0 1642 * The entity_form_display object that will be used to display the entity form
Chris@0 1643 * components.
Chris@0 1644 * @param array $context
Chris@0 1645 * An associative array containing:
Chris@0 1646 * - entity_type: The entity type, e.g., 'node' or 'user'.
Chris@0 1647 * - bundle: The bundle, e.g., 'page' or 'article'.
Chris@0 1648 * - form_mode: The form mode; e.g., 'default', 'profile', 'register', etc.
Chris@0 1649 *
Chris@0 1650 * @ingroup entity_crud
Chris@0 1651 */
Chris@0 1652 function hook_entity_form_display_alter(\Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display, array $context) {
Chris@0 1653 // Hide the 'user_picture' field from the register form.
Chris@0 1654 if ($context['entity_type'] == 'user' && $context['form_mode'] == 'register') {
Chris@0 1655 $form_display->setComponent('user_picture', [
Chris@0 1656 'region' => 'hidden',
Chris@0 1657 ]);
Chris@0 1658 }
Chris@0 1659 }
Chris@0 1660
Chris@0 1661 /**
Chris@0 1662 * Provides custom base field definitions for a content entity type.
Chris@0 1663 *
Chris@0 1664 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 1665 * The entity type definition.
Chris@0 1666 *
Chris@0 1667 * @return \Drupal\Core\Field\FieldDefinitionInterface[]
Chris@0 1668 * An array of field definitions, keyed by field name.
Chris@0 1669 *
Chris@0 1670 * @see hook_entity_base_field_info_alter()
Chris@0 1671 * @see hook_entity_bundle_field_info()
Chris@0 1672 * @see hook_entity_bundle_field_info_alter()
Chris@0 1673 * @see \Drupal\Core\Field\FieldDefinitionInterface
Chris@0 1674 * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
Chris@0 1675 */
Chris@0 1676 function hook_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
Chris@0 1677 if ($entity_type->id() == 'node') {
Chris@0 1678 $fields = [];
Chris@0 1679 $fields['mymodule_text'] = BaseFieldDefinition::create('string')
Chris@0 1680 ->setLabel(t('The text'))
Chris@0 1681 ->setDescription(t('A text property added by mymodule.'))
Chris@0 1682 ->setComputed(TRUE)
Chris@0 1683 ->setClass('\Drupal\mymodule\EntityComputedText');
Chris@0 1684
Chris@0 1685 return $fields;
Chris@0 1686 }
Chris@0 1687 }
Chris@0 1688
Chris@0 1689 /**
Chris@0 1690 * Alter base field definitions for a content entity type.
Chris@0 1691 *
Chris@0 1692 * @param \Drupal\Core\Field\FieldDefinitionInterface[] $fields
Chris@0 1693 * The array of base field definitions for the entity type.
Chris@0 1694 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 1695 * The entity type definition.
Chris@0 1696 *
Chris@0 1697 * @see hook_entity_base_field_info()
Chris@0 1698 * @see hook_entity_bundle_field_info()
Chris@0 1699 * @see hook_entity_bundle_field_info_alter()
Chris@0 1700 *
Chris@0 1701 * @todo WARNING: This hook will be changed in
Chris@0 1702 * https://www.drupal.org/node/2346329.
Chris@0 1703 */
Chris@0 1704 function hook_entity_base_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type) {
Chris@0 1705 // Alter the mymodule_text field to use a custom class.
Chris@0 1706 if ($entity_type->id() == 'node' && !empty($fields['mymodule_text'])) {
Chris@0 1707 $fields['mymodule_text']->setClass('\Drupal\anothermodule\EntityComputedText');
Chris@0 1708 }
Chris@0 1709 }
Chris@0 1710
Chris@0 1711 /**
Chris@0 1712 * Provides field definitions for a specific bundle within an entity type.
Chris@0 1713 *
Chris@0 1714 * Bundle fields either have to override an existing base field, or need to
Chris@0 1715 * provide a field storage definition via hook_entity_field_storage_info()
Chris@0 1716 * unless they are computed.
Chris@0 1717 *
Chris@0 1718 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 1719 * The entity type definition.
Chris@0 1720 * @param string $bundle
Chris@0 1721 * The bundle.
Chris@0 1722 * @param \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions
Chris@0 1723 * The list of base field definitions for the entity type.
Chris@0 1724 *
Chris@0 1725 * @return \Drupal\Core\Field\FieldDefinitionInterface[]
Chris@0 1726 * An array of bundle field definitions, keyed by field name.
Chris@0 1727 *
Chris@0 1728 * @see hook_entity_base_field_info()
Chris@0 1729 * @see hook_entity_base_field_info_alter()
Chris@0 1730 * @see hook_entity_field_storage_info()
Chris@0 1731 * @see hook_entity_field_storage_info_alter()
Chris@0 1732 * @see hook_entity_bundle_field_info_alter()
Chris@0 1733 * @see \Drupal\Core\Field\FieldDefinitionInterface
Chris@0 1734 * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
Chris@0 1735 *
Chris@0 1736 * @todo WARNING: This hook will be changed in
Chris@0 1737 * https://www.drupal.org/node/2346347.
Chris@0 1738 */
Chris@0 1739 function hook_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
Chris@0 1740 // Add a property only to nodes of the 'article' bundle.
Chris@0 1741 if ($entity_type->id() == 'node' && $bundle == 'article') {
Chris@0 1742 $fields = [];
Chris@0 1743 $fields['mymodule_text_more'] = BaseFieldDefinition::create('string')
Chris@0 1744 ->setLabel(t('More text'))
Chris@0 1745 ->setComputed(TRUE)
Chris@0 1746 ->setClass('\Drupal\mymodule\EntityComputedMoreText');
Chris@0 1747 return $fields;
Chris@0 1748 }
Chris@0 1749 }
Chris@0 1750
Chris@0 1751 /**
Chris@0 1752 * Alter bundle field definitions.
Chris@0 1753 *
Chris@0 1754 * @param \Drupal\Core\Field\FieldDefinitionInterface[] $fields
Chris@0 1755 * The array of bundle field definitions.
Chris@0 1756 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 1757 * The entity type definition.
Chris@0 1758 * @param string $bundle
Chris@0 1759 * The bundle.
Chris@0 1760 *
Chris@0 1761 * @see hook_entity_base_field_info()
Chris@0 1762 * @see hook_entity_base_field_info_alter()
Chris@0 1763 * @see hook_entity_bundle_field_info()
Chris@0 1764 *
Chris@0 1765 * @todo WARNING: This hook will be changed in
Chris@0 1766 * https://www.drupal.org/node/2346347.
Chris@0 1767 */
Chris@0 1768 function hook_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
Chris@0 1769 if ($entity_type->id() == 'node' && $bundle == 'article' && !empty($fields['mymodule_text'])) {
Chris@0 1770 // Alter the mymodule_text field to use a custom class.
Chris@0 1771 $fields['mymodule_text']->setClass('\Drupal\anothermodule\EntityComputedText');
Chris@0 1772 }
Chris@0 1773 }
Chris@0 1774
Chris@0 1775 /**
Chris@0 1776 * Provides field storage definitions for a content entity type.
Chris@0 1777 *
Chris@0 1778 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 1779 * The entity type definition.
Chris@0 1780 *
Chris@0 1781 * @return \Drupal\Core\Field\FieldStorageDefinitionInterface[]
Chris@0 1782 * An array of field storage definitions, keyed by field name.
Chris@0 1783 *
Chris@0 1784 * @see hook_entity_field_storage_info_alter()
Chris@0 1785 * @see \Drupal\Core\Field\FieldStorageDefinitionInterface
Chris@0 1786 * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldStorageDefinitions()
Chris@0 1787 */
Chris@0 1788 function hook_entity_field_storage_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
Chris@0 1789 if (\Drupal::entityManager()->getStorage($entity_type->id()) instanceof DynamicallyFieldableEntityStorageInterface) {
Chris@0 1790 // Query by filtering on the ID as this is more efficient than filtering
Chris@0 1791 // on the entity_type property directly.
Chris@0 1792 $ids = \Drupal::entityQuery('field_storage_config')
Chris@0 1793 ->condition('id', $entity_type->id() . '.', 'STARTS_WITH')
Chris@0 1794 ->execute();
Chris@0 1795 // Fetch all fields and key them by field name.
Chris@0 1796 $field_storages = FieldStorageConfig::loadMultiple($ids);
Chris@0 1797 $result = [];
Chris@0 1798 foreach ($field_storages as $field_storage) {
Chris@0 1799 $result[$field_storage->getName()] = $field_storage;
Chris@0 1800 }
Chris@0 1801
Chris@0 1802 return $result;
Chris@0 1803 }
Chris@0 1804 }
Chris@0 1805
Chris@0 1806 /**
Chris@0 1807 * Alter field storage definitions for a content entity type.
Chris@0 1808 *
Chris@0 1809 * @param \Drupal\Core\Field\FieldStorageDefinitionInterface[] $fields
Chris@0 1810 * The array of field storage definitions for the entity type.
Chris@0 1811 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 1812 * The entity type definition.
Chris@0 1813 *
Chris@0 1814 * @see hook_entity_field_storage_info()
Chris@0 1815 */
Chris@0 1816 function hook_entity_field_storage_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type) {
Chris@0 1817 // Alter the max_length setting.
Chris@0 1818 if ($entity_type->id() == 'node' && !empty($fields['mymodule_text'])) {
Chris@0 1819 $fields['mymodule_text']->setSetting('max_length', 128);
Chris@0 1820 }
Chris@0 1821 }
Chris@0 1822
Chris@0 1823 /**
Chris@0 1824 * Declares entity operations.
Chris@0 1825 *
Chris@0 1826 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1827 * The entity on which the linked operations will be performed.
Chris@0 1828 *
Chris@0 1829 * @return array
Chris@0 1830 * An operations array as returned by
Chris@0 1831 * EntityListBuilderInterface::getOperations().
Chris@0 1832 *
Chris@0 1833 * @see \Drupal\Core\Entity\EntityListBuilderInterface::getOperations()
Chris@0 1834 */
Chris@0 1835 function hook_entity_operation(\Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 1836 $operations = [];
Chris@0 1837 $operations['translate'] = [
Chris@0 1838 'title' => t('Translate'),
Chris@0 1839 'url' => \Drupal\Core\Url::fromRoute('foo_module.entity.translate'),
Chris@0 1840 'weight' => 50,
Chris@0 1841 ];
Chris@0 1842
Chris@0 1843 return $operations;
Chris@0 1844 }
Chris@0 1845
Chris@0 1846 /**
Chris@0 1847 * Alter entity operations.
Chris@0 1848 *
Chris@0 1849 * @param array $operations
Chris@0 1850 * Operations array as returned by
Chris@0 1851 * \Drupal\Core\Entity\EntityListBuilderInterface::getOperations().
Chris@0 1852 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 1853 * The entity on which the linked operations will be performed.
Chris@0 1854 */
Chris@0 1855 function hook_entity_operation_alter(array &$operations, \Drupal\Core\Entity\EntityInterface $entity) {
Chris@0 1856 // Alter the title and weight.
Chris@0 1857 $operations['translate']['title'] = t('Translate @entity_type', [
Chris@0 1858 '@entity_type' => $entity->getEntityTypeId(),
Chris@0 1859 ]);
Chris@0 1860 $operations['translate']['weight'] = 99;
Chris@0 1861 }
Chris@0 1862
Chris@0 1863 /**
Chris@0 1864 * Control access to fields.
Chris@0 1865 *
Chris@0 1866 * This hook is invoked from
Chris@0 1867 * \Drupal\Core\Entity\EntityAccessControlHandler::fieldAccess() to let modules
Chris@0 1868 * grant or deny operations on fields.
Chris@0 1869 *
Chris@0 1870 * @param string $operation
Chris@0 1871 * The operation to be performed. See
Chris@0 1872 * \Drupal\Core\Entity\EntityAccessControlHandlerInterface::fieldAccess()
Chris@0 1873 * for possible values.
Chris@0 1874 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
Chris@0 1875 * The field definition.
Chris@0 1876 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 1877 * The user account to check.
Chris@0 1878 * @param \Drupal\Core\Field\FieldItemListInterface $items
Chris@0 1879 * (optional) The entity field object on which the operation is to be
Chris@0 1880 * performed.
Chris@0 1881 *
Chris@0 1882 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 1883 * The access result.
Chris@0 1884 *
Chris@0 1885 * @see \Drupal\Core\Entity\EntityAccessControlHandlerInterface::fieldAccess()
Chris@0 1886 */
Chris@0 1887 function hook_entity_field_access($operation, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, \Drupal\Core\Session\AccountInterface $account, \Drupal\Core\Field\FieldItemListInterface $items = NULL) {
Chris@0 1888 if ($field_definition->getName() == 'field_of_interest' && $operation == 'edit') {
Chris@0 1889 return AccessResult::allowedIfHasPermission($account, 'update field of interest');
Chris@0 1890 }
Chris@0 1891 return AccessResult::neutral();
Chris@0 1892 }
Chris@0 1893
Chris@0 1894 /**
Chris@0 1895 * Alter the default access behavior for a given field.
Chris@0 1896 *
Chris@0 1897 * Use this hook to override access grants from another module. Note that the
Chris@0 1898 * original default access flag is masked under the ':default' key.
Chris@0 1899 *
Chris@0 1900 * @param \Drupal\Core\Access\AccessResultInterface[] $grants
Chris@0 1901 * An array of grants gathered by hook_entity_field_access(). The array is
Chris@0 1902 * keyed by the module that defines the field's access control; the values are
Chris@0 1903 * grant responses for each module (\Drupal\Core\Access\AccessResult).
Chris@0 1904 * @param array $context
Chris@0 1905 * Context array on the performed operation with the following keys:
Chris@0 1906 * - operation: The operation to be performed (string).
Chris@0 1907 * - field_definition: The field definition object
Chris@0 1908 * (\Drupal\Core\Field\FieldDefinitionInterface)
Chris@0 1909 * - account: The user account to check access for
Chris@0 1910 * (Drupal\user\Entity\User).
Chris@0 1911 * - items: (optional) The entity field items
Chris@0 1912 * (\Drupal\Core\Field\FieldItemListInterface).
Chris@0 1913 */
Chris@0 1914 function hook_entity_field_access_alter(array &$grants, array $context) {
Chris@0 1915 /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
Chris@0 1916 $field_definition = $context['field_definition'];
Chris@0 1917 if ($field_definition->getName() == 'field_of_interest' && $grants['node']->isForbidden()) {
Chris@0 1918 // Override node module's restriction to no opinion (neither allowed nor
Chris@0 1919 // forbidden). We don't want to provide our own access hook, we only want to
Chris@0 1920 // take out node module's part in the access handling of this field. We also
Chris@0 1921 // don't want to switch node module's grant to
Chris@0 1922 // AccessResultInterface::isAllowed() , because the grants of other modules
Chris@0 1923 // should still decide on their own if this field is accessible or not
Chris@0 1924 $grants['node'] = AccessResult::neutral()->inheritCacheability($grants['node']);
Chris@0 1925 }
Chris@0 1926 }
Chris@0 1927
Chris@0 1928 /**
Chris@0 1929 * Acts when initializing a fieldable entity object.
Chris@0 1930 *
Chris@0 1931 * This hook runs after a new entity object or a new entity translation object
Chris@0 1932 * has just been instantiated. It can be used to set initial values, e.g. to
Chris@0 1933 * provide defaults.
Chris@0 1934 *
Chris@0 1935 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
Chris@0 1936 * The entity object.
Chris@0 1937 *
Chris@0 1938 * @ingroup entity_crud
Chris@0 1939 * @see hook_ENTITY_TYPE_field_values_init()
Chris@0 1940 */
Chris@0 1941 function hook_entity_field_values_init(\Drupal\Core\Entity\FieldableEntityInterface $entity) {
Chris@0 1942 if ($entity instanceof \Drupal\Core\Entity\ContentEntityInterface && !$entity->foo->value) {
Chris@0 1943 $entity->foo->value = 'some_initial_value';
Chris@0 1944 }
Chris@0 1945 }
Chris@0 1946
Chris@0 1947 /**
Chris@0 1948 * Acts when initializing a fieldable entity object.
Chris@0 1949 *
Chris@0 1950 * This hook runs after a new entity object or a new entity translation object
Chris@0 1951 * has just been instantiated. It can be used to set initial values, e.g. to
Chris@0 1952 * provide defaults.
Chris@0 1953 *
Chris@0 1954 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
Chris@0 1955 * The entity object.
Chris@0 1956 *
Chris@0 1957 * @ingroup entity_crud
Chris@0 1958 * @see hook_entity_field_values_init()
Chris@0 1959 */
Chris@0 1960 function hook_ENTITY_TYPE_field_values_init(\Drupal\Core\Entity\FieldableEntityInterface $entity) {
Chris@0 1961 if (!$entity->foo->value) {
Chris@0 1962 $entity->foo->value = 'some_initial_value';
Chris@0 1963 }
Chris@0 1964 }
Chris@0 1965
Chris@0 1966 /**
Chris@0 1967 * Exposes "pseudo-field" components on content entities.
Chris@0 1968 *
Chris@0 1969 * Field UI's "Manage fields" and "Manage display" pages let users re-order
Chris@0 1970 * fields, but also non-field components. For nodes, these include elements
Chris@0 1971 * exposed by modules through hook_form_alter(), for instance.
Chris@0 1972 *
Chris@0 1973 * Content entities or modules that want to have their components supported
Chris@0 1974 * should expose them using this hook. The user-defined settings (weight,
Chris@0 1975 * visible) are automatically applied when entities or entity forms are
Chris@0 1976 * rendered.
Chris@0 1977 *
Chris@0 1978 * @see hook_entity_extra_field_info_alter()
Chris@0 1979 *
Chris@0 1980 * @return array
Chris@0 1981 * The array structure is identical to that of the return value of
Chris@0 1982 * \Drupal\Core\Entity\EntityFieldManagerInterface::getExtraFields().
Chris@0 1983 */
Chris@0 1984 function hook_entity_extra_field_info() {
Chris@0 1985 $extra = [];
Chris@0 1986 $module_language_enabled = \Drupal::moduleHandler()->moduleExists('language');
Chris@0 1987 $description = t('Node module element');
Chris@0 1988
Chris@0 1989 foreach (NodeType::loadMultiple() as $bundle) {
Chris@0 1990
Chris@0 1991 // Add also the 'language' select if Language module is enabled and the
Chris@0 1992 // bundle has multilingual support.
Chris@0 1993 // Visibility of the ordering of the language selector is the same as on the
Chris@0 1994 // node/add form.
Chris@0 1995 if ($module_language_enabled) {
Chris@0 1996 $configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', $bundle->id());
Chris@0 1997 if ($configuration->isLanguageAlterable()) {
Chris@0 1998 $extra['node'][$bundle->id()]['form']['language'] = [
Chris@0 1999 'label' => t('Language'),
Chris@0 2000 'description' => $description,
Chris@0 2001 'weight' => 0,
Chris@0 2002 ];
Chris@0 2003 }
Chris@0 2004 }
Chris@0 2005 $extra['node'][$bundle->id()]['display']['language'] = [
Chris@0 2006 'label' => t('Language'),
Chris@0 2007 'description' => $description,
Chris@0 2008 'weight' => 0,
Chris@0 2009 'visible' => FALSE,
Chris@0 2010 ];
Chris@0 2011 }
Chris@0 2012
Chris@0 2013 return $extra;
Chris@0 2014 }
Chris@0 2015
Chris@0 2016 /**
Chris@0 2017 * Alter "pseudo-field" components on content entities.
Chris@0 2018 *
Chris@0 2019 * @param array $info
Chris@0 2020 * The array structure is identical to that of the return value of
Chris@0 2021 * \Drupal\Core\Entity\EntityManagerInterface::getExtraFields().
Chris@0 2022 *
Chris@0 2023 * @see hook_entity_extra_field_info()
Chris@0 2024 */
Chris@0 2025 function hook_entity_extra_field_info_alter(&$info) {
Chris@0 2026 // Force node title to always be at the top of the list by default.
Chris@0 2027 foreach (NodeType::loadMultiple() as $bundle) {
Chris@0 2028 if (isset($info['node'][$bundle->id()]['form']['title'])) {
Chris@0 2029 $info['node'][$bundle->id()]['form']['title']['weight'] = -20;
Chris@0 2030 }
Chris@0 2031 }
Chris@0 2032 }
Chris@0 2033
Chris@0 2034 /**
Chris@0 2035 * @} End of "addtogroup hooks".
Chris@0 2036 */