View source
<?php
namespace Drupal\maestro\Plugin\EngineTasks;
use Drupal\Core\Plugin\PluginBase;
use Drupal\maestro\MaestroEngineTaskInterface;
use Drupal\maestro\Engine\MaestroEngine;
use Drupal\maestro\MaestroTaskTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\maestro\Form\MaestroExecuteInteractive;
class MaestroSetProcessVariableTask extends PluginBase implements MaestroEngineTaskInterface {
use MaestroTaskTrait;
public function __construct($configuration = NULL) {
if (is_array($configuration)) {
$this->processID = $configuration[0];
$this->queueID = $configuration[1];
}
}
public function isInteractive() {
return FALSE;
}
public function shortDescription() {
return t('Set Process Variable');
}
public function description() {
return $this
->t('Sets a process variable to the desired value.');
}
public function getPluginId() {
return 'MaestroSetProcessVariable';
}
public function getTaskColours() {
return '#daa520';
}
public function execute() {
$templateMachineName = MaestroEngine::getTemplateIdFromProcessId($this->processID);
$taskMachineName = MaestroEngine::getTaskIdFromQueueId($this->queueID);
$task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskMachineName);
$spv = $task['data']['spv'];
$variable = $spv['variable'];
$variableValue = $spv['variable_value'];
switch ($spv['method']) {
case 'addsubtract':
$processVar = MaestroEngine::getProcessVariable($variable, $this->processID);
$processVar = $processVar + $variableValue;
MaestroEngine::setProcessVariable($variable, $processVar, $this->processID);
return TRUE;
break;
case 'hardcoded':
MaestroEngine::setProcessVariable($variable, $variableValue, $this->processID);
return TRUE;
break;
case 'bycontentfunction':
$arr = explode(':', $variableValue);
$arguments = explode(',', $arr[1]);
$arguments[] = $this->queueID;
$arguments[] = $this->processID;
$newValue = call_user_func_array($arr[0], $arguments);
MaestroEngine::setProcessVariable($variable, $newValue, $this->processID);
return TRUE;
break;
}
}
public function getExecutableForm($modal, MaestroExecuteInteractive $parent) {
}
public function handleExecuteSubmit(array &$form, FormStateInterface $form_state) {
}
public function getTaskEditForm(array $task, $templateMachineName) {
$spv = $task['data']['spv'];
$form = [
'#markup' => $this
->t('Editing the Process Variable Task'),
];
$form['spv'] = [
'#tree' => TRUE,
'#type' => 'fieldset',
'#title' => $this
->t('Choose which process variable you wish to set and how'),
'#collapsed' => FALSE,
];
$variables = MaestroEngine::getTemplateVariables($templateMachineName);
$options = [];
foreach ($variables as $variableName => $arr) {
$options[$variableName] = $variableName;
}
$form['spv']['variable'] = [
'#type' => 'select',
'#title' => $this
->t('Choose the variable'),
'#required' => TRUE,
'#default_value' => isset($spv['variable']) ? $spv['variable'] : '',
'#options' => $options,
];
$form['spv']['method'] = [
'#type' => 'radios',
'#title' => $this
->t('Method to set variable'),
'#options' => [
'hardcoded' => $this
->t('Hardcoded Value'),
'addsubtract' => $this
->t('Add or Subtract a Value'),
'bycontentfunction' => $this
->t('By Function. Pass "function_name:parameter1,parameter2,..." in variable
as comma separated list. e.g. maestro_spv_content_value_fetch:unique_identifier,field_your_field'),
],
'#default_value' => isset($spv['method']) ? $spv['method'] : '',
'#required' => TRUE,
];
$form['spv']['variable_value'] = [
'#type' => 'textfield',
'#title' => $this
->t('Variable value'),
'#description' => $this
->t('The value you wish to set the variable to based on your METHOD selection above'),
'#default_value' => isset($spv['variable_value']) ? $spv['variable_value'] : '',
'#required' => TRUE,
];
return $form;
}
public function validateTaskEditForm(array &$form, FormStateInterface $form_state) {
$spv = $form_state
->getValue('spv');
switch ($spv['method']) {
case 'addsubtract':
$addSubValue = $spv['variable_value'];
$float = floatval($addSubValue);
if (strcmp($float, $addSubValue) != 0) {
$form_state
->setErrorByName('spv][variable_value', $this
->t('The add or subtract mechanism requires numerical values only.'));
}
break;
case 'hardcoded':
break;
case 'bycontentfunction':
$variable = $spv['variable_value'];
$variable = str_replace(' ', '', $variable);
$arr = explode(':', $variable);
if (!function_exists($arr[0])) {
$form_state
->setErrorByName('spv][variable_value', $this
->t("The function name you provided doesn't exist."));
}
break;
}
}
public function prepareTaskForSave(array &$form, FormStateInterface $form_state, array &$task) {
$spv = $form_state
->getValue('spv');
$task['data']['spv'] = [
'variable' => $spv['variable'],
'method' => $spv['method'],
'variable_value' => $spv['variable_value'],
];
}
public function performValidityCheck(array &$validation_failure_tasks, array &$validation_information_tasks, array $task) {
$data = $task['data']['spv'];
if (array_key_exists('variable', $data) && $data['variable'] == '' || !array_key_exists('variable', $data)) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The SPV Task has not been set up properly. The variable you wish to set is missing and thus the engine will be unable to execute this task.'),
];
}
if (array_key_exists('method', $data) && $data['method'] == '' || !array_key_exists('method', $data)) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The SPV Task has not been set up properly. The method you wish to set the variable with is missing and thus the engine will be unable to execute this task.'),
];
}
if (!array_key_exists('variable_value', $data)) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The SPV Task has not been set up properly. The variable value you wish to set the variable to is missing and thus the engine will be unable to execute this task.'),
];
}
}
public function getTemplateBuilderCapabilities() {
return [
'edit',
'drawlineto',
'removelines',
'remove',
];
}
}