You are here

class Field in Synonyms 8

Same name in this branch
  1. 8 src/Plugin/Derivative/Field.php \Drupal\synonyms\Plugin\Derivative\Field
  2. 8 src/Plugin/Synonyms/Provider/Field.php \Drupal\synonyms\Plugin\Synonyms\Provider\Field
Same name and namespace in other branches
  1. 2.0.x src/Plugin/Derivative/Field.php \Drupal\synonyms\Plugin\Derivative\Field

Derivative for synonyms provider plugins.

Hierarchy

Expanded class hierarchy of Field

File

src/Plugin/Derivative/Field.php, line 19

Namespace

Drupal\synonyms\Plugin\Derivative
View source
class Field extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The synonyms behavior service.
   *
   * @var \Drupal\synonyms\SynonymsService\BehaviorService
   */
  protected $behaviorService;

  /**
   * The entity type bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The field type to synonyms.
   *
   * @var \Drupal\synonyms\SynonymsService\FieldTypeToSynonyms
   */
  protected $fieldTypeToSynonyms;

  /**
   * Field constructor.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, BehaviorService $behavior_service, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager, FieldTypeToSynonyms $field_type_to_synonyms) {
    $this->entityTypeManager = $entity_type_manager;
    $this->behaviorService = $behavior_service;
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
    $this->entityFieldManager = $entity_field_manager;
    $this->fieldTypeToSynonyms = $field_type_to_synonyms;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('synonyms.behaviors'), $container
      ->get('entity_type.bundle.info'), $container
      ->get('entity_field.manager'), $container
      ->get('synonyms.provider.field_type_to_synonyms'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $field_type_to_property_map = $this->fieldTypeToSynonyms
      ->getSimpleFieldTypeToPropertyMap();
    foreach ($this->behaviorService
      ->getBehaviorServices() as $service_id => $behavior) {
      foreach ($this->entityTypeManager
        ->getDefinitions() as $entity_type) {
        if ($entity_type instanceof ContentEntityType) {
          foreach ($this->entityTypeBundleInfo
            ->getBundleInfo($entity_type
            ->id()) as $bundle => $bundle_info) {
            $base_fields = $this->entityFieldManager
              ->getBaseFieldDefinitions($entity_type
              ->id());
            $fields = $this->entityFieldManager
              ->getFieldDefinitions($entity_type
              ->id(), $bundle);
            switch ($base_plugin_definition['id']) {
              case 'base_field':
                $fields = $base_fields;
                break;
              case 'field':
                $fields = array_diff_key($fields, $base_fields);
                break;
            }
            foreach ($fields as $field) {
              if ($field
                ->getName() != 'synonyms' && isset($field_type_to_property_map[$field
                ->getType()])) {
                $derivative_name = implode('_', [
                  $service_id,
                  $entity_type
                    ->id(),
                  $bundle,
                  $field
                    ->getName(),
                ]);
                $this->derivatives[$derivative_name] = $base_plugin_definition;
                $this->derivatives[$derivative_name]['label'] = $this
                  ->t('@behavior on @field', [
                  '@behavior' => $behavior['service']
                    ->getTitle(),
                  '@field' => $field
                    ->getLabel(),
                ]);
                $this->derivatives[$derivative_name]['synonyms_behavior_service'] = $service_id;
                $this->derivatives[$derivative_name]['controlled_entity_type'] = $entity_type
                  ->id();
                $this->derivatives[$derivative_name]['controlled_bundle'] = $bundle;
                $this->derivatives[$derivative_name]['field'] = $field
                  ->getName();
              }
            }
          }
        }
      }
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
Field::$behaviorService protected property The synonyms behavior service.
Field::$entityFieldManager protected property The entity field manager.
Field::$entityTypeBundleInfo protected property The entity type bundle info.
Field::$entityTypeManager protected property The entity type manager.
Field::$fieldTypeToSynonyms protected property The field type to synonyms.
Field::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
Field::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
Field::__construct public function Field constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.