You are here

abstract class AbstractPatternsDeriver in UI Patterns 8

Class AbstractPatternsDeriver.

@package Drupal\ui_patterns\Deriver

Hierarchy

Expanded class hierarchy of AbstractPatternsDeriver

File

src/Plugin/Deriver/AbstractPatternsDeriver.php, line 19

Namespace

Drupal\ui_patterns\Plugin\Deriver
View source
abstract class AbstractPatternsDeriver extends DeriverBase implements PatternsDeriverInterface, ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * Typed data manager service.
   *
   * @var \Drupal\Core\TypedData\TypedDataManager
   */
  protected $typedDataManager;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * AbstractPatternsDeriver constructor.
   */
  public function __construct($base_plugin_id, TypedDataManager $typed_data_manager, MessengerInterface $messenger) {
    $this->basePluginId = $base_plugin_id;
    $this->typedDataManager = $typed_data_manager;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($base_plugin_id, $container
      ->get('typed_data_manager'), $container
      ->get('messenger'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    foreach ($this
      ->getPatterns() as $pattern) {
      $pattern
        ->setDeriver($base_plugin_definition['deriver']);
      $pattern
        ->setClass($base_plugin_definition['class']);
      if ($this
        ->isValidPatternDefinition($pattern)) {
        $this->derivatives[$pattern
          ->id()] = $pattern;
      }
    }
    return $this->derivatives;
  }

  /**
   * Get pattern data object.
   *
   * @param array $definition
   *   Pattern definition array.
   *
   * @return \Drupal\ui_patterns\Definition\PatternDefinition
   *   Pattern definition object.
   */
  protected function getPatternDefinition(array $definition = []) {
    return new PatternDefinition($definition);
  }

  /**
   * Validate pattern definition.
   *
   * @param \Drupal\ui_patterns\Definition\PatternDefinition $definition
   *   Pattern definition.
   *
   * @return bool
   *   Whereas current pattern definition is valid or not.
   */
  protected function isValidPatternDefinition(PatternDefinition $definition) {
    $data_definition = PatternDataDefinition::create();
    $violations = $this->typedDataManager
      ->create($data_definition, $definition
      ->toArray())
      ->validate();
    if ($violations
      ->count()) {

      /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
      $this->messenger
        ->addError($this
        ->t("Pattern ':id' is skipped because of the following validation error(s):", [
        ':id' => $definition
          ->id(),
      ]));
      foreach ($violations as $violation) {
        $message = $this
          ->t('Validation error on ":id.:property": :message', [
          ':id' => $definition
            ->id(),
          ':property' => $violation
            ->getPropertyPath(),
          ':message' => $violation
            ->getMessage(),
        ]);
        $this->messenger
          ->addError($message);
      }
      return FALSE;
    }
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractPatternsDeriver::$messenger protected property The messenger.
AbstractPatternsDeriver::$typedDataManager protected property Typed data manager service.
AbstractPatternsDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create 1
AbstractPatternsDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
AbstractPatternsDeriver::getPatternDefinition protected function Get pattern data object.
AbstractPatternsDeriver::isValidPatternDefinition protected function Validate pattern definition.
AbstractPatternsDeriver::__construct public function AbstractPatternsDeriver constructor. 1
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
PatternsDeriverInterface::getPatterns public function Get pattern definition objects. 1
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.