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 MaestroBatchFunctionTask 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('Batch Function');
}
public function description() {
return $this
->t('Batch Function.');
}
public function getPluginId() {
return 'MaestroBatchFunction';
}
public function getTaskColours() {
return '#707070';
}
public function execute() {
$returnValue = FALSE;
$returnStatus = FALSE;
$queueRecord = MaestroEngine::getQueueEntryById($this->queueID);
if ($queueRecord) {
if ($queueRecord->handler != NULL) {
$handler = $queueRecord->handler
->getString();
if (function_exists($handler)) {
$returnStatus = call_user_func_array($handler, [
$this->processID,
$this->queueID,
]);
if (is_array($returnStatus)) {
if (array_key_exists('completion_status', $returnStatus)) {
$this->completionStatus = $returnStatus['completion_status'];
}
if (array_key_exists('execution_status', $returnStatus)) {
$this->executionStatus = $returnStatus['execution_status'];
}
if (array_key_exists('status', $returnStatus)) {
$returnValue = $returnStatus['status'];
}
}
else {
$returnValue = $returnStatus;
}
}
}
else {
$returnValue = TRUE;
}
}
return $returnValue;
}
public function getExecutableForm($modal, MaestroExecuteInteractive $parent) {
}
public function handleExecuteSubmit(array &$form, FormStateInterface $form_state) {
}
public function getTaskEditForm(array $task, $templateMachineName) {
$form = [
'#markup' => t('Batch Function Edit'),
];
$handlers = \Drupal::moduleHandler()
->invokeAll('maestro_batch_handlers', []);
$handler_desc = $this
->t('The batch function name you wish to call.');
if (isset($task['handler']) && isset($handlers[$task['handler']])) {
$handler_desc = $handlers[$task['handler']];
}
$form['handler'] = [
'#type' => 'textfield',
'#title' => $this
->t('Handler'),
'#default_value' => isset($task['handler']) ? $task['handler'] : '',
'#required' => TRUE,
'#autocomplete_route_name' => 'maestro.autocomplete.batch_handlers',
'#ajax' => [
'callback' => [
$this,
'batchFunctionHandlerCallback',
],
'event' => 'autocompleteclose',
'wrapper' => 'handler-ajax-refresh-wrapper',
'progress' => [
'type' => 'throbber',
'message' => NULL,
],
],
];
$form['handler_help_text'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $handler_desc,
'#readonly' => TRUE,
'#attributes' => [
'class' => [
'handler-help-message',
],
'id' => [
'handler-ajax-refresh-wrapper',
],
],
];
return $form;
}
public function batchFunctionHandlerCallback(array &$form, FormStateInterface $form_state) {
$selected_handler = $new_objective_id = $form_state
->getValue('handler');
$handlers = \Drupal::moduleHandler()
->invokeAll('maestro_batch_handlers', []);
if ($selected_handler != '' && !function_exists($selected_handler)) {
$handler_desc = \Drupal::translation()
->translate('This handler form function does not exist.');
}
elseif (isset($handlers[$selected_handler])) {
$handler_desc = $handlers[$selected_handler];
}
else {
$handler_desc = \Drupal::translation()
->translate('The batch function name you wish to call.');
}
$form['handler_help_text'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $handler_desc,
'#readonly' => TRUE,
'#attributes' => [
'class' => [
'handler-help-message',
],
'id' => [
'handler-ajax-refresh-wrapper',
],
],
];
return $form['handler_help_text'];
}
public function validateTaskEditForm(array &$form, FormStateInterface $form_state) {
$handler = $form_state
->getValue('handler');
if (!function_exists($handler)) {
$form_state
->setErrorByName('handler', $this
->t('This handler batch function does not exist.'));
}
}
public function prepareTaskForSave(array &$form, FormStateInterface $form_state, array &$task) {
$task['handler'] = $form_state
->getValue('handler');
}
public function performValidityCheck(array &$validation_failure_tasks, array &$validation_information_tasks, array $task) {
if (array_key_exists('handler', $task) && $task['handler'] == '' || !array_key_exists('handler', $task)) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The handler for the task has not been set. This will cause a failure of the engine to execute'),
];
}
}
public function getTemplateBuilderCapabilities() {
return [
'edit',
'drawlineto',
'removelines',
'remove',
];
}
}