AbstractPatternsDeriver.php in UI Patterns 8
File
src/Plugin/Deriver/AbstractPatternsDeriver.php
View source
<?php
namespace Drupal\ui_patterns\Plugin\Deriver;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\TypedDataManager;
use Drupal\ui_patterns\Definition\PatternDefinition;
use Drupal\ui_patterns\TypedData\PatternDataDefinition;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class AbstractPatternsDeriver extends DeriverBase implements PatternsDeriverInterface, ContainerDeriverInterface {
use StringTranslationTrait;
protected $typedDataManager;
protected $messenger;
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;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($base_plugin_id, $container
->get('typed_data_manager'), $container
->get('messenger'));
}
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;
}
protected function getPatternDefinition(array $definition = []) {
return new PatternDefinition($definition);
}
protected function isValidPatternDefinition(PatternDefinition $definition) {
$data_definition = PatternDataDefinition::create();
$violations = $this->typedDataManager
->create($data_definition, $definition
->toArray())
->validate();
if ($violations
->count()) {
$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;
}
}