acquia_vwo-1.0.x-dev/src/Service/Condition/ConditionResolver.php

src/Service/Condition/ConditionResolver.php
<?php

namespace Drupal\acquia_vwo\Service\Condition;

use Drupal\Core\Condition\ConditionAccessResolverTrait;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Condition\ConditionPluginCollection;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;

/**
 * Class to manage and evaluate conditions.
 */
class ConditionResolver {

  use ConditionAccessResolverTrait;

  /**
   * The list of condition used by this module.
   *
   * @var string[]
   */
  protected $conditionsMapping = [
    'request_path' => [
      'context_mapping' => [],
    ],
    'user_role' => [
      'context_mapping' => [
        'user' => '@user.current_user_context:current_user',
      ],
    ],
    'entity_bundle:node' => [
      'context_mapping' => [
        'node' => '@node.node_route_context:node',
      ],
    ],
  ];

  /**
   * The insertion condition collection.
   *
   * @var \Drupal\Core\Condition\ConditionPluginCollection|null
   */
  protected $conditionCollection;

  /**
   * The condition plugin manager.
   *
   * @var \Drupal\Core\Condition\ConditionManager
   */
  protected $conditionManager;

  /**
   * The Context Repository Service.
   *
   * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
   */
  private ContextRepositoryInterface $contextRepository;

  /**
   * The Context Handler.
   *
   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
   */
  private ContextHandlerInterface $contextHandler;

  /**
   * Constructs a ContainerForm object.
   *
   * @param \Drupal\Core\Condition\ConditionManager $condition_manager
   *   The ConditionManager for building the insertion conditions.
   * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contextRepository
   *   Context repository.
   * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $contextHandler
   *   Context handler.
   */
  public function __construct(
    ConditionManager $condition_manager,
    ContextRepositoryInterface $contextRepository,
    ContextHandlerInterface $contextHandler,
  ) {
    $this->conditionManager = $condition_manager;
    $this->contextRepository = $contextRepository;
    $this->contextHandler = $contextHandler;
  }

  /**
   * Creates instances of config conditions.
   *
   * @param array $visibility_conditions
   *   Array of visibility conditions.
   *
   * @return array
   *   A list of config conditions.
   */
  public function getConfigConditions($visibility_conditions) {
    $conditions = [];
    $configs = array_merge($this->conditionsMapping, $visibility_conditions);
    foreach ($configs as $condition_id => $configuration) {
      $condition = $this->conditionManager->createInstance($condition_id, $configuration ?? []);
      $conditions[$condition_id] = $condition;
    }
    return $conditions;
  }

  /**
   * Evaluates visibility conditions.
   *
   * @param array $visibility_conditions
   *   An array of visibility conditions.
   *
   * @return bool
   *   True if all evaluations are true, else false.
   */
  public function evaluateConditions(array $visibility_conditions) {
    $conditions = [];
    foreach ($this->getConditions($visibility_conditions) as $condition_id => $condition) {
      if ($condition instanceof ContextAwarePluginInterface) {
        $this->setConditionContextMapping($condition);
      }
      $conditions[$condition_id] = $condition;
    }
    return $this->resolveConditions($conditions, 'and');
  }

  /**
   * Returns the set of insertion conditions.
   *
   * @param array $visibility_conditions
   *   Array of visibility conditions.
   *
   * @return \Drupal\Core\Condition\ConditionPluginCollection
   *   A collection of configured condition plugins.
   */
  public function getConditions($visibility_conditions) {
    if ($this->conditionCollection === NULL) {
      $this->conditionCollection = new ConditionPluginCollection($this->conditionManager, $visibility_conditions);
    }
    return $this->conditionCollection;
  }

  /**
   * Add context mapping and data to the conditions.
   *
   * @param \Drupal\Core\Plugin\ContextAwarePluginInterface $condition
   *   The condition.
   *
   * @throws \Drupal\Component\Plugin\Exception\ContextException
   * @throws \Drupal\Component\Plugin\Exception\MissingValueContextException
   */
  private function setConditionContextMapping(ContextAwarePluginInterface $condition) {
    $id = $condition->getPluginId();
    if (isset($this->conditionsMapping[$id])) {
      $condition->setContextMapping($this->conditionsMapping[$id]['context_mapping']);
      $context_ids = array_values($condition->getContextMapping());
      $contexts = $this->contextRepository->getRuntimeContexts($context_ids);
      foreach ($contexts as $key => $context) {
        // Remove contexts without context value.
        if (empty($context->getContextValue())) {
          unset($contexts[$key]);
        };
      }
      if (!empty($contexts)) {
        $this->contextHandler->applyContextMapping($condition, $contexts);
      }
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc