class WebformScheduledTaskForm in Webform Scheduled Tasks 8.2
The scheduled task form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\webform_scheduled_tasks\Form\WebformScheduledTaskForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of WebformScheduledTaskForm
File
- src/
Form/ WebformScheduledTaskForm.php, line 15
Namespace
Drupal\webform_scheduled_tasks\FormView source
class WebformScheduledTaskForm extends EntityForm {
/**
* The associated webform.
*
* @var \Drupal\webform\WebformInterface
*/
protected $webform;
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $webform = NULL) {
$this->webform = $webform;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\webform_scheduled_tasks\Entity\WebformScheduledTaskInterface $schedule */
$schedule = $this->entity;
if ($schedule
->isNew()) {
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Task name'),
'#maxlength' => 255,
'#default_value' => $schedule
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $schedule
->id(),
'#machine_name' => [
'exists' => [
WebformScheduledTask::class,
'load',
],
],
];
$form['task_type'] = [
'#title' => $this
->t('Task to run'),
'#type' => 'select',
'#required' => TRUE,
'#description' => $this
->t('Select the task which should be run when this scheduled task is executed.'),
'#options' => $this
->pluginManagerOptionsList('plugin.manager.webform_scheduled_tasks.task'),
];
$form['result_set_type'] = [
'#title' => $this
->t('Submissions to process'),
'#type' => 'select',
'#required' => TRUE,
'#description' => $this
->t('Select the results which should be used for .'),
'#options' => $this
->pluginManagerOptionsList('plugin.manager.webform_scheduled_tasks.result_set'),
];
$form['webform'] = [
'#type' => 'value',
'#value' => $this->webform
->id(),
];
}
else {
$form['scheduled_task_info'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Scheduled task information'),
'children' => [
'task' => [
'#type' => 'container',
'#markup' => $this
->t('<strong>Task type:</strong> @type', [
'@type' => $schedule
->getTaskPlugin()
->label(),
]),
],
'result_set' => [
'#type' => 'container',
'#markup' => $this
->t('<strong>Result set type:</strong> @type', [
'@type' => $schedule
->getResultSetPlugin()
->label(),
]),
],
'status' => [
'#type' => 'container',
'#markup' => $this
->t('<strong>Status:</strong> @status', [
'@status' => $schedule
->isHalted() ? $this
->t('Halted') : $this
->t('Active'),
]),
],
],
];
if ($schedule
->isHalted()) {
$form['scheduled_task_info']['children']['halted_info'] = [
'message' => [
'#type' => 'container',
'#markup' => $this
->t('<strong>Halted with message:</strong> @message', [
'@message' => $schedule
->getHaltedReason(),
]),
],
'resume' => [
'#type' => 'submit',
'#value' => $this
->t('Resume task'),
'#description' => $this
->t('If the reason this task was halted has been resolved and operations can resume safely, this task may be resumed.'),
'#submit' => [
'::submitForm',
'::resume',
],
],
];
}
$form['schedule_settings'] = [
'#type' => 'details',
'#open' => TRUE,
'#tree' => FALSE,
'#title' => $this
->t('Schedule'),
'children' => [],
];
$form['schedule_settings']['next_run'] = [
'#title' => $this
->t('Next scheduled run'),
'#type' => 'datetime',
'#description' => $this
->t('You may use this field to manually adjust the next time the task will run. If left blank this calculated from the current time.'),
'#default_value' => $schedule
->getNextTaskRunDate() ? DrupalDateTime::createFromTimestamp($schedule
->getNextTaskRunDate()) : NULL,
];
$form['schedule_settings']['interval'] = [
'#type' => 'container',
'#tree' => TRUE,
];
$form['schedule_settings']['interval']['amount'] = [
'#title' => $this
->t('Interval'),
'#type' => 'number',
'#field_prefix' => $this
->t('Run this scheduled task every') . ' ',
'#required' => TRUE,
'#min' => 1,
'#wrapper_attributes' => [
'style' => 'display: inline-block;',
],
'#default_value' => $schedule
->getRunIntervalAmount(),
];
$form['schedule_settings']['interval']['multiplier'] = [
'#type' => 'select',
'#required' => TRUE,
'#wrapper_attributes' => [
'style' => 'display: inline-block;',
],
'#options' => [
60 => $this
->t('Minutes'),
60 * 60 => $this
->t('Hours'),
60 * 60 * 24 => $this
->t('Days'),
60 * 60 * 24 * 7 => $this
->t('Weeks'),
],
'#default_value' => $schedule
->getRunIntervalMultiplier(),
];
$form['task_settings'] = $this
->createPluginForm('task_settings', $this
->t('Task settings'), $schedule
->getTaskPlugin(), $form, $form_state);
$form['result_set_settings'] = $this
->createPluginForm('result_set_settings', $this
->t('Result set settings'), $schedule
->getResultSetPlugin(), $form, $form_state);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
/** @var \Drupal\webform_scheduled_tasks\Entity\WebformScheduledTaskInterface $schedule */
$schedule = $this->entity;
if (!$schedule
->isNew()) {
$this
->validatePluginForm($form['task_settings'], $schedule
->getTaskPlugin(), $form, $form_state);
$this
->validatePluginForm($form['result_set_settings'], $schedule
->getResultSetPlugin(), $form, $form_state);
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform_scheduled_tasks\Entity\WebformScheduledTaskInterface $schedule */
$schedule = $this->entity;
if (!$schedule
->isNew()) {
$this
->submitPluginForm($form['task_settings'], $schedule
->getTaskPlugin(), $form, $form_state);
$this
->submitPluginForm($form['result_set_settings'], $schedule
->getResultSetPlugin(), $form, $form_state);
}
// The next time the task runs isn't a property on the config entity, so it
// must be set manually.
if ($next_run = $form_state
->getValue('next_run')) {
$schedule
->setNextTaskRunDate($next_run
->getTimestamp());
}
// On the first save, when the type plugins are set, redirect to the edit
// form to complete adding the settings for those plugin types.
$redirect_collection = $this->entity
->isNew() ? 'edit-form' : 'collection';
parent::save($form, $form_state);
$this
->messenger()
->addStatus($this
->t('The scheduled task was saved successfully.'));
$form_state
->setRedirect($this->entity
->toUrl($redirect_collection)
->getRouteName(), $this->entity
->toUrl($redirect_collection)
->getRouteParameters());
}
/**
* {@inheritdoc}
*/
public function resume(array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform_scheduled_tasks\Entity\WebformScheduledTaskInterface $schedule */
$schedule = $this->entity;
// Resume the task, but queue it up to run from the current time to the next
// interval.
$this
->messenger()
->addStatus($this
->t('The scheduled task was resumed and will run during the next scheduled interval.'));
$schedule
->incrementTaskRunDateByInterval();
$schedule
->resume();
}
/**
* Create a plugin sub-form.
*
* @param string $key
* The key that will be used to store this plugin form.
* @param string $label
* The label of the section.
* @param \Drupal\Core\Plugin\PluginFormInterface $plugin
* The plugin to add a form for.
* @param array $parent_form
* The parent form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The parent form state.
*
* @return array
* A sub-form.
*/
protected function createPluginForm($key, $label, PluginFormInterface $plugin, array $parent_form, FormStateInterface $form_state) {
$form = [
'#parents' => [
$key,
],
'#type' => 'details',
'#open' => TRUE,
'#tree' => TRUE,
'#title' => $label,
];
$parent_form['#parents'] = [];
$sub_form_state = SubformState::createForSubform($form, $parent_form, $form_state);
$form += $plugin
->buildConfigurationForm($form, $sub_form_state);
return $form;
}
/**
* Validate a plugin form.
*
* @param array $plugin_form
* The plugin form.
* @param \Drupal\Core\Plugin\PluginFormInterface $plugin
* The plugin.
* @param array $parent_form
* The parent form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
protected function validatePluginForm(array $plugin_form, PluginFormInterface $plugin, array $parent_form, FormStateInterface $form_state) {
$sub_form_state = SubformState::createForSubform($plugin_form, $parent_form, $form_state);
$plugin
->validateConfigurationForm($plugin_form, $sub_form_state);
}
/**
* Submit a plugin form.
*/
protected function submitPluginForm($plugin_form, PluginFormInterface $plugin, $parent_form, $form_state) {
$sub_form_state = SubformState::createForSubform($plugin_form, $parent_form, $form_state);
$plugin
->submitConfigurationForm($plugin_form, $sub_form_state);
}
/**
* Get an options list from a plugin manager.
*
* @param string $manager
* The ID of the plugin manager to generate an options list for.
*
* @return array
* An array for options for a select list.
*/
protected function pluginManagerOptionsList($manager) {
/** @var \Drupal\Core\Plugin\DefaultPluginManager $manager */
$manager = \Drupal::service($manager);
return array_column($manager
->getDefinitions(), 'label', 'id');
}
}
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 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
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. | |
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. | |
WebformScheduledTaskForm:: |
protected | property | The associated webform. | |
WebformScheduledTaskForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
WebformScheduledTaskForm:: |
protected | function | Create a plugin sub-form. | |
WebformScheduledTaskForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
WebformScheduledTaskForm:: |
protected | function | Get an options list from a plugin manager. | |
WebformScheduledTaskForm:: |
public | function | ||
WebformScheduledTaskForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
WebformScheduledTaskForm:: |
protected | function | Submit a plugin form. | |
WebformScheduledTaskForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
WebformScheduledTaskForm:: |
protected | function | Validate a plugin form. |