You are here

DomainMenusSelection.php in Domain Menus for Domains 3.x

File

src/Plugin/EntityReferenceSelection/DomainMenusSelection.php
View source
<?php

namespace Drupal\domain_menus\Plugin\EntityReferenceSelection;

use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\domain\DomainNegotiatorInterface;
use Drupal\domain_access\DomainAccessManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Domain Menus plugin implementation of the Entity Reference Selection plugin.
 *
 * Returns matching nodes with domain assignment to the specified domain.
 *
 * @EntityReferenceSelection(
 *   id = "domain_menus:node",
 *   label = @Translation("Domain Menus"),
 *   entity_types = {"node"},
 *   group = "default",
 *   weight = 1,
 * )
 */
class DomainMenusSelection extends DefaultSelection {

  /**
   * @var \Drupal\domain\DomainNegotiatorInterface
   */
  protected $domainNegotiator;

  /**
   * @var \Drupal\domain_access\DomainAccessManagerInterface
   */
  protected $domainAccessManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, EntityRepositoryInterface $entity_repository, DomainNegotiatorInterface $domain_negotiator, DomainAccessManagerInterface $domain_access_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $module_handler, $current_user, $entity_field_manager, $entity_type_bundle_info, $entity_repository);
    $this->domainNegotiator = $domain_negotiator;
    $this->domainAccessManager = $domain_access_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('module_handler'), $container
      ->get('current_user'), $container
      ->get('entity_field.manager'), $container
      ->get('entity_type.bundle.info'), $container
      ->get('entity.repository'), $container
      ->get('domain.negotiator'), $container
      ->get('domain_access.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    $config = $this
      ->getConfiguration();
    $domain_id = $config['domain_id'] ?? null;
    if ($domain_id == null) {
      return parent::getReferenceableEntities($match, $match_operator, $limit);
    }
    $target_type = $config['target_type'];
    $options = parent::getReferenceableEntities($match, $match_operator);
    $filtered = [];
    $count = 0;

    // Filter target entities by domain.
    foreach ($options as $bundle => &$items) {
      foreach ($items as $entity_id => $label) {
        $entity = $this->entityTypeManager
          ->getStorage($target_type)
          ->load($entity_id);
        $access_values = $this->domainAccessManager
          ->getAccessValues($entity);
        $access_all_value = $this->domainAccessManager
          ->getAllValue($entity);
        if (!($access_values != null && empty($access_all_value) && $access_all_value != null && empty(array_intersect_key($access_values, $domain_id)))) {
          $filtered[$bundle][$entity_id] = $label;
          $count++;
          if ($limit && $count >= $limit) {
            break 2;
          }
        }
      }
    }
    return $filtered;
  }

}

Classes

Namesort descending Description
DomainMenusSelection Domain Menus plugin implementation of the Entity Reference Selection plugin.