comparison core/lib/Drupal/Core/Entity/entity.api.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children c2387f117808
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
66 * information on how to instantiate the correct view builder class for 66 * information on how to instantiate the correct view builder class for
67 * an entity type. 67 * an entity type.
68 * - During many operations, static methods are called on the entity class, 68 * - During many operations, static methods are called on the entity class,
69 * which implements \Drupal\Entity\EntityInterface. 69 * which implements \Drupal\Entity\EntityInterface.
70 * 70 *
71 * @section entities_revisions_translations Entities, revisions and translations
72 * A content entity can have multiple stored variants: based on its definition,
73 * it can be revisionable, translatable, or both.
74 *
75 * A revisionable entity can keep track of the changes that affect its data. In
76 * fact all previous revisions of the entity can be stored and made available as
77 * "historical" information. The "default" revision is the canonical variant of
78 * the entity, the one that is loaded when no specific revision is requested.
79 * Only changes to the default revision may be performed without triggering the
80 * creation of a new revision, in any other case revision data is not supposed
81 * to change. Aside from historical revisions, there can be "pending" revisions,
82 * that contain changes that did not make their way into the default revision.
83 * Typically these revisions contain data that is waiting for some form of
84 * approval, before being accepted as canonical.
85 * @see \Drupal\Core\Entity\RevisionableInterface
86 * @see \Drupal\Core\Entity\RevisionableStorageInterface
87 *
88 * A translatable entity can contain multiple translations of the same content.
89 * Content entity data is stored via fields, and each field can have one version
90 * for each enabled language. Some fields may be defined as untranslatable,
91 * which means that their values are shared among all translations. The
92 * "default" translation is the canonical variant of the entity, the one whose
93 * content will be accessible in the entity field data. Other translations
94 * can be instantiated from the default one. Every translation has an "active
95 * language" that is used to determine which field translation values should be
96 * handled. Typically the default translation's active language is the language
97 * of the content that was originally entered and served as source for the other
98 * translations.
99 * @see \Drupal\Core\Entity\TranslatableInterface
100 * @see \Drupal\Core\Entity\TranslatableStorageInterface
101 *
102 * An entity that is both revisionable and translatable has all the features
103 * described above: every revision can contain one or more translations. The
104 * canonical variant of the entity is the default translation of the default
105 * revision. Any revision will be initially loaded as the default translation,
106 * the other revision translations can be instantiated from this one. If a
107 * translation has changes in a certain revision, the translation is considered
108 * "affected" by that revision, and will be flagged as such via the
109 * "revision_translation_affected" field. With the built-in UI, every time a new
110 * revision is saved, the changes for the edited translations will be stored,
111 * while all field values for the other translations will be copied as-is.
112 * However, if multiple translations of the default revision are being
113 * subsequently modified without creating a new revision when saving, they will
114 * all be affected by the default revision. Additionally, all revision
115 * translations will be affected when saving a revision containing changes for
116 * untranslatable fields. On the other hand, pending revisions are not supposed
117 * to contain multiple affected translations, even when they are being
118 * manipulated via the API.
119 * @see \Drupal\Core\Entity\TranslatableRevisionableInterface
120 * @see \Drupal\Core\Entity\TranslatableRevisionableStorageInterface
121 *
71 * @section create Create operations 122 * @section create Create operations
72 * To create an entity: 123 * To create an entity:
73 * @code 124 * @code
74 * $entity = $storage->create(); 125 * $entity = $storage->create();
75 * 126 *
82 * with an array of provided property values: \Drupal\Core\Entity::create(). 133 * with an array of provided property values: \Drupal\Core\Entity::create().
83 * 134 *
84 * Hooks invoked during the create operation: 135 * Hooks invoked during the create operation:
85 * - hook_ENTITY_TYPE_create() 136 * - hook_ENTITY_TYPE_create()
86 * - hook_entity_create() 137 * - hook_entity_create()
138 * - When handling content entities, if a new translation is added to the entity
139 * object:
140 * - hook_ENTITY_TYPE_translation_create()
141 * - hook_entity_translation_create()
87 * 142 *
88 * See @ref save below for the save portion of the operation. 143 * See @ref save below for the save portion of the operation.
89 * 144 *
90 * @section load Read/Load operations 145 * @section load Read/Load operations
91 * To load (read) a single entity: 146 * To load (read) a single entity:
111 * @code 166 * @code
112 * $entity = $storage->loadRevision($revision_id); 167 * $entity = $storage->loadRevision($revision_id);
113 * @endcode 168 * @endcode
114 * This involves the same hooks and operations as regular entity loading. 169 * This involves the same hooks and operations as regular entity loading.
115 * 170 *
116 * @section entities_revisions_translations Entities, revisions and translations 171 * The "latest revision" of an entity is the most recently created one,
117 * 172 * regardless of it being default or pending. If the entity is translatable,
118 * A translation is not a revision and a revision is not necessarily a 173 * revision translations are not taken into account either. In other words, any
119 * translation. Revisions and translations are the two axes on the "spreadsheet" 174 * time a new revision is created, that becomes the latest revision for the
120 * of an entity. If you use the built-in UI and have revisions enabled, then a 175 * entity overall, regardless of the affected translations. To load the latest
121 * new translation change would create a new revision (with a copy of all data 176 * revision of an entity:
122 * for other languages in that revision). If an entity does not use revisions or 177 * @code
123 * the entity is being modified via the API, then multiple translations can be 178 * $revision_id = $storage->getLatestRevisionId($entity_id);
124 * modified in a single revision. Conceptually, the revisions are columns on the 179 * $entity = $storage->loadRevision($revision_id);
125 * spreadsheet and translations are rows. 180 * @endcode
181 * As usual, if the entity is translatable, this code instantiates into $entity
182 * the default translation of the revision, even if the latest revision contains
183 * only changes to a different translation:
184 * @code
185 * $is_default = $entity->isDefaultTranslation(); // returns TRUE
186 * @endcode
187 *
188 * The "latest translation-affected revision" is the most recently created one
189 * that affects the specified translation. For example, when a new revision
190 * introducing some changes to an English translation is saved, that becomes the
191 * new "latest revision". However, if an existing Italian translation was not
192 * affected by those changes, then the "latest translation-affected revision"
193 * for Italian remains what it was. To load the Italian translation at its
194 * latest translation-affected revision:
195 * @code
196 * $revision_id = $storage->getLatestTranslationAffectedRevisionId($entity_id, 'it');
197 * $it_translation = $storage->loadRevision($revision_id)->getTranslation('it');
198 * @endcode
126 * 199 *
127 * @section save Save operations 200 * @section save Save operations
128 * To update an existing entity, you will need to load it, change properties, 201 * To update an existing entity, you will need to load it, change properties,
129 * and then save; as described above, when creating a new entity, you will also 202 * and then save; as described above, when creating a new entity, you will also
130 * need to save it. Here is the order of hooks and other events that happen 203 * need to save it. Here is the order of hooks and other events that happen
151 * hook_node_access_records_alter() 224 * hook_node_access_records_alter()
152 * - Config entities that are acting as entity bundles in postSave(): 225 * - Config entities that are acting as entity bundles in postSave():
153 * hook_entity_bundle_create() 226 * hook_entity_bundle_create()
154 * - Comment: hook_comment_publish() and hook_comment_unpublish() as 227 * - Comment: hook_comment_publish() and hook_comment_unpublish() as
155 * appropriate. 228 * appropriate.
229 *
230 * Note that all translations available for the entity are stored during a save
231 * operation. When saving a new revision, a copy of every translation is stored,
232 * regardless of it being affected by the revision.
156 * 233 *
157 * @section edit Editing operations 234 * @section edit Editing operations
158 * When an entity's add/edit form is used to add or edit an entity, there 235 * When an entity's add/edit form is used to add or edit an entity, there
159 * are several hooks that are invoked: 236 * are several hooks that are invoked:
160 * - hook_entity_prepare_form() 237 * - hook_entity_prepare_form()