You are here

public function SqlContentEntityStorage::countFieldData in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::countFieldData()

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 1589
Contains \Drupal\Core\Entity\Sql\SqlContentEntityStorage.

Class

SqlContentEntityStorage
A content entity database storage implementation.

Namespace

Drupal\Core\Entity\Sql

Code

public function countFieldData($storage_definition, $as_bool = FALSE) {
  $table_mapping = $this
    ->getTableMapping();
  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;
}