2011-09-21 10:09:49 +00:00
< ? php
/**
* @ file
* Entity API for handling entities like nodes or users .
*/
2012-09-12 09:18:04 +00:00
use Drupal\Core\Entity\EntityInterface ;
2016-02-15 07:35:51 +00:00
use Drupal\Core\Entity\Entity\EntityFormDisplay ;
2016-02-16 03:35:57 +00:00
use Drupal\Core\Entity\Entity\EntityViewDisplay ;
2011-09-21 10:09:49 +00:00
2013-09-22 08:31:05 +00:00
/**
* Clears the entity render cache for all entity types .
2018-12-23 13:34:50 +00:00
*
* @ deprecated in Drupal 8.7 . x and will be removed before Drupal 9.0 . 0. Instead ,
* use \Drupal\Core\Entity\EntityViewBuilderInterface :: resetCache () on the
* required entity types or invalidate specific cache tags .
*
* @ see https :// www . drupal . org / node / 3000037
* @ see \Drupal\Core\Entity\EntityViewBuilderInterface :: resetCache ()
* @ see \Drupal\Core\Entity\EntityTypeManagerInterface :: getDefinitions ()
2013-09-22 08:31:05 +00:00
*/
function entity_render_cache_clear () {
2018-12-23 13:34:50 +00:00
@ 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 );
2013-09-22 08:31:05 +00:00
$entity_manager = Drupal :: entityManager ();
foreach ( $entity_manager -> getDefinitions () as $entity_type => $info ) {
2014-08-22 11:59:25 +00:00
if ( $entity_manager -> hasHandler ( $entity_type , 'view_builder' )) {
2013-10-25 20:54:18 +00:00
$entity_manager -> getViewBuilder ( $entity_type ) -> resetCache ();
2013-09-22 08:31:05 +00:00
}
}
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
/**
2013-01-23 17:46:47 +00:00
* Returns the entity bundle info .
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*
2013-01-23 17:46:47 +00:00
* @ param string | null $entity_type
* The entity type whose bundle info should be returned , or NULL for all
* bundles info . Defaults to NULL .
*
* @ return array
* The bundle info for a specific entity type , or all entity types .
2013-09-08 11:36:50 +00:00
*
2015-07-06 02:13:24 +00:00
* @ deprecated in Drupal 8. x - dev and will be removed before Drupal 9.0 . 0. Use
2016-06-08 10:49:17 +00:00
* \Drupal\Core\Entity\EntityTypeBundleInfoInterface :: getBundleInfo () for a
* single bundle , or
* \Drupal\Core\Entity\EntityTypeBundleInfoInterface :: getAllBundleInfo () for
* all bundles .
2015-07-06 02:13:24 +00:00
*
2016-06-08 10:49:17 +00:00
* @ see \Drupal\Core\Entity\EntityTypeBundleInfoInterface :: getBundleInfo ()
* @ see \Drupal\Core\Entity\EntityTypeBundleInfoInterface :: getAllBundleInfo ()
2013-01-23 17:46:47 +00:00
*/
function entity_get_bundles ( $entity_type = NULL ) {
2013-09-08 11:36:50 +00:00
if ( isset ( $entity_type )) {
2013-09-16 03:58:06 +00:00
return \Drupal :: entityManager () -> getBundleInfo ( $entity_type );
2013-01-23 17:46:47 +00:00
}
2013-09-08 11:36:50 +00:00
else {
2013-09-16 03:58:06 +00:00
return \Drupal :: entityManager () -> getAllBundleInfo ();
2013-01-23 17:46:47 +00:00
}
}
2011-09-21 10:09:49 +00:00
/**
2012-04-26 04:07:55 +00:00
* Loads an entity from the database .
*
* @ param string $entity_type
* The entity type to load , e . g . node or user .
2013-09-12 14:47:48 +00:00
* @ param mixed $id
2012-04-26 04:07:55 +00:00
* The id of the entity to load .
* @ param bool $reset
* Whether to reset the internal cache for the requested entity type .
*
2014-07-07 13:31:23 +00:00
* @ return \Drupal\Core\Entity\EntityInterface | null
* The entity object , or NULL if there is no entity with the given ID .
2012-04-26 04:07:55 +00:00
*
Issue #2723593 by alexpott, Jo Fitzgerald, daffie, JacobSanford, rajeshwari10, chishah92, Saviktor, marvin_B8, valthebald, Mile23, Berdir: Properly deprecate entity_load() and friends
2019-02-05 12:08:32 +00:00
* @ deprecated in Drupal 8.0 . x , will be removed before Drupal 9.0 . 0. Use the
* entity type storage ' s load () method .
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
Issue #2723593 by alexpott, Jo Fitzgerald, daffie, JacobSanford, rajeshwari10, chishah92, Saviktor, marvin_B8, valthebald, Mile23, Berdir: Properly deprecate entity_load() and friends
2019-02-05 12:08:32 +00:00
* @ see https :// www . drupal . org / node / 2266845
2012-04-26 04:07:55 +00:00
*/
function entity_load ( $entity_type , $id , $reset = FALSE ) {
Issue #2723593 by alexpott, Jo Fitzgerald, daffie, JacobSanford, rajeshwari10, chishah92, Saviktor, marvin_B8, valthebald, Mile23, Berdir: Properly deprecate entity_load() and friends
2019-02-05 12:08:32 +00:00
@ 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 );
2014-03-27 11:54:40 +00:00
$controller = \Drupal :: entityManager () -> getStorage ( $entity_type );
2013-07-01 00:09:20 +00:00
if ( $reset ) {
2017-03-04 01:20:24 +00:00
$controller -> resetCache ([ $id ]);
2013-07-01 00:09:20 +00:00
}
return $controller -> load ( $id );
2012-04-26 04:07:55 +00:00
}
2012-08-21 15:38:04 +00:00
/**
* Loads an entity from the database .
*
* @ param string $entity_type
* The entity type to load , e . g . node or user .
* @ param int $revision_id
* The id of the entity to load .
*
2014-10-14 08:32:57 +00:00
* @ return \Drupal\Core\Entity\EntityInterface | null
* The entity object , or NULL if there is no entity with the given revision
2012-08-21 15:38:04 +00:00
* id .
*
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ deprecated as of Drupal 8.0 . x , will be removed before Drupal 9.0 . 0. Use
* the entity storage ' s loadRevision () method to load a specific entity
* revision :
2018-05-25 14:25:50 +00:00
* @ code
* \Drupal :: entityTypeManager ()
* -> getStorage ( $entity_type )
* -> loadRevision ( $revision_id );
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
2016-06-08 10:49:17 +00:00
* @ see \Drupal\Core\Entity\EntityTypeManagerInterface :: getStorage ()
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ see \Drupal\Core\Entity\EntityStorageInterface :: loadRevision ()
2014-09-08 12:12:45 +00:00
* @ see \Drupal\Core\Entity\Sql\SqlContentEntityStorage
2012-08-21 15:38:04 +00:00
*/
function entity_revision_load ( $entity_type , $revision_id ) {
2013-09-16 03:58:06 +00:00
return \Drupal :: entityManager ()
2014-03-27 11:54:40 +00:00
-> getStorage ( $entity_type )
2013-01-19 04:58:23 +00:00
-> loadRevision ( $revision_id );
2012-08-21 15:38:04 +00:00
}
2012-10-14 05:44:26 +00:00
/**
2012-10-27 19:59:37 +00:00
* Deletes an entity revision .
2012-10-14 05:44:26 +00:00
*
* @ param string $entity_type
* The entity type to load , e . g . node or user .
* @ param $revision_id
* The revision ID to delete .
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
* @ deprecated as of Drupal 8.0 . x , will be removed before Drupal 9.0 . 0. Use
* the entity storage ' s deleteRevision () method to delete a specific entity
* revision :
2018-05-25 14:25:50 +00:00
* @ code
* \Drupal :: entityTypeManager ()
* -> getStorage ( $entity_type )
* -> deleteRevision ( $revision_id );
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
2016-06-08 10:49:17 +00:00
* @ see \Drupal\Core\Entity\EntityTypeManagerInterface :: getStorage ()
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ see \Drupal\Core\Entity\EntityStorageInterface :: deleteRevision ()
2012-10-14 05:44:26 +00:00
*/
function entity_revision_delete ( $entity_type , $revision_id ) {
2013-09-16 03:58:06 +00:00
\Drupal :: entityManager ()
2014-03-27 11:54:40 +00:00
-> getStorage ( $entity_type )
2013-01-19 04:58:23 +00:00
-> deleteRevision ( $revision_id );
2012-10-14 05:44:26 +00:00
}
2012-04-26 04:07:55 +00:00
/**
* Loads multiple entities from the database .
2011-09-21 10:09:49 +00:00
*
* This function should be used whenever you need to load more than one entity
* from the database . The entities are loaded into memory and will not require
* database access if loaded again during the same page request .
*
* The actual loading is done through a class that has to implement the
2016-03-08 23:33:30 +00:00
* \Drupal\Core\Entity\EntityStorageInterface interface . By default ,
* \Drupal\Core\Entity\Sql\SqlContentEntityStorage is used for content entities
2014-03-27 11:54:40 +00:00
* and Drupal\Core\Config\Entity\ConfigEntityStorage for config entities . Entity
* types can specify that a different class should be used by setting the
2016-03-08 23:33:30 +00:00
* " handlers['storage'] " key in the entity plugin annotation . These classes
* can either implement the \Drupal\Core\Entity\EntityStorageInterface
2012-10-30 20:37:18 +00:00
* interface , or , most commonly , extend the
2016-03-08 23:33:30 +00:00
* \Drupal\Core\Entity\Sql\SqlContentEntityStorage class . See
* \Drupal\node\Entity\Node and \Drupal\node\NodeStorage for an example .
2011-09-21 10:09:49 +00:00
*
2012-04-26 04:07:55 +00:00
* @ param string $entity_type
2011-09-21 10:09:49 +00:00
* The entity type to load , e . g . node or user .
2012-08-21 15:38:04 +00:00
* @ param array $ids
* ( optional ) An array of entity IDs . If omitted , all entities are loaded .
2012-04-26 04:07:55 +00:00
* @ param bool $reset
2011-09-21 10:09:49 +00:00
* Whether to reset the internal cache for the requested entity type .
*
2012-04-26 04:07:55 +00:00
* @ return array
2014-07-07 13:31:23 +00:00
* An array of entity objects indexed by their IDs .
2011-09-21 10:09:49 +00:00
*
2019-02-04 12:29:50 +00:00
* @ deprecated in Drupal 8.0 . 0 and will be removed before Drupal 9.0 . 0. Use the
* entity type storage ' s loadMultiple () method .
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
2019-02-04 12:29:50 +00:00
* @ see https :// www . drupal . org / node / 2266845
2011-09-21 10:09:49 +00:00
*/
2012-08-21 15:38:04 +00:00
function entity_load_multiple ( $entity_type , array $ids = NULL , $reset = FALSE ) {
2019-02-04 12:29:50 +00:00
@ 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 );
2014-03-27 11:54:40 +00:00
$controller = \Drupal :: entityManager () -> getStorage ( $entity_type );
2011-09-21 10:09:49 +00:00
if ( $reset ) {
2013-07-01 00:09:20 +00:00
$controller -> resetCache ( $ids );
2011-09-21 10:09:49 +00:00
}
2013-07-01 00:09:20 +00:00
return $controller -> loadMultiple ( $ids );
2012-08-21 15:38:04 +00:00
}
/**
* Load entities by their property values .
*
* @ param string $entity_type
* The entity type to load , e . g . node or user .
* @ param array $values
* An associative array where the keys are the property names and the
* values are the values those properties must have .
*
* @ return array
2014-07-07 13:31:23 +00:00
* An array of entity objects indexed by their IDs . Returns an empty array if
2015-02-18 15:21:41 +00:00
* no matching entities are found .
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
* @ deprecated as of Drupal 8.0 . x , will be removed before Drupal 9.0 . 0. Use
* the entity storage ' s loadByProperties () method to load an entity by their
* property values :
2018-05-25 14:25:50 +00:00
* @ code
* \Drupal :: entityTypeManager ()
* -> getStorage ( $entity_type )
* -> loadByProperties ( $values );
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
2016-06-08 10:49:17 +00:00
* @ see \Drupal\Core\Entity\EntityTypeManagerInterface :: getStorage ()
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ see \Drupal\Core\Entity\EntityStorageInterface :: loadByProperties ()
2012-08-21 15:38:04 +00:00
*/
function entity_load_multiple_by_properties ( $entity_type , array $values ) {
2013-09-16 03:58:06 +00:00
return \Drupal :: entityManager ()
2014-03-27 11:54:40 +00:00
-> getStorage ( $entity_type )
2013-01-19 04:58:23 +00:00
-> loadByProperties ( $values );
2011-09-21 10:09:49 +00:00
}
/**
* Loads the unchanged , i . e . not modified , entity from the database .
*
* Unlike entity_load () this function ensures the entity is directly loaded from
* the database , thus bypassing any static cache . In particular , this function
* is useful to determine changes by comparing the entity being saved to the
* stored entity .
*
* @ param $entity_type
* The entity type to load , e . g . node or user .
* @ param $id
2012-01-19 12:43:21 +00:00
* The ID of the entity to load .
2011-09-21 10:09:49 +00:00
*
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ return \Drupal\Core\Entity\EntityInterface | null
2011-09-21 10:09:49 +00:00
* The unchanged entity , or FALSE if the entity cannot be loaded .
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
* @ deprecated as of Drupal 8.0 . x , will be removed before Drupal 9.0 . 0. Use
* the entity storage ' s loadUnchanged () method to load an unchanged entity :
2018-05-25 14:25:50 +00:00
* @ code
* \Drupal :: entityTypeManager () -> getStorage ( $entity_type ) -> loadUnchanged ( $id );
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
2016-06-08 10:49:17 +00:00
* @ see \Drupal\Core\Entity\EntityTypeManagerInterface :: getStorage ()
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ see \Drupal\Core\Entity\EntityStorageInterface :: loadUnchanged ()
2011-09-21 10:09:49 +00:00
*/
function entity_load_unchanged ( $entity_type , $id ) {
2013-09-16 03:58:06 +00:00
return \Drupal :: entityManager ()
2014-03-27 11:54:40 +00:00
-> getStorage ( $entity_type )
2013-03-06 21:53:22 +00:00
-> loadUnchanged ( $id );
2011-09-21 10:09:49 +00:00
}
2011-12-05 12:12:15 +00:00
/**
* Deletes multiple entities permanently .
*
2012-10-31 23:19:29 +00:00
* @ param string $entity_type
2011-12-05 12:12:15 +00:00
* The type of the entity .
2012-10-31 23:19:29 +00:00
* @ param array $ids
2011-12-05 12:12:15 +00:00
* An array of entity IDs of the entities to delete .
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
* @ deprecated as of Drupal 8.0 . x , will be removed before Drupal 9.0 . 0. Use
* the entity storage ' s delete () method to delete multiple entities :
2018-05-25 14:25:50 +00:00
* @ code
* $storage_handler = \Drupal :: entityTypeManager () -> getStorage ( $entity_type );
* $entities = $storage_handler -> loadMultiple ( $ids );
* $storage_handler -> delete ( $entities );
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
2016-06-08 10:49:17 +00:00
* @ see \Drupal\Core\Entity\EntityTypeManagerInterface :: getStorage ()
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ see \Drupal\Core\Entity\EntityStorageInterface :: loadMultiple ()
* @ see \Drupal\Core\Entity\EntityStorageInterface :: delete ()
2011-12-05 12:12:15 +00:00
*/
2012-10-31 23:19:29 +00:00
function entity_delete_multiple ( $entity_type , array $ids ) {
2014-03-27 11:54:40 +00:00
$controller = \Drupal :: entityManager () -> getStorage ( $entity_type );
2013-07-01 00:09:20 +00:00
$entities = $controller -> loadMultiple ( $ids );
2012-10-31 23:19:29 +00:00
$controller -> delete ( $entities );
2011-12-05 12:12:15 +00:00
}
/**
2012-04-02 02:55:32 +00:00
* Constructs a new entity object , without permanently saving it .
2011-12-05 12:12:15 +00:00
*
2014-02-07 07:47:06 +00:00
* @ param string $entity_type
2011-12-05 12:12:15 +00:00
* The type of the entity .
2014-02-07 07:47:06 +00:00
* @ param array $values
* ( optional ) An array of values to set , keyed by property name . If the
* entity type has bundles , the bundle key has to be specified .
2011-12-05 12:12:15 +00:00
*
2013-10-03 11:26:25 +00:00
* @ return \Drupal\Core\Entity\EntityInterface
2011-12-05 12:12:15 +00:00
* A new entity object .
2015-06-08 22:47:11 +00:00
*
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ deprecated in Drupal 8.0 . x , will be removed before Drupal 9.0 . 0. Use
* The method overriding Entity :: create () for the entity type , e . g .
* \Drupal\node\Entity\Node :: create () if the entity type is known . If the
* entity type is variable , use the entity storage ' s create () method to
* construct a new entity :
2018-05-25 14:25:50 +00:00
* @ code
* \Drupal :: entityTypeManager () -> getStorage ( $entity_type ) -> create ( $values );
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
2016-06-08 10:49:17 +00:00
* @ see \Drupal\Core\Entity\EntityTypeManagerInterface :: getStorage ()
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ see \Drupal\Core\Entity\EntityStorageInterface :: create ()
2011-12-05 12:12:15 +00:00
*/
2017-03-04 01:20:24 +00:00
function entity_create ( $entity_type , array $values = []) {
2013-09-16 03:58:06 +00:00
return \Drupal :: entityManager ()
2014-03-27 11:54:40 +00:00
-> getStorage ( $entity_type )
2013-01-19 04:58:23 +00:00
-> create ( $values );
2011-12-05 12:12:15 +00:00
}
2011-09-21 10:09:49 +00:00
/**
* Returns the label of an entity .
*
2013-10-03 11:26:25 +00:00
* @ param \Drupal\Core\Entity\EntityInterface $entity
2011-09-21 10:09:49 +00:00
* The entity for which to generate the label .
2012-06-16 15:39:01 +00:00
* @ param $langcode
* ( optional ) The language code of the language that should be used for
* getting the label . If set to NULL , the entity ' s default language is
* used .
2011-09-21 10:09:49 +00:00
*
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ return string | null
2012-06-15 16:57:57 +00:00
* The label of the entity , or NULL if there is no label defined .
2011-12-05 12:12:15 +00:00
*
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ deprecated as of Drupal 8.0 . x , will be removed before Drupal 9.0 . 0. Use
* the entity ' s label () method to get the label of the entity :
2018-05-25 14:25:50 +00:00
* @ code
* $entity -> label ( $langcode );
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
2013-10-03 11:26:25 +00:00
* @ see \Drupal\Core\Entity\EntityInterface :: label ()
2011-09-21 10:09:49 +00:00
*/
2012-06-16 15:39:01 +00:00
function entity_page_label ( EntityInterface $entity , $langcode = NULL ) {
return $entity -> label ( $langcode );
2011-09-21 10:09:49 +00:00
}
2012-10-14 06:40:03 +00:00
/**
* Returns the render array for an entity .
*
2013-10-03 11:26:25 +00:00
* @ param \Drupal\Core\Entity\EntityInterface $entity
2012-10-14 06:40:03 +00:00
* The entity to be rendered .
* @ param string $view_mode
* The view mode that should be used to display the entity .
* @ param string $langcode
* ( optional ) For which language the entity should be rendered , defaults to
* the current content language .
2013-09-22 08:31:05 +00:00
* @ param bool $reset
* ( optional ) Whether to reset the render cache for the requested entity .
* Defaults to FALSE .
2012-10-14 06:40:03 +00:00
*
* @ return array
* A render array for the entity .
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
* @ deprecated as of Drupal 8.0 . x , will be removed before Drupal 9.0 . 0.
* Use the entity view builder ' s view () method for creating a render array :
2018-05-25 14:25:50 +00:00
* @ code
* $view_builder = \Drupal :: entityTypeManager ()
* -> getViewBuilder ( $entity -> getEntityTypeId ());
* return $view_builder -> view ( $entity , $view_mode , $langcode );
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
2016-06-08 10:49:17 +00:00
* @ see \Drupal\Core\Entity\EntityTypeManagerInterface :: getViewBuilder ()
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ see \Drupal\Core\Entity\EntityViewBuilderInterface :: view ()
2012-10-14 06:40:03 +00:00
*/
2013-09-22 08:31:05 +00:00
function entity_view ( EntityInterface $entity , $view_mode , $langcode = NULL , $reset = FALSE ) {
2014-01-30 12:06:58 +00:00
$render_controller = \Drupal :: entityManager () -> getViewBuilder ( $entity -> getEntityTypeId ());
2013-09-22 08:31:05 +00:00
if ( $reset ) {
2015-10-05 23:33:05 +00:00
$render_controller -> resetCache ([ $entity ]);
2013-09-22 08:31:05 +00:00
}
return $render_controller -> view ( $entity , $view_mode , $langcode );
2012-10-14 06:40:03 +00:00
}
/**
* Returns the render array for the provided entities .
*
2014-01-30 12:06:58 +00:00
* @ param \Drupal\Core\Entity\EntityInterface [] $entities
2012-10-14 06:40:03 +00:00
* The entities to be rendered , must be of the same type .
* @ param string $view_mode
* The view mode that should be used to display the entity .
* @ param string $langcode
* ( optional ) For which language the entity should be rendered , defaults to
* the current content language .
2013-09-22 08:31:05 +00:00
* @ param bool $reset
* ( optional ) Whether to reset the render cache for the requested entities .
* Defaults to FALSE .
2012-10-14 06:40:03 +00:00
*
* @ return array
* A render array for the entities , indexed by the same keys as the
* entities array passed in $entities .
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
* @ deprecated as of Drupal 8.0 . x , will be removed before Drupal 9.0 . 0.
* Use the entity view builder ' s viewMultiple () method for creating a render
* array for the provided entities :
2018-05-25 14:25:50 +00:00
* @ code
* $view_builder = \Drupal :: entityTypeManager ()
* -> getViewBuilder ( $entity -> getEntityTypeId ());
* return $view_builder -> viewMultiple ( $entities , $view_mode , $langcode );
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
2016-06-08 10:49:17 +00:00
* @ see \Drupal\Core\Entity\EntityTypeManagerInterface :: getViewBuilder ()
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* @ see \Drupal\Core\Entity\EntityViewBuilderInterface :: viewMultiple ()
2012-10-14 06:40:03 +00:00
*/
2013-09-22 08:31:05 +00:00
function entity_view_multiple ( array $entities , $view_mode , $langcode = NULL , $reset = FALSE ) {
2014-01-30 12:06:58 +00:00
$render_controller = \Drupal :: entityManager () -> getViewBuilder ( reset ( $entities ) -> getEntityTypeId ());
2013-09-22 08:31:05 +00:00
if ( $reset ) {
2015-10-05 23:33:05 +00:00
$render_controller -> resetCache ( $entities );
2013-09-22 08:31:05 +00:00
}
return $render_controller -> viewMultiple ( $entities , $view_mode , $langcode );
2012-10-14 06:40:03 +00:00
}
2012-10-30 10:41:42 +00:00
2012-12-28 23:03:17 +00:00
/**
2015-08-18 12:10:00 +00:00
* Returns the entity view display associated with a bundle and view mode .
2012-12-28 23:03:17 +00:00
*
* Use this function when assigning suggested display options for a component
* in a given view mode . Note that they will only be actually used at render
* time if the view mode itself is configured to use dedicated display settings
* for the bundle ; if not , the 'default' display is used instead .
*
2014-02-09 00:54:32 +00:00
* The function reads the entity view display from the current configuration , or
* returns a ready - to - use empty one if configuration entry exists yet for this
* bundle and view mode . This streamlines manipulation of display objects by
* always returning a consistent object that reflects the current state of the
* configuration .
2012-12-28 23:03:17 +00:00
*
* Example usage :
* - Set the 'body' field to be displayed and the 'field_image' field to be
* hidden on article nodes in the 'default' display .
* @ code
2012-12-31 21:57:58 +00:00
* entity_get_display ( 'node' , 'article' , 'default' )
2012-12-28 23:03:17 +00:00
* -> setComponent ( 'body' , array (
* 'type' => 'text_summary_or_trimmed' ,
* 'settings' => array ( 'trim_length' => '200' )
* 'weight' => 1 ,
* ))
* -> removeComponent ( 'field_image' )
* -> save ();
* @ endcode
*
* @ param string $entity_type
* The entity type .
* @ param string $bundle
* The bundle .
* @ param string $view_mode
* The view mode , or 'default' to retrieve the 'default' display object for
* this bundle .
*
2013-12-12 23:34:44 +00:00
* @ return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
2015-08-18 12:10:00 +00:00
* The entity view display associated with the view mode .
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
* @ deprecated as of Drupal 8.0 . x , will be removed before Drupal 9.0 . 0.
* If the display is available in configuration use :
2015-08-31 16:33:11 +00:00
* @ code
2016-06-08 10:49:17 +00:00
* \Drupal :: entityTypeManager ()
* -> getStorage ( 'entity_view_display' )
* -> load ( $entity_type . '.' . $bundle . '.' . $view_mode );
2015-08-31 16:33:11 +00:00
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
* When the display is not available in configuration , you can create a new
* EntityViewDisplay object using :
2015-08-31 16:33:11 +00:00
* @ code
* $values = array (
* 'targetEntityType' => $entity_type ,
* 'bundle' => $bundle ,
* 'mode' => $view_mode ,
* 'status' => TRUE ,
2016-09-22 16:40:39 +00:00
* );
2016-06-08 10:49:17 +00:00
* \Drupal :: entityTypeManager ()
* -> getStorage ( 'entity_view_display' )
* -> create ( $values );
2015-08-31 16:33:11 +00:00
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
* @ see \Drupal\Core\Entity\EntityStorageInterface :: create ()
* @ see \Drupal\Core\Entity\EntityStorageInterface :: load ()
2012-12-28 23:03:17 +00:00
*/
function entity_get_display ( $entity_type , $bundle , $view_mode ) {
// Try loading the display from configuration.
2016-05-18 12:03:34 +00:00
$display = EntityViewDisplay :: load ( $entity_type . '.' . $bundle . '.' . $view_mode );
2012-12-28 23:03:17 +00:00
// If not found, create a fresh display object. We do not preemptively create
2014-02-09 00:54:32 +00:00
// new entity_view_display configuration entries for each existing entity type
// and bundle whenever a new view mode becomes available. Instead,
// configuration entries are only created when a display object is explicitly
// configured and saved.
2012-12-28 23:03:17 +00:00
if ( ! $display ) {
2017-03-04 01:20:24 +00:00
$display = EntityViewDisplay :: create ([
2012-12-28 23:03:17 +00:00
'targetEntityType' => $entity_type ,
'bundle' => $bundle ,
2013-05-19 20:02:16 +00:00
'mode' => $view_mode ,
2013-09-27 10:06:58 +00:00
'status' => TRUE ,
2017-03-04 01:20:24 +00:00
]);
2012-12-28 23:03:17 +00:00
}
return $display ;
}
2013-05-19 20:02:16 +00:00
/**
2015-08-18 12:10:00 +00:00
* Returns the entity form display associated with a bundle and form mode .
2013-05-19 20:02:16 +00:00
*
2014-02-09 00:54:32 +00:00
* The function reads the entity form display object from the current
* configuration , or returns a ready - to - use empty one if no configuration entry
2013-05-19 20:02:16 +00:00
* exists yet for this bundle and form mode . This streamlines manipulation of
2014-02-09 00:54:32 +00:00
* entity form displays by always returning a consistent object that reflects
* the current state of the configuration .
2013-05-19 20:02:16 +00:00
*
* Example usage :
* - Set the 'body' field to be displayed with the 'text_textarea_with_summary'
* widget and the 'field_image' field to be hidden on article nodes in the
* 'default' form mode .
* @ code
* entity_get_form_display ( 'node' , 'article' , 'default' )
* -> setComponent ( 'body' , array (
* 'type' => 'text_textarea_with_summary' ,
* 'weight' => 1 ,
* ))
* -> setComponent ( 'field_image' , array (
2016-10-25 21:21:33 +00:00
* 'region' => 'hidden' ,
2013-05-19 20:02:16 +00:00
* ))
* -> save ();
* @ endcode
*
* @ param string $entity_type
* The entity type .
* @ param string $bundle
* The bundle .
* @ param string $form_mode
* The form mode .
*
2013-12-12 23:34:44 +00:00
* @ return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
2015-08-18 12:10:00 +00:00
* The entity form display associated with the given form mode .
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
* @ deprecated as of Drupal 8.0 . x , will be removed before Drupal 9.0 . 0.
* If the entity form display is available in configuration use :
2017-07-20 13:49:09 +00:00
* @ code
* \Drupal :: entityTypeManager ()
* -> getStorage ( 'entity_form_display' )
* -> load ( $entity_type . '.' . $bundle . '.' . $form_mode );
* @ endcode
2016-06-08 10:49:17 +00:00
* When the entity form display is not available in configuration , you can
* create a new EntityFormDisplay object using :
2017-07-20 13:49:09 +00:00
* @ code
* $values = array (
* 'targetEntityType' => $entity_type ,
* 'bundle' => $bundle ,
* 'mode' => $form_mode ,
* 'status' => TRUE ,
* );
* \Drupal :: entityTypeManager ()
* -> getStorage ( 'entity_form_display' )
* -> create ( $values );
* @ endcode
Issue #2474151 by JeroenT, dcmul, naveenvalecha, sorabh.v6, trwad, Nitesh Sethia, sushilkr, disasm, dylanf, Mile23, xjm, dawehner, tim.plunkett, andypost: Mark procedural wrappers in entity.inc as deprecated
2015-07-30 14:21:44 +00:00
*
* @ see \Drupal\Core\Entity\EntityStorageInterface :: create ()
* @ see \Drupal\Core\Entity\EntityStorageInterface :: load ()
2013-05-19 20:02:16 +00:00
*/
function entity_get_form_display ( $entity_type , $bundle , $form_mode ) {
// Try loading the entity from configuration.
2016-05-17 09:28:36 +00:00
$entity_form_display = EntityFormDisplay :: load ( $entity_type . '.' . $bundle . '.' . $form_mode );
2013-05-19 20:02:16 +00:00
// If not found, create a fresh entity object. We do not preemptively create
2014-02-09 00:54:32 +00:00
// new entity form display configuration entries for each existing entity type
2013-05-19 20:02:16 +00:00
// and bundle whenever a new form mode becomes available. Instead,
2014-02-09 00:54:32 +00:00
// configuration entries are only created when an entity form display is
2013-05-19 20:02:16 +00:00
// explicitly configured and saved.
if ( ! $entity_form_display ) {
2017-03-04 01:20:24 +00:00
$entity_form_display = EntityFormDisplay :: create ([
2013-05-19 20:02:16 +00:00
'targetEntityType' => $entity_type ,
'bundle' => $bundle ,
'mode' => $form_mode ,
2013-09-27 10:06:58 +00:00
'status' => TRUE ,
2017-03-04 01:20:24 +00:00
]);
2013-05-19 20:02:16 +00:00
}
return $entity_form_display ;
}