RuleComponent.php in RNG - Events and Registrations 8.2
File
src/Entity/RuleComponent.php
View source
<?php
namespace Drupal\rng\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
class RuleComponent extends ContentEntityBase implements RuleComponentInterface {
public function getRule() {
return $this
->get('rule')->entity;
}
public function setRule(RuleInterface $rule) {
$this
->set('rule', [
'entity' => $rule,
]);
return $this;
}
public function getType() {
return $this
->get('type')->value;
}
public function setType($type) {
$this
->set('type', $type);
return $this;
}
public function getPluginId() {
return $this
->get('action')->value;
}
public function setPluginId($action_id) {
$this
->set('action', $action_id);
return $this;
}
public function getConfiguration() {
return $this
->get('configuration')
->first() ? $this
->get('configuration')
->first()
->getValue() : [];
}
public function setConfiguration(array $configuration) {
$this
->set('configuration', $configuration);
return $this;
}
public function createInstance() {
if (in_array($this
->getType(), [
'action',
'condition',
])) {
$manager = \Drupal::service('plugin.manager.' . $this
->getType());
return $manager
->createInstance($this
->getPluginId(), $this
->getConfiguration());
}
else {
throw new \Exception('Invalid RuleComponent type.');
}
}
public function execute(array $context) {
$this
->createInstance()
->execute($context);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Rule Component ID'))
->setDescription(t('The rule component ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['rule'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Rule ID'))
->setDescription(t('The rule ID.'))
->setReadOnly(TRUE)
->setRequired(TRUE)
->setSetting('target_type', 'rng_rule');
$fields['type'] = BaseFieldDefinition::create('string')
->setLabel(t('Type'))
->setDescription(t('Whether this is an action or condition.'))
->setRequired(TRUE)
->setReadOnly(TRUE);
$fields['action'] = BaseFieldDefinition::create('string')
->setLabel(t('Plugin ID'))
->setDescription(t('The plugin ID.'))
->setRequired(TRUE)
->setReadOnly(TRUE);
$fields['configuration'] = BaseFieldDefinition::create('map')
->setLabel(t('Configuration'))
->setDescription(t('The component configuration.'))
->setRequired(TRUE);
return $fields;
}
}
Classes
Name |
Description |
RuleComponent |
Defines a event rule plugin instance entity: a condition or action. |