EventTypeRuleComponentEdit.php in RNG - Events and Registrations 3.x
File
src/Form/EventTypeRuleComponentEdit.php
View source
<?php
namespace Drupal\rng\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Action\ActionManager;
use Drupal\Core\Condition\ConditionManager;
use Drupal\rng\EventManagerInterface;
use Drupal\rng\Entity\EventTypeRuleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
class EventTypeRuleComponentEdit extends FormBase {
protected $actionManager;
protected $conditionManager;
protected $eventManager;
public $eventTypeRule;
public $componentType;
public $componentId;
protected $plugin;
public function __construct(ActionManager $action_manager, ConditionManager $condition_manager, EventManagerInterface $event_manager) {
$this->actionManager = $action_manager;
$this->conditionManager = $condition_manager;
$this->eventManager = $event_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.action'), $container
->get('plugin.manager.condition'), $container
->get('rng.event_manager'));
}
public function getFormId() {
return 'rng_event_type_rule_component_edit';
}
public function buildForm(array $form, FormStateInterface $form_state, EventTypeRuleInterface $rng_event_type_rule = NULL, $component_type = NULL, $component_id = NULL) {
$this->eventTypeRule = $rng_event_type_rule;
$this->componentType = $component_type;
$this->componentId = $component_id;
if ($this->componentType == 'condition') {
$manager = 'conditionManager';
$components = $this->eventTypeRule
->getConditions();
$configuration = $components[$this->componentId];
$plugin_id = $configuration['id'];
unset($configuration['id']);
}
elseif ($this->componentType == 'action') {
$manager = 'actionManager';
$components = $this->eventTypeRule
->getActions();
$plugin_id = $components[$this->componentId]['id'];
$configuration = $components[$this->componentId]['configuration'];
}
else {
return $form;
}
$this->plugin = $this->{$manager}
->createInstance($plugin_id, $configuration);
$form += $this->plugin
->buildConfigurationForm($form, $form_state);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
'#button_type' => 'primary',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->plugin
->submitConfigurationForm($form, $form_state);
$configuration = $this->plugin
->getConfiguration();
if ($this->componentType == 'condition') {
$this->eventTypeRule
->setCondition($this->componentId, $configuration);
}
elseif ($this->componentType == 'action') {
$this->eventTypeRule
->setAction($this->componentId, $configuration);
}
$this->eventTypeRule
->save();
$event_type = $this->eventManager
->eventType($this->eventTypeRule
->getEventEntityTypeId(), $this->eventTypeRule
->getEventBundle());
$this->eventManager
->invalidateEventType($event_type);
}
}