You are here

function field_inheritance_entity_bundle_field_info_alter in Field Inheritance 8

Same name and namespace in other branches
  1. 2.0.x field_inheritance.module \field_inheritance_entity_bundle_field_info_alter()

Implements hook_entity_bundle_field_info_alter().

File

./field_inheritance.module, line 37
Contains field_inheritance.module.

Code

function field_inheritance_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {

  // Grab inherited fields just for the current entity type and bundle.
  $inherited_field_ids = \Drupal::entityQuery('field_inheritance')
    ->condition('destinationEntityType', $entity_type
    ->id())
    ->condition('destinationEntityBundle', $bundle)
    ->execute();
  if (!empty($inherited_field_ids)) {
    $inherited_fields = \Drupal::entityTypeManager()
      ->getStorage('field_inheritance')
      ->loadMultiple($inherited_field_ids);
    if (!empty($inherited_fields)) {
      $x = 0;
      foreach ($inherited_fields as $field) {

        // We are only interested in adding computed fields to the destination
        // entity type.
        if ($field
          ->destinationEntityType() !== $entity_type
          ->id()) {
          continue;
        }

        // We are only interested in adding computed fields to the destination
        // entity bundle.
        if ($field
          ->destinationEntityBundle() !== $bundle) {
          continue;
        }

        // Configure the settings array with all the values from the config
        // entity.
        $settings = [
          'id' => $field
            ->idWithoutTypeAndBundle(),
          'source entity type' => $field
            ->sourceEntityType(),
          'source entity bundle' => $field
            ->sourceEntityBundle(),
          'source field' => $field
            ->sourceField(),
          'method' => $field
            ->type(),
          'plugin' => $field
            ->plugin(),
        ];

        // Only set the destination field if one was configured, which will be
        // for inheritance strategies other than inherit.
        if ($field
          ->destinationField()) {
          $settings['destination field'] = $field
            ->destinationField();
        }

        // Grab the configuration of the source field.
        $source_field = FieldConfig::loadByName($field
          ->sourceEntityType(), $field
          ->sourceEntityBundle(), $field
          ->sourceField());
        if (!empty($source_field)) {
          $settings = array_merge($settings, $source_field
            ->getSettings());
          $type = $source_field
            ->getType();
        }
        else {

          // This may be a basefield.
          $basefields = \Drupal::service('entity_field.manager')
            ->getBaseFieldDefinitions($field
            ->sourceEntityType());
          if (!empty($basefields[$field
            ->sourceField()])) {
            $source_field = $basefields[$field
              ->sourceField()];
            $settings = array_merge($settings, $source_field
              ->getSettings());
            $type = $source_field
              ->getType();
          }
          else {
            continue;
          }
        }

        // Set the factory class used to inherit the data from the source.
        $class = '\\Drupal\\field_inheritance\\FieldInheritanceFactory';
        if ($field
          ->plugin() === 'entity_reference_inheritance') {
          $class = '\\Drupal\\field_inheritance\\EntityReferenceFieldInheritanceFactory';
        }

        // Allow developers to override the class to use for a field.
        \Drupal::moduleHandler()
          ->alter('field_inheritance_inheritance_class', $class, $field);

        // Add a computed field for the inherited field to the appropriate
        // destination entity type and bundle.
        $fields[$field
          ->idWithoutTypeAndBundle()] = FieldStorageDefinition::create($type)
          ->setLabel(t('@label', [
          '@label' => $field
            ->label(),
        ]))
          ->setName($field
          ->idWithoutTypeAndBundle())
          ->setDescription(t('The inherited field: @field', [
          '@field' => $field
            ->label(),
        ]))
          ->setComputed(TRUE)
          ->setClass($class)
          ->setSettings($settings)
          ->setTargetEntityTypeId($field
          ->destinationEntityType())
          ->setTargetBundle($field
          ->destinationEntityBundle())
          ->setTranslatable(FALSE)
          ->setRevisionable(FALSE)
          ->setReadOnly(TRUE)
          ->setDisplayConfigurable('view', TRUE)
          ->setDisplayOptions('view', [
          'label' => 'visible',
          'weight' => 50 + $x,
        ]);
        $x++;
      }
    }
  }
}