You are here

function civicrm_entity_entity_bundle_field_info in CiviCRM Entity 8.3

Implements hook_entity_bundle_field_info().

This ensures CiviCRM Entity entity types have their field config instances across all bundles. It's a copy of the Field module's logic, but clones field config definitions.

See also

field_entity_bundle_field_info().

File

./civicrm_entity.module, line 179
Module file for the CiviCRM Entity module.

Code

function civicrm_entity_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  $result = [];
  if ($entity_type
    ->get('civicrm_entity_ui_exposed') && $entity_type
    ->hasKey('bundle')) {

    // Query by filtering on the ID as this is more efficient than filtering
    // on the entity_type property directly.
    $ids = \Drupal::entityQuery('field_config')
      ->condition('id', $entity_type
      ->id() . '.', 'STARTS_WITH')
      ->execute();

    // Fetch all fields and key them by field name.
    $field_configs = FieldConfig::loadMultiple($ids);

    // Clone the field configs, so that we can modify them and change the
    // target bundle type without manipulating the statically cached entries
    // in the entity storage;
    $cloned_field_configs = array_map(static function (FieldConfigInterface $field) use ($bundle) {
      $cloned = clone $field;
      $cloned
        ->set('bundle', $bundle);
      return $cloned;
    }, $field_configs);
    foreach ($cloned_field_configs as $field_instance) {
      $result[$field_instance
        ->getName()] = $field_instance;
    }
  }
  return $result;
}