function field_has_data

Determines whether a field has any data.

Parameters

$field: A field structure.

Return value

TRUE if the field has data for any entity; FALSE otherwise.

Related topics

4 calls to field_has_data()
Field::saveUpdated in drupal/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php
Saves an updated field definition.
FieldEditForm::buildForm in drupal/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldEditForm.php
Form constructor.
translation_entity_form_field_ui_field_edit_form_alter in drupal/core/modules/translation_entity/translation_entity.module
Implements hook_form_FORM_ID_alter() for 'field_ui_field_edit_form'.
_translation_entity_update_field_translatability in drupal/core/modules/translation_entity/translation_entity.admin.inc
Stores entity translation settings.

File

drupal/core/modules/field/field.module, line 895
Attach custom data fields to Drupal entities.

Code

function field_has_data($field) {
  $columns = array_keys($field['columns']);
  $factory = Drupal::service('entity.query');
  foreach ($field['bundles'] as $entity_type => $bundle) {

    // Entity Query throws an exception if there is no base table.
    $entity_info = entity_get_info($entity_type);
    if (!isset($entity_info['base_table'])) {
      continue;
    }
    $query = $factory
      ->get($entity_type);
    $group = $query
      ->orConditionGroup();
    foreach ($columns as $column) {
      $group
        ->exists($field['field_name'] . '.' . $column);
    }
    $result = $query
      ->condition($group)
      ->count()
      ->accessCheck(FALSE)
      ->range(0, 1)
      ->execute();
    if ($result) {
      return TRUE;
    }
  }
  return FALSE;
}