public function ConfigurableFieldManager::hasData in Commerce Core 8.2
Checks whether the configurable field has data.
Parameters
\Drupal\entity\BundleFieldDefinition $field_definition: The field definition.
Return value
bool TRUE if data was found, FALSE otherwise.
Throws
\InvalidArgumentException Thrown when given an incomplete field definition (missing name, target entity type ID, or target bundle).
\RuntimeException Thrown when no matching field was found.
Overrides ConfigurableFieldManagerInterface::hasData
File
- src/
ConfigurableFieldManager.php, line 109
Class
Namespace
Drupal\commerceCode
public function hasData(EntityBundleFieldDefinition $field_definition) {
$field_name = $field_definition
->getName();
$entity_type_id = $field_definition
->getTargetEntityTypeId();
$bundle = $field_definition
->getTargetBundle();
if (empty($field_name) || empty($entity_type_id) || empty($bundle)) {
throw new \InvalidArgumentException('The passed $field_definition is incomplete.');
}
// Prevent an EntityQuery crash by first confirming the field exists.
$field = FieldConfig::loadByName($entity_type_id, $bundle, $field_name);
if (empty($field)) {
throw new \RuntimeException(sprintf('The field "%s" does not exist on bundle "%s" of entity type "%s".', $field_name, $bundle, $entity_type_id));
}
$property = $field
->getFieldStorageDefinition()
->getMainPropertyName();
if (!$property) {
// EntityQuery crashes if the field doesn't declare a main property.
// Using the first defined property is a safe fallback for most field
// types except address, where the first property is "langcode" and
// potentially empty.
if ($field_definition
->getType() == 'address') {
$property = 'country_code';
}
else {
$properties = $field
->getFieldStorageDefinition()
->getPropertyNames();
$property = reset($properties);
}
}
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$bundle_key = $storage
->getEntityType()
->getKey('bundle');
$query = $storage
->getQuery();
$query
->accessCheck(FALSE)
->condition($bundle_key, $bundle)
->exists($field_name . '.' . $property)
->range(0, 1);
$result = $query
->execute();
return !empty($result);
}