function FieldHooks::entityFieldStorageInfo

Implements hook_entity_field_storage_info().

Attributes

#[Hook('entity_field_storage_info')]

File

core/modules/field/src/Hook/FieldHooks.php, line 179

Class

FieldHooks
Hook implementations for field.

Namespace

Drupal\field\Hook

Code

public function entityFieldStorageInfo(EntityTypeInterface $entity_type) : array {
  if (\Drupal::entityTypeManager()->getStorage($entity_type->id()) instanceof DynamicallyFieldableEntityStorageInterface) {
    // Query by filtering on the ID as this is more efficient than filtering
    // on the entity_type property directly.
    $ids = \Drupal::entityQuery('field_storage_config')->condition('id', $entity_type->id() . '.', 'STARTS_WITH')
      ->execute();
    // Fetch all fields and key them by field name.
    $field_storages = FieldStorageConfig::loadMultiple($ids);
    $result = [];
    foreach ($field_storages as $field_storage) {
      $result[$field_storage->getName()] = $field_storage;
    }
    return $result;
  }
  return [];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.