entity.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 \Drupal\Core\Entity\EntityStorageInterface::delete()
  226. * method to delete multiple entities:
  227. * @code
  228. * $storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
  229. * $entities = $storage_handler->loadMultiple($ids);
  230. * $storage_handler->delete($entities);
  231. * @endcode
  232. *
  233. * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
  234. * @see \Drupal\Core\Entity\EntityStorageInterface::loadMultiple()
  235. * @see \Drupal\Core\Entity\EntityStorageInterface::delete()
  236. * @see https://www.drupal.org/node/3051072
  237. */
  238. function entity_delete_multiple($entity_type, array $ids) {
  239. @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);
  240. $controller = \Drupal::entityManager()->getStorage($entity_type);
  241. $entities = $controller->loadMultiple($ids);
  242. $controller->delete($entities);
  243. }
  244. /**
  245. * Constructs a new entity object, without permanently saving it.
  246. *
  247. * @param string $entity_type
  248. * The type of the entity.
  249. * @param array $values
  250. * (optional) An array of values to set, keyed by property name. If the
  251. * entity type has bundles, the bundle key has to be specified.
  252. *
  253. * @return \Drupal\Core\Entity\EntityInterface
  254. * A new entity object.
  255. *
  256. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  257. * The method overriding Entity::create() for the entity type, e.g.
  258. * \Drupal\node\Entity\Node::create() if the entity type is known. If the
  259. * entity type is variable, use the entity storage's create() method to
  260. * construct a new entity:
  261. * @code
  262. * \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);
  263. * @endcode
  264. *
  265. * @see https://www.drupal.org/node/2266845
  266. * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
  267. * @see \Drupal\Core\Entity\EntityStorageInterface::create()
  268. */
  269. function entity_create($entity_type, array $values = []) {
  270. @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);
  271. return \Drupal::entityManager()
  272. ->getStorage($entity_type)
  273. ->create($values);
  274. }
  275. /**
  276. * Returns the label of an entity.
  277. *
  278. * @param \Drupal\Core\Entity\EntityInterface $entity
  279. * The entity for which to generate the label.
  280. * @param $langcode
  281. * (optional) The language code of the language that should be used for
  282. * getting the label. If set to NULL, the entity's default language is
  283. * used.
  284. *
  285. * @return string|null
  286. * The label of the entity, or NULL if there is no label defined.
  287. *
  288. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the
  289. * entity's label() method.
  290. *
  291. * @see https://www.drupal.org/node/2549923
  292. * @see \Drupal\Core\Entity\EntityInterface::label()
  293. */
  294. function entity_page_label(EntityInterface $entity, $langcode = NULL) {
  295. @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);
  296. return $entity->label($langcode);
  297. }
  298. /**
  299. * Returns the render array for an entity.
  300. *
  301. * @param \Drupal\Core\Entity\EntityInterface $entity
  302. * The entity to be rendered.
  303. * @param string $view_mode
  304. * The view mode that should be used to display the entity.
  305. * @param string $langcode
  306. * (optional) For which language the entity should be rendered, defaults to
  307. * the current content language.
  308. * @param bool $reset
  309. * (optional) Whether to reset the render cache for the requested entity.
  310. * Defaults to FALSE.
  311. *
  312. * @return array
  313. * A render array for the entity.
  314. *
  315. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  316. * Use the entity view builder's view() method for creating a render array:
  317. * @code
  318. * $view_builder = \Drupal::entityTypeManager()
  319. * ->getViewBuilder($entity->getEntityTypeId());
  320. * return $view_builder->view($entity, $view_mode, $langcode);
  321. * @endcode
  322. *
  323. * @see https://www.drupal.org/node/3033656
  324. * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getViewBuilder()
  325. * @see \Drupal\Core\Entity\EntityViewBuilderInterface::view()
  326. */
  327. function entity_view(EntityInterface $entity, $view_mode, $langcode = NULL, $reset = FALSE) {
  328. @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);
  329. $render_controller = \Drupal::entityManager()->getViewBuilder($entity->getEntityTypeId());
  330. if ($reset) {
  331. $render_controller->resetCache([$entity]);
  332. }
  333. return $render_controller->view($entity, $view_mode, $langcode);
  334. }
  335. /**
  336. * Returns the render array for the provided entities.
  337. *
  338. * @param \Drupal\Core\Entity\EntityInterface[] $entities
  339. * The entities to be rendered, must be of the same type.
  340. * @param string $view_mode
  341. * The view mode that should be used to display the entity.
  342. * @param string $langcode
  343. * (optional) For which language the entity should be rendered, defaults to
  344. * the current content language.
  345. * @param bool $reset
  346. * (optional) Whether to reset the render cache for the requested entities.
  347. * Defaults to FALSE.
  348. *
  349. * @return array
  350. * A render array for the entities, indexed by the same keys as the
  351. * entities array passed in $entities.
  352. *
  353. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  354. * Use the entity view builder's viewMultiple() method for creating a render
  355. * array for the provided entities:
  356. * @code
  357. * $view_builder = \Drupal::entityTypeManager()
  358. * ->getViewBuilder($entity->getEntityTypeId());
  359. * return $view_builder->viewMultiple($entities, $view_mode, $langcode);
  360. * @endcode
  361. *
  362. * @see https://www.drupal.org/node/3033656
  363. * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getViewBuilder()
  364. * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewMultiple()
  365. */
  366. function entity_view_multiple(array $entities, $view_mode, $langcode = NULL, $reset = FALSE) {
  367. @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);
  368. $render_controller = \Drupal::entityManager()->getViewBuilder(reset($entities)->getEntityTypeId());
  369. if ($reset) {
  370. $render_controller->resetCache($entities);
  371. }
  372. return $render_controller->viewMultiple($entities, $view_mode, $langcode);
  373. }
  374. /**
  375. * Returns the entity view display associated with a bundle and view mode.
  376. *
  377. * Use this function when assigning suggested display options for a component
  378. * in a given view mode. Note that they will only be actually used at render
  379. * time if the view mode itself is configured to use dedicated display settings
  380. * for the bundle; if not, the 'default' display is used instead.
  381. *
  382. * The function reads the entity view display from the current configuration, or
  383. * returns a ready-to-use empty one if configuration entry exists yet for this
  384. * bundle and view mode. This streamlines manipulation of display objects by
  385. * always returning a consistent object that reflects the current state of the
  386. * configuration.
  387. *
  388. * Example usage:
  389. * - Set the 'body' field to be displayed and the 'field_image' field to be
  390. * hidden on article nodes in the 'default' display.
  391. * @code
  392. * entity_get_display('node', 'article', 'default')
  393. * ->setComponent('body', array(
  394. * 'type' => 'text_summary_or_trimmed',
  395. * 'settings' => array('trim_length' => '200')
  396. * 'weight' => 1,
  397. * ))
  398. * ->removeComponent('field_image')
  399. * ->save();
  400. * @endcode
  401. *
  402. * @param string $entity_type
  403. * The entity type.
  404. * @param string $bundle
  405. * The bundle.
  406. * @param string $view_mode
  407. * The view mode, or 'default' to retrieve the 'default' display object for
  408. * this bundle.
  409. *
  410. * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
  411. * The entity view display associated with the view mode.
  412. *
  413. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  414. * EntityDisplayRepositoryInterface::getViewDisplay() instead.
  415. *
  416. * @see https://www.drupal.org/node/2835616
  417. */
  418. function entity_get_display($entity_type, $bundle, $view_mode) {
  419. @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);
  420. return \Drupal::service('entity_display.repository')
  421. ->getViewDisplay($entity_type, $bundle, $view_mode);
  422. }
  423. /**
  424. * Returns the entity form display associated with a bundle and form mode.
  425. *
  426. * The function reads the entity form display object from the current
  427. * configuration, or returns a ready-to-use empty one if no configuration entry
  428. * exists yet for this bundle and form mode. This streamlines manipulation of
  429. * entity form displays by always returning a consistent object that reflects
  430. * the current state of the configuration.
  431. *
  432. * Example usage:
  433. * - Set the 'body' field to be displayed with the 'text_textarea_with_summary'
  434. * widget and the 'field_image' field to be hidden on article nodes in the
  435. * 'default' form mode.
  436. * @code
  437. * entity_get_form_display('node', 'article', 'default')
  438. * ->setComponent('body', array(
  439. * 'type' => 'text_textarea_with_summary',
  440. * 'weight' => 1,
  441. * ))
  442. * ->setComponent('field_image', array(
  443. * 'region' => 'hidden',
  444. * ))
  445. * ->save();
  446. * @endcode
  447. *
  448. * @param string $entity_type
  449. * The entity type.
  450. * @param string $bundle
  451. * The bundle.
  452. * @param string $form_mode
  453. * The form mode.
  454. *
  455. * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
  456. * The entity form display associated with the given form mode.
  457. *
  458. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  459. * EntityDisplayRepositoryInterface::getFormDisplay() instead.
  460. *
  461. * @see https://www.drupal.org/node/2835616
  462. * @see \Drupal\Core\Entity\EntityStorageInterface::create()
  463. * @see \Drupal\Core\Entity\EntityStorageInterface::load()
  464. */
  465. function entity_get_form_display($entity_type, $bundle, $form_mode) {
  466. @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);
  467. return \Drupal::service('entity_display.repository')
  468. ->getFormDisplay($entity_type, $bundle, $form_mode);
  469. }