RulesComponentActionDeriver.php in Rules 8.3
File
src/Plugin/RulesAction/RulesComponentActionDeriver.php
View source
<?php
namespace Drupal\rules\Plugin\RulesAction;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\rules\Engine\ExpressionManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RulesComponentActionDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $storage;
protected $expressionManager;
public function __construct(EntityStorageInterface $storage, ExpressionManagerInterface $expression_manager) {
$this->storage = $storage;
$this->expressionManager = $expression_manager;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('entity_type.manager')
->getStorage('rules_component'), $container
->get('plugin.manager.rules_expression'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$rules_components = $this->storage
->loadMultiple();
foreach ($rules_components as $rules_component) {
$component_config = $rules_component
->get('component');
$expression_definition = $this->expressionManager
->getDefinition($component_config['expression']['id']);
$this->derivatives[$rules_component
->id()] = [
'label' => $this
->t('@expression_type: @label', [
'@expression_type' => $expression_definition['label'],
'@label' => $rules_component
->label(),
]),
'category' => $this
->t('Components'),
'component_id' => $rules_component
->id(),
'context_definitions' => $rules_component
->getContextDefinitions(),
'provides' => $rules_component
->getProvidedContextDefinitions(),
] + $base_plugin_definition;
}
return $this->derivatives;
}
}