On this page
public function SqlContentEntityStorage::countFieldData
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 1588
Class
- SqlContentEntityStorage
- A content entity database storage implementation.
Namespace
Drupal\Core\Entity\SqlCode
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;
}
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Sql!SqlContentEntityStorage.php/function/SqlContentEntityStorage::countFieldData/8.1.x