You are here

function dynamic_entity_reference_get_all_base_fields in Dynamic Entity Reference 8.2

Helper function to return all the base fields.

Return value

array Returns all the entity types with dynamic_entity_reference base field, all the SQL entity types and all the base fields.

2 calls to dynamic_entity_reference_get_all_base_fields()
dynamic_entity_reference_views_data in ./dynamic_entity_reference.views.inc
Implements hook_views_data().
dynamic_entity_reference_views_data_alter in ./dynamic_entity_reference.views.inc
Implements hook_views_data_alter().

File

./dynamic_entity_reference.views.inc, line 303
Provides views data for the dynamic_entity_reference module.

Code

function dynamic_entity_reference_get_all_base_fields() {
  $entity_types = [];
  $sql_entity_types = [];
  $fields_all = [];

  // Ensure origin and target entity types are SQL.
  $entity_manager = \Drupal::entityTypeManager();
  foreach ($entity_manager
    ->getDefinitions() as $entity_type) {

    // \Drupal\views\EntityViewsData class only allows entities with
    // \Drupal\Core\Entity\Sql\SqlEntityStorageInterface.
    if ($entity_type
      ->hasHandlerClass('views_data') && $entity_manager
      ->getStorage($entity_type
      ->id()) instanceof SqlEntityStorageInterface) {
      $sql_entity_types[$entity_type
        ->id()] = $entity_type
        ->id();

      // Only fieldable entities have base fields.
      if ($entity_type
        ->entityClassImplements(FieldableEntityInterface::class)) {
        $entity_types[$entity_type
          ->id()] = $entity_type;
        $entity_field_manager = \Drupal::service('entity_field.manager');
        foreach ($entity_field_manager
          ->getBaseFieldDefinitions($entity_type
          ->id()) as $base_field) {
          if ($base_field
            ->getType() == 'dynamic_entity_reference' && !$base_field
            ->isComputed()) {
            $fields_all[$entity_type
              ->id()][] = $base_field;
          }
        }
      }
    }
  }
  return [
    $entity_types,
    $sql_entity_types,
    $fields_all,
  ];
}