public SqlContentEntityStorage::countFieldData($storage_definition, $as_bool = FALSE)
Determines the number of entities with values for a given field.
Parameters
\Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition: The field for which to count data records.
bool $as_bool: (Optional) Optimises the query for checking whether there are any records or not. Defaults to FALSE.
Return value
bool|int The number of entities. If $as_bool parameter is TRUE then the value will either be TRUE or FALSE.
Overrides FieldableEntityStorageInterface::countFieldData
See also
\Drupal\Core\Entity\FieldableEntityStorageInterface::purgeFieldData()
File
- core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php, line 1605
Class
- SqlContentEntityStorage
- A content entity database storage implementation.
Namespace
Drupal\Core\Entity\Sql
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | public function countFieldData( $storage_definition , $as_bool = FALSE) { // The table mapping contains stale data during a request when a field // storage definition is added, so bypass the internal storage definitions // and fetch the table mapping using the passed in storage definition. // @todo Fix this in https://www.drupal.org/node/2705205. $storage_definitions = $this ->entityManager->getFieldStorageDefinitions( $this ->entityTypeId); $storage_definitions [ $storage_definition ->getName()] = $storage_definition ; $table_mapping = $this ->getTableMapping( $storage_definitions ); if ( $table_mapping ->requiresDedicatedTableStorage( $storage_definition )) { $is_deleted = $this ->storageDefinitionIsDeleted( $storage_definition ); if ( $this ->entityType->isRevisionable()) { $table_name = $table_mapping ->getDedicatedRevisionTableName( $storage_definition , $is_deleted ); } else { $table_name = $table_mapping ->getDedicatedDataTableName( $storage_definition , $is_deleted ); } $query = $this ->database->select( $table_name , 't' ); $or = $query ->orConditionGroup(); foreach ( $storage_definition ->getColumns() as $column_name => $data ) { $or ->isNotNull( $table_mapping ->getFieldColumnName( $storage_definition , $column_name )); } $query ->condition( $or ); if (! $as_bool ) { $query ->fields( 't' , array ( 'entity_id' )) ->distinct(TRUE); } } elseif ( $table_mapping ->allowsSharedTableStorage( $storage_definition )) { // Ascertain the table this field is mapped too. $field_name = $storage_definition ->getName(); try { $table_name = $table_mapping ->getFieldTableName( $field_name ); } catch (SqlContentEntityStorageException $e ) { // This may happen when changing field storage schema, since we are not // able to use a table mapping matching the passed storage definition. // @todo Revisit this once we are able to instantiate the table mapping // properly. See https://www.drupal.org/node/2274017. $table_name = $this ->dataTable ? : $this ->baseTable; } $query = $this ->database->select( $table_name , 't' ); $or = $query ->orConditionGroup(); foreach ( array_keys ( $storage_definition ->getColumns()) as $property_name ) { $or ->isNotNull( $table_mapping ->getFieldColumnName( $storage_definition , $property_name )); } $query ->condition( $or ); if (! $as_bool ) { $query ->fields( 't' , array ( $this ->idKey)) ->distinct(TRUE); } } // @todo Find a way to count field data also for fields having custom // storage. See https://www.drupal.org/node/2337753. $count = 0; if (isset( $query )) { // If we are performing the query just to check if the field has data // limit the number of rows. if ( $as_bool ) { $query ->range(0, 1) ->addExpression( '1' ); } else { // Otherwise count the number of rows. $query = $query ->countQuery(); } $count = $query ->execute()->fetchField(); } return $as_bool ? (bool) $count : (int) $count ; } |
Please login to continue.