EntityInterface.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Access\AccessibleInterface;
  4. use Drupal\Core\Cache\CacheableDependencyInterface;
  5. use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
  6. /**
  7. * Defines a common interface for all entity objects.
  8. *
  9. * @ingroup entity_api
  10. */
  11. interface EntityInterface extends AccessibleInterface, CacheableDependencyInterface, RefinableCacheableDependencyInterface {
  12. /**
  13. * Gets the entity UUID (Universally Unique Identifier).
  14. *
  15. * The UUID is guaranteed to be unique and can be used to identify an entity
  16. * across multiple systems.
  17. *
  18. * @return string|null
  19. * The UUID of the entity, or NULL if the entity does not have one.
  20. */
  21. public function uuid();
  22. /**
  23. * Gets the identifier.
  24. *
  25. * @return string|int|null
  26. * The entity identifier, or NULL if the object does not yet have an
  27. * identifier.
  28. */
  29. public function id();
  30. /**
  31. * Gets the language of the entity.
  32. *
  33. * @return \Drupal\Core\Language\LanguageInterface
  34. * The language object.
  35. */
  36. public function language();
  37. /**
  38. * Determines whether the entity is new.
  39. *
  40. * Usually an entity is new if no ID exists for it yet. However, entities may
  41. * be enforced to be new with existing IDs too.
  42. *
  43. * @return bool
  44. * TRUE if the entity is new, or FALSE if the entity has already been saved.
  45. *
  46. * @see \Drupal\Core\Entity\EntityInterface::enforceIsNew()
  47. */
  48. public function isNew();
  49. /**
  50. * Enforces an entity to be new.
  51. *
  52. * Allows migrations to create entities with pre-defined IDs by forcing the
  53. * entity to be new before saving.
  54. *
  55. * @param bool $value
  56. * (optional) Whether the entity should be forced to be new. Defaults to
  57. * TRUE.
  58. *
  59. * @return $this
  60. *
  61. * @see \Drupal\Core\Entity\EntityInterface::isNew()
  62. */
  63. public function enforceIsNew($value = TRUE);
  64. /**
  65. * Gets the ID of the type of the entity.
  66. *
  67. * @return string
  68. * The entity type ID.
  69. */
  70. public function getEntityTypeId();
  71. /**
  72. * Gets the bundle of the entity.
  73. *
  74. * @return string
  75. * The bundle of the entity. Defaults to the entity type ID if the entity
  76. * type does not make use of different bundles.
  77. */
  78. public function bundle();
  79. /**
  80. * Gets the label of the entity.
  81. *
  82. * @return string|null
  83. * The label of the entity, or NULL if there is no label defined.
  84. */
  85. public function label();
  86. /**
  87. * Gets the URL object for the entity.
  88. *
  89. * @param string $rel
  90. * The link relationship type, for example: canonical or edit-form.
  91. * @param array $options
  92. * See \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
  93. * the available options.
  94. *
  95. * @return \Drupal\Core\Url
  96. * The URL object.
  97. *
  98. * @deprecated in Drupal 8.0.0, intended to be removed in Drupal 9.0.0
  99. * Use \Drupal\Core\Entity\EntityInterface::toUrl() instead.
  100. *
  101. * @see https://www.drupal.org/node/2614344
  102. * @see \Drupal\Core\Entity\EntityInterface::toUrl
  103. */
  104. public function urlInfo($rel = 'canonical', array $options = []);
  105. /**
  106. * Gets the URL object for the entity.
  107. *
  108. * The entity must have an id already. Content entities usually get their IDs
  109. * by saving them.
  110. *
  111. * URI templates might be set in the links array in an annotation, for
  112. * example:
  113. * @code
  114. * links = {
  115. * "canonical" = "/node/{node}",
  116. * "edit-form" = "/node/{node}/edit",
  117. * "version-history" = "/node/{node}/revisions"
  118. * }
  119. * @endcode
  120. * or specified in a callback function set like:
  121. * @code
  122. * uri_callback = "comment_uri",
  123. * @endcode
  124. * If the path is not set in the links array, the uri_callback function is
  125. * used for setting the path. If this does not exist and the link relationship
  126. * type is canonical, the path is set using the default template:
  127. * entity/entityType/id.
  128. *
  129. * @param string $rel
  130. * The link relationship type, for example: canonical or edit-form.
  131. * @param array $options
  132. * See \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
  133. * the available options.
  134. *
  135. * @return \Drupal\Core\Url
  136. * The URL object.
  137. *
  138. * @throws \Drupal\Core\Entity\EntityMalformedException
  139. * @throws \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException
  140. */
  141. public function toUrl($rel = 'canonical', array $options = []);
  142. /**
  143. * Gets the public URL for this entity.
  144. *
  145. * @param string $rel
  146. * The link relationship type, for example: canonical or edit-form.
  147. * @param array $options
  148. * See \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
  149. * the available options.
  150. *
  151. * @return string
  152. * The URL for this entity.
  153. *
  154. * @deprecated in Drupal 8.0.0, intended to be removed in Drupal 9.0.0
  155. * Please use toUrl() instead.
  156. *
  157. * @see https://www.drupal.org/node/2614344
  158. * @see \Drupal\Core\Entity\EntityInterface::toUrl
  159. */
  160. public function url($rel = 'canonical', $options = []);
  161. /**
  162. * Deprecated way of generating a link to the entity. See toLink().
  163. *
  164. * @param string|null $text
  165. * (optional) The link text for the anchor tag as a translated string.
  166. * If NULL, it will use the entity's label. Defaults to NULL.
  167. * @param string $rel
  168. * (optional) The link relationship type. Defaults to 'canonical'.
  169. * @param array $options
  170. * See \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
  171. * the available options.
  172. *
  173. * @return string
  174. * An HTML string containing a link to the entity.
  175. *
  176. * @deprecated in Drupal 8.0.0, intended to be removed in Drupal 9.0.0
  177. * Please use toLink() instead.
  178. *
  179. * @see https://www.drupal.org/node/2614344
  180. * @see \Drupal\Core\Entity\EntityInterface::toLink
  181. */
  182. public function link($text = NULL, $rel = 'canonical', array $options = []);
  183. /**
  184. * Generates the HTML for a link to this entity.
  185. *
  186. * @param string|null $text
  187. * (optional) The link text for the anchor tag as a translated string.
  188. * If NULL, it will use the entity's label. Defaults to NULL.
  189. * @param string $rel
  190. * (optional) The link relationship type. Defaults to 'canonical'.
  191. * @param array $options
  192. * See \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
  193. * the available options.
  194. *
  195. * @return \Drupal\Core\Link
  196. * A Link to the entity.
  197. *
  198. * @throws \Drupal\Core\Entity\EntityMalformedException
  199. * @throws \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException
  200. */
  201. public function toLink($text = NULL, $rel = 'canonical', array $options = []);
  202. /**
  203. * Indicates if a link template exists for a given key.
  204. *
  205. * @param string $key
  206. * The link type.
  207. *
  208. * @return bool
  209. * TRUE if the link template exists, FALSE otherwise.
  210. */
  211. public function hasLinkTemplate($key);
  212. /**
  213. * Gets a list of URI relationships supported by this entity.
  214. *
  215. * @return string[]
  216. * An array of link relationships supported by this entity.
  217. */
  218. public function uriRelationships();
  219. /**
  220. * Loads an entity.
  221. *
  222. * @param mixed $id
  223. * The id of the entity to load.
  224. *
  225. * @return static
  226. * The entity object or NULL if there is no entity with the given ID.
  227. */
  228. public static function load($id);
  229. /**
  230. * Loads one or more entities.
  231. *
  232. * @param array $ids
  233. * An array of entity IDs, or NULL to load all entities.
  234. *
  235. * @return static[]
  236. * An array of entity objects indexed by their IDs.
  237. */
  238. public static function loadMultiple(array $ids = NULL);
  239. /**
  240. * Constructs a new entity object, without permanently saving it.
  241. *
  242. * @param array $values
  243. * (optional) An array of values to set, keyed by property name. If the
  244. * entity type has bundles, the bundle key has to be specified.
  245. *
  246. * @return static
  247. * The entity object.
  248. */
  249. public static function create(array $values = []);
  250. /**
  251. * Saves an entity permanently.
  252. *
  253. * When saving existing entities, the entity is assumed to be complete,
  254. * partial updates of entities are not supported.
  255. *
  256. * @return int
  257. * Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
  258. *
  259. * @throws \Drupal\Core\Entity\EntityStorageException
  260. * In case of failures an exception is thrown.
  261. */
  262. public function save();
  263. /**
  264. * Deletes an entity permanently.
  265. *
  266. * @throws \Drupal\Core\Entity\EntityStorageException
  267. * In case of failures an exception is thrown.
  268. */
  269. public function delete();
  270. /**
  271. * Acts on an entity before the presave hook is invoked.
  272. *
  273. * Used before the entity is saved and before invoking the presave hook. Note
  274. * that in case of translatable content entities this callback is only fired
  275. * on their current translation. It is up to the developer to iterate
  276. * over all translations if needed. This is different from its counterpart in
  277. * the Field API, FieldItemListInterface::preSave(), which is fired on all
  278. * field translations automatically.
  279. * @todo Adjust existing implementations and the documentation according to
  280. * https://www.drupal.org/node/2577609 to have a consistent API.
  281. *
  282. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  283. * The entity storage object.
  284. *
  285. * @see \Drupal\Core\Field\FieldItemListInterface::preSave()
  286. *
  287. * @throws \Exception
  288. * When there is a problem that should prevent saving the entity.
  289. */
  290. public function preSave(EntityStorageInterface $storage);
  291. /**
  292. * Acts on a saved entity before the insert or update hook is invoked.
  293. *
  294. * Used after the entity is saved, but before invoking the insert or update
  295. * hook. Note that in case of translatable content entities this callback is
  296. * only fired on their current translation. It is up to the developer to
  297. * iterate over all translations if needed.
  298. *
  299. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  300. * The entity storage object.
  301. * @param bool $update
  302. * TRUE if the entity has been updated, or FALSE if it has been inserted.
  303. */
  304. public function postSave(EntityStorageInterface $storage, $update = TRUE);
  305. /**
  306. * Changes the values of an entity before it is created.
  307. *
  308. * Load defaults for example.
  309. *
  310. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  311. * The entity storage object.
  312. * @param mixed[] $values
  313. * An array of values to set, keyed by property name. If the entity type has
  314. * bundles the bundle key has to be specified.
  315. */
  316. public static function preCreate(EntityStorageInterface $storage, array &$values);
  317. /**
  318. * Acts on a created entity before hooks are invoked.
  319. *
  320. * Used after the entity is created, but before saving the entity and before
  321. * any of the presave hooks are invoked.
  322. *
  323. * See the @link entity_crud Entity CRUD topic @endlink for more information.
  324. *
  325. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  326. * The entity storage object.
  327. *
  328. * @see \Drupal\Core\Entity\EntityInterface::create()
  329. */
  330. public function postCreate(EntityStorageInterface $storage);
  331. /**
  332. * Acts on entities before they are deleted and before hooks are invoked.
  333. *
  334. * Used before the entities are deleted and before invoking the delete hook.
  335. *
  336. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  337. * The entity storage object.
  338. * @param \Drupal\Core\Entity\EntityInterface[] $entities
  339. * An array of entities.
  340. */
  341. public static function preDelete(EntityStorageInterface $storage, array $entities);
  342. /**
  343. * Acts on deleted entities before the delete hook is invoked.
  344. *
  345. * Used after the entities are deleted but before invoking the delete hook.
  346. *
  347. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  348. * The entity storage object.
  349. * @param \Drupal\Core\Entity\EntityInterface[] $entities
  350. * An array of entities.
  351. */
  352. public static function postDelete(EntityStorageInterface $storage, array $entities);
  353. /**
  354. * Acts on loaded entities.
  355. *
  356. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  357. * The entity storage object.
  358. * @param \Drupal\Core\Entity\EntityInterface[] $entities
  359. * An array of entities.
  360. */
  361. public static function postLoad(EntityStorageInterface $storage, array &$entities);
  362. /**
  363. * Creates a duplicate of the entity.
  364. *
  365. * @return static
  366. * A clone of $this with all identifiers unset, so saving it inserts a new
  367. * entity into the storage system.
  368. */
  369. public function createDuplicate();
  370. /**
  371. * Gets the entity type definition.
  372. *
  373. * @return \Drupal\Core\Entity\EntityTypeInterface
  374. * The entity type definition.
  375. */
  376. public function getEntityType();
  377. /**
  378. * Gets a list of entities referenced by this entity.
  379. *
  380. * @return \Drupal\Core\Entity\EntityInterface[]
  381. * An array of entities.
  382. */
  383. public function referencedEntities();
  384. /**
  385. * Gets the original ID.
  386. *
  387. * @return int|string|null
  388. * The original ID, or NULL if no ID was set or for entity types that do not
  389. * support renames.
  390. */
  391. public function getOriginalId();
  392. /**
  393. * Returns the cache tags that should be used to invalidate caches.
  394. *
  395. * This will not return additional cache tags added through addCacheTags().
  396. *
  397. * @return string[]
  398. * Set of cache tags.
  399. *
  400. * @see \Drupal\Core\Cache\RefinableCacheableDependencyInterface::addCacheTags()
  401. * @see \Drupal\Core\Cache\CacheableDependencyInterface::getCacheTags()
  402. */
  403. public function getCacheTagsToInvalidate();
  404. /**
  405. * Sets the original ID.
  406. *
  407. * @param int|string|null $id
  408. * The new ID to set as original ID. If the entity supports renames, setting
  409. * NULL will prevent an update from being considered a rename.
  410. *
  411. * @return $this
  412. */
  413. public function setOriginalId($id);
  414. /**
  415. * Gets an array of all property values.
  416. *
  417. * @return mixed[]
  418. * An array of property values, keyed by property name.
  419. */
  420. public function toArray();
  421. /**
  422. * Gets a typed data object for this entity object.
  423. *
  424. * The returned typed data object wraps this entity and allows dealing with
  425. * entities based on the generic typed data API.
  426. *
  427. * @return \Drupal\Core\TypedData\ComplexDataInterface
  428. * The typed data object for this entity.
  429. *
  430. * @see \Drupal\Core\TypedData\TypedDataInterface
  431. */
  432. public function getTypedData();
  433. /**
  434. * Gets the key that is used to store configuration dependencies.
  435. *
  436. * @return string
  437. * The key to be used in configuration dependencies when storing
  438. * dependencies on entities of this type.
  439. *
  440. * @see \Drupal\Core\Entity\EntityTypeInterface::getConfigDependencyKey()
  441. */
  442. public function getConfigDependencyKey();
  443. /**
  444. * Gets the configuration dependency name.
  445. *
  446. * Configuration entities can depend on content and configuration entities.
  447. * They store an array of content and config dependency names in their
  448. * "dependencies" key.
  449. *
  450. * @return string
  451. * The configuration dependency name.
  452. *
  453. * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
  454. */
  455. public function getConfigDependencyName();
  456. /**
  457. * Gets the configuration target identifier for the entity.
  458. *
  459. * Used to supply the correct format for storing a reference targeting this
  460. * entity in configuration.
  461. *
  462. * @return string
  463. * The configuration target identifier.
  464. */
  465. public function getConfigTarget();
  466. }