comparison core/lib/Drupal/Core/Entity/entity.api.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
7 7
8 use Drupal\Core\Access\AccessResult; 8 use Drupal\Core\Access\AccessResult;
9 use Drupal\Core\Entity\ContentEntityInterface; 9 use Drupal\Core\Entity\ContentEntityInterface;
10 use Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface; 10 use Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface;
11 use Drupal\Core\Field\BaseFieldDefinition; 11 use Drupal\Core\Field\BaseFieldDefinition;
12 use Drupal\Core\Field\FieldDefinition;
12 use Drupal\Core\Render\Element; 13 use Drupal\Core\Render\Element;
13 use Drupal\language\Entity\ContentLanguageSettings; 14 use Drupal\language\Entity\ContentLanguageSettings;
14 use Drupal\node\Entity\NodeType; 15 use Drupal\node\Entity\NodeType;
15 16
16 /** 17 /**
528 * @section load_query Loading, querying, and rendering entities 529 * @section load_query Loading, querying, and rendering entities
529 * To load entities, use the entity storage manager, which is an object 530 * To load entities, use the entity storage manager, which is an object
530 * implementing \Drupal\Core\Entity\EntityStorageInterface that you can 531 * implementing \Drupal\Core\Entity\EntityStorageInterface that you can
531 * retrieve with: 532 * retrieve with:
532 * @code 533 * @code
533 * $storage = \Drupal::entityManager()->getStorage('your_entity_type'); 534 * $storage = \Drupal::entityTypeManager()->getStorage('your_entity_type');
534 * // Or if you have a $container variable: 535 * // Or if you have a $container variable:
535 * $storage = $container->get('entity.manager')->getStorage('your_entity_type'); 536 * $storage = $container->get('entity_type.manager')->getStorage('your_entity_type');
536 * @endcode 537 * @endcode
537 * Here, 'your_entity_type' is the machine name of your entity type ('id' 538 * Here, 'your_entity_type' is the machine name of your entity type ('id'
538 * annotation property on the entity class), and note that you should use 539 * annotation property on the entity class), and note that you should use
539 * dependency injection to retrieve this object if possible. See the 540 * dependency injection to retrieve this object if possible. See the
540 * @link container Services and Dependency Injection topic @endlink for more 541 * @link container Services and Dependency Injection topic @endlink for more
619 * developers can interact with. This is described in the 620 * developers can interact with. This is described in the
620 * @link node_access Node access topic. @endlink 621 * @link node_access Node access topic. @endlink
621 * 622 *
622 * @see i18n 623 * @see i18n
623 * @see entity_crud 624 * @see entity_crud
624 * @see \Drupal\Core\Entity\EntityManagerInterface::getTranslationFromContext() 625 * @see \Drupal\Core\Entity\EntityRepositoryInterface::getTranslationFromContext()
625 * @} 626 * @}
626 */ 627 */
627 628
628 /** 629 /**
629 * @addtogroup hooks 630 * @addtogroup hooks
795 * Alter the view modes for entity types. 796 * Alter the view modes for entity types.
796 * 797 *
797 * @param array $view_modes 798 * @param array $view_modes
798 * An array of view modes, keyed first by entity type, then by view mode name. 799 * An array of view modes, keyed first by entity type, then by view mode name.
799 * 800 *
800 * @see \Drupal\Core\Entity\EntityManagerInterface::getAllViewModes() 801 * @see \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getAllViewModes()
801 * @see \Drupal\Core\Entity\EntityManagerInterface::getViewModes() 802 * @see \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewModes()
802 */ 803 */
803 function hook_entity_view_mode_info_alter(&$view_modes) { 804 function hook_entity_view_mode_info_alter(&$view_modes) {
804 $view_modes['user']['full']['status'] = TRUE; 805 $view_modes['user']['full']['status'] = TRUE;
805 } 806 }
806 807
810 * @return array 811 * @return array
811 * An associative array of all entity bundles, keyed by the entity 812 * An associative array of all entity bundles, keyed by the entity
812 * type name, and then the bundle name, with the following keys: 813 * type name, and then the bundle name, with the following keys:
813 * - label: The human-readable name of the bundle. 814 * - label: The human-readable name of the bundle.
814 * - uri_callback: (optional) The same as the 'uri_callback' key defined for 815 * - uri_callback: (optional) The same as the 'uri_callback' key defined for
815 * the entity type in the EntityManager, but for the bundle only. When 816 * the entity type in the EntityTypeManager, but for the bundle only. When
816 * determining the URI of an entity, if a 'uri_callback' is defined for both 817 * determining the URI of an entity, if a 'uri_callback' is defined for both
817 * the entity type and the bundle, the one for the bundle is used. 818 * the entity type and the bundle, the one for the bundle is used.
818 * - translatable: (optional) A boolean value specifying whether this bundle 819 * - translatable: (optional) A boolean value specifying whether this bundle
819 * has translation support enabled. Defaults to FALSE. 820 * has translation support enabled. Defaults to FALSE.
820 * 821 *
956 // synchronized from the default revision. 957 // synchronized from the default revision.
957 $new_revision->set('untranslatable_field', $entity->get('untranslatable_field')); 958 $new_revision->set('untranslatable_field', $entity->get('untranslatable_field'));
958 } 959 }
959 960
960 /** 961 /**
962 * Act on an array of entity IDs before they are loaded.
963 *
964 * This hook can be used by modules that need, for example, to return a
965 * different revision than the default one.
966 *
967 * @param array $ids
968 * An array of entity IDs that have to be loaded.
969 * @param string $entity_type_id
970 * The type of entities being loaded (i.e. node, user, comment).
971 *
972 * @return \Drupal\Core\Entity\EntityInterface[]
973 * An array of pre-loaded entity objects.
974 *
975 * @ingroup entity_crud
976 */
977 function hook_entity_preload(array $ids, $entity_type_id) {
978 $entities = [];
979
980 foreach ($ids as $id) {
981 $entities[] = mymodule_swap_revision($id);
982 }
983
984 return $entities;
985 }
986
987 /**
961 * Act on entities when loaded. 988 * Act on entities when loaded.
962 * 989 *
963 * This is a generic load hook called for all entity types loaded via the 990 * This is a generic load hook called for all entity types loaded via the
964 * entity API. 991 * entity API.
965 * 992 *
1079 * @ingroup entity_crud 1106 * @ingroup entity_crud
1080 * @see hook_ENTITY_TYPE_insert() 1107 * @see hook_ENTITY_TYPE_insert()
1081 */ 1108 */
1082 function hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity) { 1109 function hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
1083 // Insert the new entity into a fictional table of all entities. 1110 // Insert the new entity into a fictional table of all entities.
1084 db_insert('example_entity') 1111 \Drupal::database()->insert('example_entity')
1085 ->fields([ 1112 ->fields([
1086 'type' => $entity->getEntityTypeId(), 1113 'type' => $entity->getEntityTypeId(),
1087 'id' => $entity->id(), 1114 'id' => $entity->id(),
1088 'created' => REQUEST_TIME, 1115 'created' => REQUEST_TIME,
1089 'updated' => REQUEST_TIME, 1116 'updated' => REQUEST_TIME,
1103 * @ingroup entity_crud 1130 * @ingroup entity_crud
1104 * @see hook_entity_insert() 1131 * @see hook_entity_insert()
1105 */ 1132 */
1106 function hook_ENTITY_TYPE_insert(Drupal\Core\Entity\EntityInterface $entity) { 1133 function hook_ENTITY_TYPE_insert(Drupal\Core\Entity\EntityInterface $entity) {
1107 // Insert the new entity into a fictional table of this type of entity. 1134 // Insert the new entity into a fictional table of this type of entity.
1108 db_insert('example_entity') 1135 \Drupal::database()->insert('example_entity')
1109 ->fields([ 1136 ->fields([
1110 'id' => $entity->id(), 1137 'id' => $entity->id(),
1111 'created' => REQUEST_TIME, 1138 'created' => REQUEST_TIME,
1112 'updated' => REQUEST_TIME, 1139 'updated' => REQUEST_TIME,
1113 ]) 1140 ])
1127 * @ingroup entity_crud 1154 * @ingroup entity_crud
1128 * @see hook_ENTITY_TYPE_update() 1155 * @see hook_ENTITY_TYPE_update()
1129 */ 1156 */
1130 function hook_entity_update(Drupal\Core\Entity\EntityInterface $entity) { 1157 function hook_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
1131 // Update the entity's entry in a fictional table of all entities. 1158 // Update the entity's entry in a fictional table of all entities.
1132 db_update('example_entity') 1159 \Drupal::database()->update('example_entity')
1133 ->fields([ 1160 ->fields([
1134 'updated' => REQUEST_TIME, 1161 'updated' => REQUEST_TIME,
1135 ]) 1162 ])
1136 ->condition('type', $entity->getEntityTypeId()) 1163 ->condition('type', $entity->getEntityTypeId())
1137 ->condition('id', $entity->id()) 1164 ->condition('id', $entity->id())
1151 * @ingroup entity_crud 1178 * @ingroup entity_crud
1152 * @see hook_entity_update() 1179 * @see hook_entity_update()
1153 */ 1180 */
1154 function hook_ENTITY_TYPE_update(Drupal\Core\Entity\EntityInterface $entity) { 1181 function hook_ENTITY_TYPE_update(Drupal\Core\Entity\EntityInterface $entity) {
1155 // Update the entity's entry in a fictional table of this type of entity. 1182 // Update the entity's entry in a fictional table of this type of entity.
1156 db_update('example_entity') 1183 \Drupal::database()->update('example_entity')
1157 ->fields([ 1184 ->fields([
1158 'updated' => REQUEST_TIME, 1185 'updated' => REQUEST_TIME,
1159 ]) 1186 ])
1160 ->condition('id', $entity->id()) 1187 ->condition('id', $entity->id())
1161 ->execute(); 1188 ->execute();
1279 * 1306 *
1280 * @ingroup entity_crud 1307 * @ingroup entity_crud
1281 * @see hook_ENTITY_TYPE_predelete() 1308 * @see hook_ENTITY_TYPE_predelete()
1282 */ 1309 */
1283 function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) { 1310 function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
1311 $connection = \Drupal::database();
1284 // Count references to this entity in a custom table before they are removed 1312 // Count references to this entity in a custom table before they are removed
1285 // upon entity deletion. 1313 // upon entity deletion.
1286 $id = $entity->id(); 1314 $id = $entity->id();
1287 $type = $entity->getEntityTypeId(); 1315 $type = $entity->getEntityTypeId();
1288 $count = db_select('example_entity_data') 1316 $count = \Drupal::database()->select('example_entity_data')
1289 ->condition('type', $type) 1317 ->condition('type', $type)
1290 ->condition('id', $id) 1318 ->condition('id', $id)
1291 ->countQuery() 1319 ->countQuery()
1292 ->execute() 1320 ->execute()
1293 ->fetchField(); 1321 ->fetchField();
1294 1322
1295 // Log the count in a table that records this statistic for deleted entities. 1323 // Log the count in a table that records this statistic for deleted entities.
1296 db_merge('example_deleted_entity_statistics') 1324 $connection->merge('example_deleted_entity_statistics')
1297 ->key(['type' => $type, 'id' => $id]) 1325 ->key(['type' => $type, 'id' => $id])
1298 ->fields(['count' => $count]) 1326 ->fields(['count' => $count])
1299 ->execute(); 1327 ->execute();
1300 } 1328 }
1301 1329
1307 * 1335 *
1308 * @ingroup entity_crud 1336 * @ingroup entity_crud
1309 * @see hook_entity_predelete() 1337 * @see hook_entity_predelete()
1310 */ 1338 */
1311 function hook_ENTITY_TYPE_predelete(Drupal\Core\Entity\EntityInterface $entity) { 1339 function hook_ENTITY_TYPE_predelete(Drupal\Core\Entity\EntityInterface $entity) {
1340 $connection = \Drupal::database();
1312 // Count references to this entity in a custom table before they are removed 1341 // Count references to this entity in a custom table before they are removed
1313 // upon entity deletion. 1342 // upon entity deletion.
1314 $id = $entity->id(); 1343 $id = $entity->id();
1315 $type = $entity->getEntityTypeId(); 1344 $type = $entity->getEntityTypeId();
1316 $count = db_select('example_entity_data') 1345 $count = \Drupal::database()->select('example_entity_data')
1317 ->condition('type', $type) 1346 ->condition('type', $type)
1318 ->condition('id', $id) 1347 ->condition('id', $id)
1319 ->countQuery() 1348 ->countQuery()
1320 ->execute() 1349 ->execute()
1321 ->fetchField(); 1350 ->fetchField();
1322 1351
1323 // Log the count in a table that records this statistic for deleted entities. 1352 // Log the count in a table that records this statistic for deleted entities.
1324 db_merge('example_deleted_entity_statistics') 1353 $connection->merge('example_deleted_entity_statistics')
1325 ->key(['type' => $type, 'id' => $id]) 1354 ->key(['type' => $type, 'id' => $id])
1326 ->fields(['count' => $count]) 1355 ->fields(['count' => $count])
1327 ->execute(); 1356 ->execute();
1328 } 1357 }
1329 1358
1338 * @ingroup entity_crud 1367 * @ingroup entity_crud
1339 * @see hook_ENTITY_TYPE_delete() 1368 * @see hook_ENTITY_TYPE_delete()
1340 */ 1369 */
1341 function hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity) { 1370 function hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
1342 // Delete the entity's entry from a fictional table of all entities. 1371 // Delete the entity's entry from a fictional table of all entities.
1343 db_delete('example_entity') 1372 \Drupal::database()->delete('example_entity')
1344 ->condition('type', $entity->getEntityTypeId()) 1373 ->condition('type', $entity->getEntityTypeId())
1345 ->condition('id', $entity->id()) 1374 ->condition('id', $entity->id())
1346 ->execute(); 1375 ->execute();
1347 } 1376 }
1348 1377
1357 * @ingroup entity_crud 1386 * @ingroup entity_crud
1358 * @see hook_entity_delete() 1387 * @see hook_entity_delete()
1359 */ 1388 */
1360 function hook_ENTITY_TYPE_delete(Drupal\Core\Entity\EntityInterface $entity) { 1389 function hook_ENTITY_TYPE_delete(Drupal\Core\Entity\EntityInterface $entity) {
1361 // Delete the entity's entry from a fictional table of all entities. 1390 // Delete the entity's entry from a fictional table of all entities.
1362 db_delete('example_entity') 1391 \Drupal::database()->delete('example_entity')
1363 ->condition('type', $entity->getEntityTypeId()) 1392 ->condition('type', $entity->getEntityTypeId())
1364 ->condition('id', $entity->id()) 1393 ->condition('id', $entity->id())
1365 ->execute(); 1394 ->execute();
1366 } 1395 }
1367 1396
1796 * 1825 *
1797 * @see hook_entity_base_field_info_alter() 1826 * @see hook_entity_base_field_info_alter()
1798 * @see hook_entity_bundle_field_info() 1827 * @see hook_entity_bundle_field_info()
1799 * @see hook_entity_bundle_field_info_alter() 1828 * @see hook_entity_bundle_field_info_alter()
1800 * @see \Drupal\Core\Field\FieldDefinitionInterface 1829 * @see \Drupal\Core\Field\FieldDefinitionInterface
1801 * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions() 1830 * @see \Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()
1802 */ 1831 */
1803 function hook_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) { 1832 function hook_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
1804 if ($entity_type->id() == 'node') { 1833 if ($entity_type->id() == 'node') {
1805 $fields = []; 1834 $fields = [];
1806 $fields['mymodule_text'] = BaseFieldDefinition::create('string') 1835 $fields['mymodule_text'] = BaseFieldDefinition::create('string')
1856 * @see hook_entity_base_field_info_alter() 1885 * @see hook_entity_base_field_info_alter()
1857 * @see hook_entity_field_storage_info() 1886 * @see hook_entity_field_storage_info()
1858 * @see hook_entity_field_storage_info_alter() 1887 * @see hook_entity_field_storage_info_alter()
1859 * @see hook_entity_bundle_field_info_alter() 1888 * @see hook_entity_bundle_field_info_alter()
1860 * @see \Drupal\Core\Field\FieldDefinitionInterface 1889 * @see \Drupal\Core\Field\FieldDefinitionInterface
1861 * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions() 1890 * @see \Drupal\Core\Field\FieldDefinition
1891 * @see \Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()
1862 * 1892 *
1863 * @todo WARNING: This hook will be changed in 1893 * @todo WARNING: This hook will be changed in
1864 * https://www.drupal.org/node/2346347. 1894 * https://www.drupal.org/node/2346347.
1865 */ 1895 */
1866 function hook_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { 1896 function hook_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
1867 // Add a property only to nodes of the 'article' bundle. 1897 // Add a property only to nodes of the 'article' bundle.
1868 if ($entity_type->id() == 'node' && $bundle == 'article') { 1898 if ($entity_type->id() == 'node' && $bundle == 'article') {
1869 $fields = []; 1899 $fields = [];
1870 $fields['mymodule_text_more'] = BaseFieldDefinition::create('string') 1900 $storage_definitions = mymodule_entity_field_storage_info($entity_type);
1871 ->setLabel(t('More text')) 1901 $fields['mymodule_bundle_field'] = FieldDefinition::createFromFieldStorageDefinition($storage_definitions['mymodule_bundle_field'])
1872 ->setComputed(TRUE) 1902 ->setLabel(t('Bundle Field'));
1873 ->setClass('\Drupal\mymodule\EntityComputedMoreText');
1874 return $fields; 1903 return $fields;
1875 } 1904 }
1905
1876 } 1906 }
1877 1907
1878 /** 1908 /**
1879 * Alter bundle field definitions. 1909 * Alter bundle field definitions.
1880 * 1910 *