commands-1.x-dev/modules/tasks/src/Form/TaskTypeForm.php
modules/tasks/src/Form/TaskTypeForm.php
<?php
declare(strict_types=1);
namespace Drupal\tasks\Form;
use Drupal\commands\Entity\CommandType;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tasks\Entity\TaskType;
/**
* Form handler for tasks type forms.
*/
final class TaskTypeForm extends BundleEntityFormBase {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state): array {
$form = parent::form($form, $form_state);
$task_type = $this->getEntity();
if ($this->operation === 'edit') {
$form['#title'] = $this->t('Edit %label task type', ['%label' => $this->entity->label()]);
}
if ($task_type->isNew()) {
$plugin = \Drupal::routeMatch()->getParameter('plugin');
if (empty($plugin)) {
$this->messenger()->addError($this->t('Click "Enable" under "Disabled task types" to add a new task type.'));
$this->redirect('entity.task_type.collection')->send();
return $form;
}
else {
$task_type->setPlugin($plugin);
}
}
else {
$plugin = $task_type->id();
}
$definition = \Drupal::service('plugin.manager.task_type')->getDefinition($plugin);
try {
$task_type->setPlugin($plugin);
$task_plugin = $task_type->getPlugin();
$commands = $task_plugin->commands();
$required_commands = array_keys($commands);
$commands_list = [];
foreach ($commands as $command_type => $params) {
$command = CommandType::load($command_type);
if ($command) {
$active_commands[] = $command_type;
$commands_list[] = $command->toLink(null, 'edit-form');
}
}
if (count($required_commands) > count($active_commands)) {
// @TODO: These errors are not showing.
$this->messenger()->addError(t('Task type cannot be enabled because the following commands are not enabled: :commands', [
':commands' => implode(', ', array_diff($required_commands, $active_commands)),
]));
$this->redirect('entity.task_type.collection')->send();
return $form;
}
$form['commands'] = [
'#title' => t('Commands'),
'#theme' => 'item_list',
'#items' => $commands_list,
];
$form['help'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => ['class' => 'form-item__description'],
'#value' => t('These commands will be run for every created task.'),
];
}
catch (PluginNotFoundException $e) {
$this->messenger()->addError($e->getMessage());
$this->redirect('entity.task_type.collection')->send();
return $form;
}
$form['label'] = [
'#title' => $this->t('Label'),
'#type' => 'textfield',
'#default_value' => $task_type->label() ?: $definition['label'],
'#description' => $this->t('The text to display for this task.'),
'#required' => TRUE,
'#size' => 30,
];
$form['description'] = [
'#title' => $this->t('Description'),
'#type' => 'textarea',
'#default_value' => $task_type->description() ?: $definition['description'],
'#description' => $this->t('Text to display to the user when choosing what task to run.'),
'#size' => 30,
];
$form['id'] = [
'#type' => 'value',
'#default_value' => $task_type->id(),
];
return $this->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state): array {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->t('Save tasks type');
$actions['delete']['#value'] = $this->t('Delete tasks type');
return $actions;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var TaskType $entity_type */
$entity_type = $this->entity;
$entity_type->set('id', $entity_type->getPlugin()->getPluginId());
if (empty($entity_type->get('label'))) {
$entity_type->set('label', $entity_type->getPlugin()->label());
}
$status = $entity_type->save();
$t_args = ['%name' => $entity_type->label()];
if ($status == SAVED_UPDATED) {
$message = $this->t('The task type %name has been updated.', $t_args);
}
elseif ($status == SAVED_NEW) {
$message = $this->t('The task type %name has been enabled.', $t_args);
}
$this->messenger()->addStatus($message);
$form_state->setRedirectUrl($entity_type->toUrl('collection'));
}
}
