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