class MaestroTemplateBuilderAddNew in Maestro 8.2
Same name and namespace in other branches
- 3.x modules/maestro_template_builder/src/Form/MaestroTemplateBuilderAddNew.php \Drupal\maestro_template_builder\Form\MaestroTemplateBuilderAddNew
Maestro Template Builder Add New form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\maestro_template_builder\Form\MaestroTemplateBuilderAddNew
Expanded class hierarchy of MaestroTemplateBuilderAddNew
1 string reference to 'MaestroTemplateBuilderAddNew'
- maestro_template_builder.routing.yml in modules/
maestro_template_builder/ maestro_template_builder.routing.yml - modules/maestro_template_builder/maestro_template_builder.routing.yml
File
- modules/
maestro_template_builder/ src/ Form/ MaestroTemplateBuilderAddNew.php, line 16
Namespace
Drupal\maestro_template_builder\FormView source
class MaestroTemplateBuilderAddNew extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'template_add_new';
}
/**
* {@inheritdoc}
*/
public static function exists($submitted_value, array $element, FormStateInterface $form_state) {
$templateMachineName = $form_state
->getValue('template_machine_name');
$template = MaestroEngine::getTemplate($templateMachineName);
$tasks = $template->tasks;
if (array_key_exists($submitted_value, $tasks) == TRUE) {
return TRUE;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Everything in the base form is mandatory. nothing really to check here.
}
/**
* {@inheritdoc}
*/
public function cancelForm(array &$form, FormStateInterface $form_state) {
// We cancel the modal dialog by first sending down the form's error state as the cancel is a submit.
// we then close the modal.
$response = new AjaxResponse();
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$response
->addCommand(new HtmlCommand('#template-add-new-form', $form));
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Do we have any errors? if so, handle them by returning the form's HTML and replacing the form.
if ($form_state
->getErrors()) {
unset($form['#prefix'], $form['#suffix']);
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$response = new AjaxResponse();
// Replaces the form HTML with the validated HTML.
$response
->addCommand(new HtmlCommand('#template-add-new-form', $form));
return $response;
}
else {
$templateMachineName = $form_state
->getValue('template_machine_name');
$id = $form_state
->getValue('task_machine_name');
$label = $form_state
->getValue('task_label');
$type = $form_state
->getValue('choose_task');
// Create the new task entry in the template.
$template = MaestroEngine::getTemplate($templateMachineName);
$this_task = MaestroEngine::getPluginTask($type);
$capabilities = $this_task
->getTemplateBuilderCapabilities();
foreach ($capabilities as $key => $c) {
$capabilities[$key] = 'maestro_template_' . $c;
}
$template->tasks[$id] = [
'id' => $id,
'label' => $label,
'tasktype' => $type,
'nextstep' => '',
'nextfalsestep' => '',
'top' => '15',
'left' => '15',
'assignby' => 'fixed',
'assignto' => '',
'raphael' => '',
'to' => '',
'pointedfrom' => '',
'falsebranch' => '',
'lines' => [],
];
// We need to have this template validated now.
$template->validated = FALSE;
$template
->save();
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('signalValidationRequired', []));
$response
->addCommand(new FireJavascriptCommand('addNewTask', [
'id' => $id,
'label' => $label,
'type' => $type,
'capabilities' => $capabilities,
'uilabel' => $this
->t(str_replace('Maestro', '', $type)),
]));
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
}
/**
* Ajax callback for add-new-form button click.
*/
public function buildForm(array $form, FormStateInterface $form_state, $templateMachineName = '') {
$template = MaestroEngine::getTemplate($templateMachineName);
// Need to validate this template to ensure that it exists.
if ($template == NULL) {
$form = [
'#title' => $this
->t('Error!'),
'#markup' => $this
->t("The template you are attempting to add a task to doesn't exist"),
];
return $form;
}
$form = [
'#title' => $this
->t('Add a new task'),
'#markup' => '<div id="maestro-template-error" class="messages messages--error">dddd</div>',
];
$form['#prefix'] = '<div id="template-add-new-form">';
$form['#suffix'] = '</div>';
// Add all the task types here.
$manager = \Drupal::service('plugin.manager.maestro_tasks');
$plugins = $manager
->getDefinitions();
$options = [];
foreach ($plugins as $plugin) {
if ($plugin['id'] != 'MaestroStart') {
$task = $manager
->createInstance($plugin['id'], [
0,
0,
]);
$options[$task
->getPluginId()] = $task
->shortDescription();
}
}
$form['template_machine_name'] = [
'#type' => 'hidden',
'#title' => $this
->t('machine name of the template'),
'#default_value' => $templateMachineName,
'#required' => TRUE,
];
$form['task_label'] = [
'#type' => 'textfield',
'#title' => $this
->t('The Label for the new task'),
'#required' => TRUE,
];
$form['task_machine_name'] = [
'#type' => 'machine_name',
'#title' => $this
->t('A unique name for this task'),
'#machine_name' => [
'exists' => [
get_class($this),
'exists',
],
],
];
$form['choose_task'] = [
'#type' => 'radios',
'#options' => $options,
'#title' => $this
->t('Which task would you like to create?'),
'#required' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['create'] = [
'#type' => 'submit',
'#value' => $this
->t('Create Task'),
'#required' => TRUE,
'#submit' => [],
'#ajax' => [
'callback' => '::submitForm',
'event' => 'click',
],
];
$form['actions']['cancel'] = [
'#type' => 'button',
'#value' => $this
->t('Cancel'),
'#required' => TRUE,
'#ajax' => [
'callback' => [
$this,
'cancelForm',
],
'wrapper' => '',
],
];
return $form;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MaestroTemplateBuilderAddNew:: |
public | function |
Ajax callback for add-new-form button click. Overrides FormInterface:: |
|
MaestroTemplateBuilderAddNew:: |
public | function | ||
MaestroTemplateBuilderAddNew:: |
public static | function | ||
MaestroTemplateBuilderAddNew:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MaestroTemplateBuilderAddNew:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
MaestroTemplateBuilderAddNew:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |