SaveEntityVariableAction.php in Business Rules 8
File
src/Plugin/BusinessRulesAction/SaveEntityVariableAction.php
View source
<?php
namespace Drupal\business_rules\Plugin\BusinessRulesAction;
use Drupal\business_rules\ActionInterface;
use Drupal\business_rules\Entity\Action;
use Drupal\business_rules\Entity\Variable;
use Drupal\business_rules\Events\BusinessRulesEvent;
use Drupal\business_rules\ItemInterface;
use Drupal\business_rules\Plugin\BusinessRulesActionPlugin;
use Drupal\business_rules\VariableObject;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
class SaveEntityVariableAction extends BusinessRulesActionPlugin {
public function __destruct() {
$key_value = $this->util
->getKeyValueExpirable('save_entity_variable');
$key_value
->deleteAll();
}
public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
$settings = [];
if (!$item
->isNew()) {
$settings['variable'] = [
'#type' => 'select',
'#title' => t('Entity variable'),
'#required' => TRUE,
'#description' => t('Entity variable to be saved. Remember to create actions to fill the entity variable fields and execute them before save the entity.'),
'#options' => $this
->getAvailableEmptyVariables($item),
'#default_value' => empty($item
->getSettings('variable')) ? '' : $item
->getSettings('variable'),
];
}
return $settings;
}
protected function getAvailableEmptyVariables(Action $item) {
$variables = Variable::loadMultiple();
$output = [];
foreach ($variables as $variable) {
if ($item
->getTargetEntityType() == $variable
->getTargetEntityType() && $item
->getTargetBundle() == $variable
->getTargetBundle() && $variable
->getType() == 'entity_empty_variable') {
$output[$variable
->id()] = $variable
->label() . ' [' . $variable
->id() . ']';
}
}
return $output;
}
public function buildForm(array &$form, FormStateInterface $form_state) {
unset($form['variables']);
}
public function execute(ActionInterface $action, BusinessRulesEvent $event) {
$variables = $event
->getArgument('variables');
$key_value = $this->util
->getKeyValueExpirable('save_entity_variable');
if ($variables
->count()) {
$variable = $variables
->getVariable($action
->getSettings('variable'));
$entity = $variable ? $variable
->getValue() : FALSE;
if ($entity instanceof EntityInterface) {
$uuid = $entity->uuid->value;
$saved_uuid = $key_value
->get($uuid);
if ($entity->uuid
->getValue() !== $saved_uuid) {
$key_value
->set($uuid, $uuid);
$entity
->save();
$result = [
'#type' => 'markup',
'#markup' => t('Entity: %entity on variable: %variable saved.', [
'%entity' => $entity
->getEntityTypeId(),
'%variable' => $action
->getSettings('variable'),
]),
];
return $result;
}
}
}
}
public function getVariables(ItemInterface $item) {
$variableSet = parent::getVariables($item);
$variable = new VariableObject($item
->getSettings('variable'));
$variableSet
->append($variable);
return $variableSet;
}
}