function SqlContentEntityStorage::countFieldData
Same name in other branches
- 9 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::countFieldData()
- 8.9.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::countFieldData()
- 11.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::countFieldData()
File
-
core/
lib/ Drupal/ Core/ Entity/ Sql/ SqlContentEntityStorage.php, line 1735
Class
- SqlContentEntityStorage
- A content entity database storage implementation.
Namespace
Drupal\Core\Entity\SqlCode
public function countFieldData($storage_definition, $as_bool = FALSE) {
// Ensure that the table mapping is instantiated with the passed-in field
// storage definition.
$storage_definitions = $this->fieldStorageDefinitions;
$storage_definitions[$storage_definition->getName()] = $storage_definition;
$table_mapping = $this->getTableMapping($storage_definitions);
if ($table_mapping->requiresDedicatedTableStorage($storage_definition)) {
$is_deleted = $storage_definition->isDeleted();
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', [
'entity_id',
])
->distinct(TRUE);
}
}
elseif ($table_mapping->allowsSharedTableStorage($storage_definition)) {
// Ascertain the table this field is mapped too.
$field_name = $storage_definition->getName();
$table_name = $table_mapping->getFieldTableName($field_name);
$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', [
$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;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.