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