You are here

class BehaviorService in Synonyms 8

Same name and namespace in other branches
  1. 2.0.x src/SynonymsService/BehaviorService.php \Drupal\synonyms\SynonymsService\BehaviorService

Collect all known synonyms behavior services.

Collect all known synonyms behavior services during dependency injection container compilation.

Hierarchy

Expanded class hierarchy of BehaviorService

9 files declare their use of BehaviorService
AutocompleteService.php in src/SynonymsService/Behavior/AutocompleteService.php
EntityReferenceField.php in src/Plugin/Derivative/EntityReferenceField.php
Field.php in src/Plugin/Derivative/Field.php
SearchService.php in synonyms_search/src/SynonymsService/Behavior/SearchService.php
SelectService.php in src/SynonymsService/Behavior/SelectService.php

... See full list

1 string reference to 'BehaviorService'
synonyms.services.yml in ./synonyms.services.yml
synonyms.services.yml
1 service uses BehaviorService
synonyms.behaviors in ./synonyms.services.yml
Drupal\synonyms\SynonymsService\BehaviorService

File

src/SynonymsService/BehaviorService.php, line 16

Namespace

Drupal\synonyms\SynonymsService
View source
class BehaviorService implements ContainerInjectionInterface {

  /**
   * The behavior services.
   *
   * @var array
   */
  protected $behaviorServices;

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

  /**
   * BehaviorService constructor.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->behaviorServices = [];
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'));
  }

  /**
   * Add a new discovered behavior service.
   *
   * @param \Drupal\synonyms\SynonymsService\Behavior\SynonymsBehaviorInterface $behavior_service
   *   Behavior service object that was discovered and should be added into the
   *   list of known ones.
   * @param string $id
   *   Service ID that corresponds to this behavior service.
   */
  public function addBehaviorService(SynonymsBehaviorInterface $behavior_service, $id) {
    if (!isset($this->behaviorServices[$id])) {
      $this->behaviorServices[$id] = [
        'service' => $behavior_service,
      ];
    }
  }

  /**
   * Array of known synonyms behavior services.
   *
   * @return array
   *   The return value
   */
  public function getBehaviorServices() {
    return $this->behaviorServices;
  }

  /**
   * Get a synonyms behavior service.
   *
   * @param string $behavior_service_id
   *   ID of the service to retrieve.
   *
   * @return array
   *   Array of information about the behavior service. The array will have the
   *   following structure:
   *   - service: (SynonymsBehaviorInterface) Initiated behavior service
   */
  public function getBehaviorService($behavior_service_id) {
    return isset($this->behaviorServices[$behavior_service_id]) ? $this->behaviorServices[$behavior_service_id] : NULL;
  }

  /**
   * Get a list of enabled synonym providers for a requested synonyms behavior.
   *
   * @param string $synonyms_behavior
   *   ID of the synonyms behavior services whose enabled providers should be
   *   returned.
   * @param string $entity_type
   *   Entity type for which to conduct the search.
   * @param string|array $bundle
   *   Single bundle or an array of them for which to conduct the search. If
   *   null is given, then no restrictions are applied on bundle level.
   *
   * @return \Drupal\synonyms\Entity\Synonym[]
   *   The array of enabled synonym providers
   */
  public function getSynonymConfigEntities($synonyms_behavior, $entity_type, $bundle) {
    $entities = [];
    if (is_scalar($bundle) && !is_null($bundle)) {
      $bundle = [
        $bundle,
      ];
    }
    foreach ($this->entityTypeManager
      ->getStorage('synonym')
      ->loadMultiple() as $entity) {
      $provider_instance = $entity
        ->getProviderPluginInstance();
      $provider_definition = $provider_instance
        ->getPluginDefinition();
      if ($provider_definition['synonyms_behavior_service'] == $synonyms_behavior && $provider_definition['controlled_entity_type'] == $entity_type && (!is_array($bundle) || in_array($provider_definition['controlled_bundle'], $bundle))) {
        $entities[] = $entity;
      }
    }
    return $entities;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BehaviorService::$behaviorServices protected property The behavior services.
BehaviorService::$entityTypeManager protected property The entity type manager.
BehaviorService::addBehaviorService public function Add a new discovered behavior service.
BehaviorService::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
BehaviorService::getBehaviorService public function Get a synonyms behavior service.
BehaviorService::getBehaviorServices public function Array of known synonyms behavior services.
BehaviorService::getSynonymConfigEntities public function Get a list of enabled synonym providers for a requested synonyms behavior.
BehaviorService::__construct public function BehaviorService constructor.