entity.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /**
  3. * @file
  4. * Entity API for handling entities like nodes or users.
  5. */
  6. use Drupal\Core\Entity\EntityInterface;
  7. /**
  8. * Clears the entity render cache for all entity types.
  9. *
  10. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Instead,
  11. * use \Drupal\Core\Entity\EntityViewBuilderInterface::resetCache() on the
  12. * required entity types or invalidate specific cache tags.
  13. *
  14. * @see https://www.drupal.org/node/3000037
  15. * @see \Drupal\Core\Entity\EntityViewBuilderInterface::resetCache()
  16. * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getDefinitions()
  17. */
  18. function entity_render_cache_clear() {
  19. @trigger_error(__FUNCTION__ . '() is deprecated. Use \Drupal\Core\Entity\EntityViewBuilderInterface::resetCache() on the required entity types or invalidate specific cache tags instead. See https://www.drupal.org/node/3000037', E_USER_DEPRECATED);
  20. $entity_manager = Drupal::entityManager();
  21. foreach ($entity_manager->getDefinitions() as $entity_type => $info) {
  22. if ($entity_manager->hasHandler($entity_type, 'view_builder')) {
  23. $entity_manager->getViewBuilder($entity_type)->resetCache();
  24. }
  25. }
  26. }
  27. /**
  28. * Returns the entity bundle info.
  29. *
  30. * @param string|null $entity_type
  31. * The entity type whose bundle info should be returned, or NULL for all
  32. * bundles info. Defaults to NULL.
  33. *
  34. * @return array
  35. * The bundle info for a specific entity type, or all entity types.
  36. *
  37. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  38. * \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getBundleInfo() for a
  39. * single bundle, or
  40. * \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getAllBundleInfo() for
  41. * all bundles.
  42. *
  43. * @see https://www.drupal.org/node/3051077
  44. * @see \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getBundleInfo()
  45. * @see \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getAllBundleInfo()
  46. */
  47. function entity_get_bundles($entity_type = NULL) {
  48. @trigger_error('entity_get_bundles() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getBundleInfo() for a single bundle, or \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getAllBundleInfo() for all bundles. See https://www.drupal.org/node/3051077', E_USER_DEPRECATED);
  49. if (isset($entity_type)) {
  50. return \Drupal::entityManager()->getBundleInfo($entity_type);
  51. }
  52. else {
  53. return \Drupal::entityManager()->getAllBundleInfo();
  54. }
  55. }
  56. /**
  57. * Loads an entity from the database.
  58. *
  59. * @param string $entity_type
  60. * The entity type to load, e.g. node or user.
  61. * @param mixed $id
  62. * The id of the entity to load.
  63. * @param bool $reset
  64. * Whether to reset the internal cache for the requested entity type.
  65. *
  66. * @return \Drupal\Core\Entity\EntityInterface|null
  67. * The entity object, or NULL if there is no entity with the given ID.
  68. *
  69. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the
  70. * entity type storage's load() method.
  71. *
  72. * @see https://www.drupal.org/node/2266845
  73. */
  74. function entity_load($entity_type, $id, $reset = FALSE) {
  75. @trigger_error('entity_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage\'s load() method. See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
  76. $controller = \Drupal::entityManager()->getStorage($entity_type);
  77. if ($reset) {
  78. $controller->resetCache([$id]);
  79. }
  80. return $controller->load($id);
  81. }
  82. /**
  83. * Loads an entity from the database.
  84. *
  85. * @param string $entity_type
  86. * The entity type to load, e.g. node or user.
  87. * @param int $revision_id
  88. * The id of the entity to load.
  89. *
  90. * @return \Drupal\Core\Entity\EntityInterface|null
  91. * The entity object, or NULL if there is no entity with the given revision
  92. * id.
  93. *
  94. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the
  95. * entity type storage's loadRevision() method.
  96. *
  97. * @see https://www.drupal.org/node/1818376
  98. */
  99. function entity_revision_load($entity_type, $revision_id) {
  100. @trigger_error('entity_revision_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage\'s loadRevision() method. See https://www.drupal.org/node/1818376', E_USER_DEPRECATED);
  101. return \Drupal::entityManager()
  102. ->getStorage($entity_type)
  103. ->loadRevision($revision_id);
  104. }
  105. /**
  106. * Deletes an entity revision.
  107. *
  108. * @param string $entity_type
  109. * The entity type to load, e.g. node or user.
  110. * @param $revision_id
  111. * The revision ID to delete.
  112. *
  113. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the
  114. * entity type storage's deleteRevision() method.
  115. *
  116. * @see https://www.drupal.org/node/1818376
  117. */
  118. function entity_revision_delete($entity_type, $revision_id) {
  119. @trigger_error('entity_revision_delete() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage\'s deleteRevision() method. See https://www.drupal.org/node/1818376', E_USER_DEPRECATED);
  120. \Drupal::entityManager()
  121. ->getStorage($entity_type)
  122. ->deleteRevision($revision_id);
  123. }
  124. /**
  125. * Loads multiple entities from the database.
  126. *
  127. * This function should be used whenever you need to load more than one entity
  128. * from the database. The entities are loaded into memory and will not require
  129. * database access if loaded again during the same page request.
  130. *
  131. * The actual loading is done through a class that has to implement the
  132. * \Drupal\Core\Entity\EntityStorageInterface interface. By default,
  133. * \Drupal\Core\Entity\Sql\SqlContentEntityStorage is used for content entities
  134. * and Drupal\Core\Config\Entity\ConfigEntityStorage for config entities. Entity
  135. * types can specify that a different class should be used by setting the
  136. * "handlers['storage']" key in the entity plugin annotation. These classes
  137. * can either implement the \Drupal\Core\Entity\EntityStorageInterface
  138. * interface, or, most commonly, extend the
  139. * \Drupal\Core\Entity\Sql\SqlContentEntityStorage class. See
  140. * \Drupal\node\Entity\Node and \Drupal\node\NodeStorage for an example.
  141. *
  142. * @param string $entity_type
  143. * The entity type to load, e.g. node or user.
  144. * @param array $ids
  145. * (optional) An array of entity IDs. If omitted, all entities are loaded.
  146. * @param bool $reset
  147. * Whether to reset the internal cache for the requested entity type.
  148. *
  149. * @return array
  150. * An array of entity objects indexed by their IDs.
  151. *
  152. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the
  153. * entity type storage's loadMultiple() method.
  154. *
  155. * @see https://www.drupal.org/node/2266845
  156. */
  157. function entity_load_multiple($entity_type, array $ids = NULL, $reset = FALSE) {
  158. @trigger_error('entity_load_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage\'s loadMultiple() method. See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
  159. $controller = \Drupal::entityManager()->getStorage($entity_type);
  160. if ($reset) {
  161. $controller->resetCache($ids);
  162. }
  163. return $controller->loadMultiple($ids);
  164. }
  165. /**
  166. * Load entities by their property values.
  167. *
  168. * @param string $entity_type
  169. * The entity type to load, e.g. node or user.
  170. * @param array $values
  171. * An associative array where the keys are the property names and the
  172. * values are the values those properties must have.
  173. *
  174. * @return array
  175. * An array of entity objects indexed by their IDs. Returns an empty array if
  176. * no matching entities are found.
  177. *
  178. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the
  179. * entity type storage's loadByProperties() method.
  180. *
  181. * @see https://www.drupal.org/node/3050910
  182. */
  183. function entity_load_multiple_by_properties($entity_type, array $values) {
  184. @trigger_error('entity_load_multiple_by_properties() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage\'s loadByProperties() method. See https://www.drupal.org/node/3050910', E_USER_DEPRECATED);
  185. return \Drupal::entityManager()
  186. ->getStorage($entity_type)
  187. ->loadByProperties($values);
  188. }
  189. /**
  190. * Loads the unchanged, i.e. not modified, entity from the database.
  191. *
  192. * Unlike entity_load() this function ensures the entity is directly loaded from
  193. * the database, thus bypassing any static cache. In particular, this function
  194. * is useful to determine changes by comparing the entity being saved to the
  195. * stored entity.
  196. *
  197. * @param $entity_type
  198. * The entity type to load, e.g. node or user.
  199. * @param $id
  200. * The ID of the entity to load.
  201. *
  202. * @return \Drupal\Core\Entity\EntityInterface|null
  203. * The unchanged entity, or FALSE if the entity cannot be loaded.
  204. *
  205. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the
  206. * entity type storage's loadUnchanged() method.
  207. *
  208. * @see https://www.drupal.org/node/1935744
  209. */
  210. function entity_load_unchanged($entity_type, $id) {
  211. @trigger_error('entity_load_unchanged() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage\'s loadUnchanged() method. See https://www.drupal.org/node/1935744', E_USER_DEPRECATED);
  212. return \Drupal::entityManager()
  213. ->getStorage($entity_type)
  214. ->loadUnchanged($id);
  215. }
  216. /**
  217. * Deletes multiple entities permanently.
  218. *
  219. * @param string $entity_type
  220. * The type of the entity.
  221. * @param array $ids
  222. * An array of entity IDs of the entities to delete.
  223. *
  224. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  225. * the entity storage's delete() method to delete multiple entities:
  226. * @code
  227. * $storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
  228. * $entities = $storage_handler->loadMultiple($ids);
  229. * $storage_handler->delete($entities);
  230. * @endcode
  231. *
  232. * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
  233. * @see \Drupal\Core\Entity\EntityStorageInterface::loadMultiple()
  234. * @see \Drupal\Core\Entity\EntityStorageInterface::delete()
  235. * @see https://www.drupal.org/node/3051072
  236. */
  237. function entity_delete_multiple($entity_type, array $ids) {
  238. @trigger_error(__FUNCTION__ . ' is deprecated in drupal:8.0.0 and will be removed in drupal:9.0.0. Use the entity storage\'s delete() method to delete multiple entities. @see https://www.drupal.org/node/3051072', E_USER_DEPRECATED);
  239. $controller = \Drupal::entityManager()->getStorage($entity_type);
  240. $entities = $controller->loadMultiple($ids);
  241. $controller->delete($entities);
  242. }
  243. /**
  244. * Constructs a new entity object, without permanently saving it.
  245. *
  246. * @param string $entity_type
  247. * The type of the entity.
  248. * @param array $values
  249. * (optional) An array of values to set, keyed by property name. If the
  250. * entity type has bundles, the bundle key has to be specified.
  251. *
  252. * @return \Drupal\Core\Entity\EntityInterface
  253. * A new entity object.
  254. *
  255. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  256. * The method overriding Entity::create() for the entity type, e.g.
  257. * \Drupal\node\Entity\Node::create() if the entity type is known. If the
  258. * entity type is variable, use the entity storage's create() method to
  259. * construct a new entity:
  260. * @code
  261. * \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);
  262. * @endcode
  263. *
  264. * @see https://www.drupal.org/node/2266845
  265. * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
  266. * @see \Drupal\Core\Entity\EntityStorageInterface::create()
  267. */
  268. function entity_create($entity_type, array $values = []) {
  269. @trigger_error('entity_create() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the create() method of the entity type class directly or \Drupal::entityTypeManager()->getStorage($entity_type)->create($values) instead. See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
  270. return \Drupal::entityManager()
  271. ->getStorage($entity_type)
  272. ->create($values);
  273. }
  274. /**
  275. * Returns the label of an entity.
  276. *
  277. * @param \Drupal\Core\Entity\EntityInterface $entity
  278. * The entity for which to generate the label.
  279. * @param $langcode
  280. * (optional) The language code of the language that should be used for
  281. * getting the label. If set to NULL, the entity's default language is
  282. * used.
  283. *
  284. * @return string|null
  285. * The label of the entity, or NULL if there is no label defined.
  286. *
  287. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the
  288. * entity's label() method.
  289. *
  290. * @see https://www.drupal.org/node/2549923
  291. * @see \Drupal\Core\Entity\EntityInterface::label()
  292. */
  293. function entity_page_label(EntityInterface $entity, $langcode = NULL) {
  294. @trigger_error('entity_page_label() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity\'s label() method. See https://www.drupal.org/node/2549923', E_USER_DEPRECATED);
  295. return $entity->label($langcode);
  296. }
  297. /**
  298. * Returns the render array for an entity.
  299. *
  300. * @param \Drupal\Core\Entity\EntityInterface $entity
  301. * The entity to be rendered.
  302. * @param string $view_mode
  303. * The view mode that should be used to display the entity.
  304. * @param string $langcode
  305. * (optional) For which language the entity should be rendered, defaults to
  306. * the current content language.
  307. * @param bool $reset
  308. * (optional) Whether to reset the render cache for the requested entity.
  309. * Defaults to FALSE.
  310. *
  311. * @return array
  312. * A render array for the entity.
  313. *
  314. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  315. * Use the entity view builder's view() method for creating a render array:
  316. * @code
  317. * $view_builder = \Drupal::entityTypeManager()
  318. * ->getViewBuilder($entity->getEntityTypeId());
  319. * return $view_builder->view($entity, $view_mode, $langcode);
  320. * @endcode
  321. *
  322. * @see https://www.drupal.org/node/3033656
  323. * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getViewBuilder()
  324. * @see \Drupal\Core\Entity\EntityViewBuilderInterface::view()
  325. */
  326. function entity_view(EntityInterface $entity, $view_mode, $langcode = NULL, $reset = FALSE) {
  327. @trigger_error('entity_view() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, $view_mode, $langcode) instead. See https://www.drupal.org/node/3033656', E_USER_DEPRECATED);
  328. $render_controller = \Drupal::entityManager()->getViewBuilder($entity->getEntityTypeId());
  329. if ($reset) {
  330. $render_controller->resetCache([$entity]);
  331. }
  332. return $render_controller->view($entity, $view_mode, $langcode);
  333. }
  334. /**
  335. * Returns the render array for the provided entities.
  336. *
  337. * @param \Drupal\Core\Entity\EntityInterface[] $entities
  338. * The entities to be rendered, must be of the same type.
  339. * @param string $view_mode
  340. * The view mode that should be used to display the entity.
  341. * @param string $langcode
  342. * (optional) For which language the entity should be rendered, defaults to
  343. * the current content language.
  344. * @param bool $reset
  345. * (optional) Whether to reset the render cache for the requested entities.
  346. * Defaults to FALSE.
  347. *
  348. * @return array
  349. * A render array for the entities, indexed by the same keys as the
  350. * entities array passed in $entities.
  351. *
  352. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  353. * Use the entity view builder's viewMultiple() method for creating a render
  354. * array for the provided entities:
  355. * @code
  356. * $view_builder = \Drupal::entityTypeManager()
  357. * ->getViewBuilder($entity->getEntityTypeId());
  358. * return $view_builder->viewMultiple($entities, $view_mode, $langcode);
  359. * @endcode
  360. *
  361. * @see https://www.drupal.org/node/3033656
  362. * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getViewBuilder()
  363. * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewMultiple()
  364. */
  365. function entity_view_multiple(array $entities, $view_mode, $langcode = NULL, $reset = FALSE) {
  366. @trigger_error('entity_view_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->viewMultiple($entities, $view_mode, $langcode) instead. See https://www.drupal.org/node/3033656', E_USER_DEPRECATED);
  367. $render_controller = \Drupal::entityManager()->getViewBuilder(reset($entities)->getEntityTypeId());
  368. if ($reset) {
  369. $render_controller->resetCache($entities);
  370. }
  371. return $render_controller->viewMultiple($entities, $view_mode, $langcode);
  372. }
  373. /**
  374. * Returns the entity view display associated with a bundle and view mode.
  375. *
  376. * Use this function when assigning suggested display options for a component
  377. * in a given view mode. Note that they will only be actually used at render
  378. * time if the view mode itself is configured to use dedicated display settings
  379. * for the bundle; if not, the 'default' display is used instead.
  380. *
  381. * The function reads the entity view display from the current configuration, or
  382. * returns a ready-to-use empty one if configuration entry exists yet for this
  383. * bundle and view mode. This streamlines manipulation of display objects by
  384. * always returning a consistent object that reflects the current state of the
  385. * configuration.
  386. *
  387. * Example usage:
  388. * - Set the 'body' field to be displayed and the 'field_image' field to be
  389. * hidden on article nodes in the 'default' display.
  390. * @code
  391. * entity_get_display('node', 'article', 'default')
  392. * ->setComponent('body', array(
  393. * 'type' => 'text_summary_or_trimmed',
  394. * 'settings' => array('trim_length' => '200')
  395. * 'weight' => 1,
  396. * ))
  397. * ->removeComponent('field_image')
  398. * ->save();
  399. * @endcode
  400. *
  401. * @param string $entity_type
  402. * The entity type.
  403. * @param string $bundle
  404. * The bundle.
  405. * @param string $view_mode
  406. * The view mode, or 'default' to retrieve the 'default' display object for
  407. * this bundle.
  408. *
  409. * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
  410. * The entity view display associated with the view mode.
  411. *
  412. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  413. * EntityDisplayRepositoryInterface::getViewDisplay() instead.
  414. *
  415. * @see https://www.drupal.org/node/2835616
  416. */
  417. function entity_get_display($entity_type, $bundle, $view_mode) {
  418. @trigger_error('entity_get_display() is deprecated in drupal:8.8.0. It will be removed before drupal:9.0.0. Use \Drupal::service(\'entity_display.repository\')->getViewDisplay() instead. See https://www.drupal.org/node/2835616', E_USER_DEPRECATED);
  419. return \Drupal::service('entity_display.repository')
  420. ->getViewDisplay($entity_type, $bundle, $view_mode);
  421. }
  422. /**
  423. * Returns the entity form display associated with a bundle and form mode.
  424. *
  425. * The function reads the entity form display object from the current
  426. * configuration, or returns a ready-to-use empty one if no configuration entry
  427. * exists yet for this bundle and form mode. This streamlines manipulation of
  428. * entity form displays by always returning a consistent object that reflects
  429. * the current state of the configuration.
  430. *
  431. * Example usage:
  432. * - Set the 'body' field to be displayed with the 'text_textarea_with_summary'
  433. * widget and the 'field_image' field to be hidden on article nodes in the
  434. * 'default' form mode.
  435. * @code
  436. * entity_get_form_display('node', 'article', 'default')
  437. * ->setComponent('body', array(
  438. * 'type' => 'text_textarea_with_summary',
  439. * 'weight' => 1,
  440. * ))
  441. * ->setComponent('field_image', array(
  442. * 'region' => 'hidden',
  443. * ))
  444. * ->save();
  445. * @endcode
  446. *
  447. * @param string $entity_type
  448. * The entity type.
  449. * @param string $bundle
  450. * The bundle.
  451. * @param string $form_mode
  452. * The form mode.
  453. *
  454. * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
  455. * The entity form display associated with the given form mode.
  456. *
  457. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  458. * EntityDisplayRepositoryInterface::getFormDisplay() instead.
  459. *
  460. * @see https://www.drupal.org/node/2835616
  461. * @see \Drupal\Core\Entity\EntityStorageInterface::create()
  462. * @see \Drupal\Core\Entity\EntityStorageInterface::load()
  463. */
  464. function entity_get_form_display($entity_type, $bundle, $form_mode) {
  465. @trigger_error('entity_get_form_display() is deprecated in drupal:8.8.0. It will be removed before drupal:9.0.0. Use \Drupal::service(\'entity_display.repository\')->getFormDisplay() instead. See https://www.drupal.org/node/2835616', E_USER_DEPRECATED);
  466. return \Drupal::service('entity_display.repository')
  467. ->getFormDisplay($entity_type, $bundle, $form_mode);
  468. }