class FormsStepsEditForm in Forms Steps 8
Provides a form to edit a Forms Steps.
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\forms_steps\Form\FormsStepsEditForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of FormsStepsEditForm
File
- src/
Form/ FormsStepsEditForm.php, line 18
Namespace
Drupal\forms_steps\FormView source
class FormsStepsEditForm extends EntityForm {
/**
* PathValidatorInterface.
*
* @var \Drupal\Core\Path\PathValidatorInterface
*/
protected $pathValidator;
/**
* RouteProvider.
*
* @var \Drupal\Core\Routing\RouteProvider
*/
protected $routeProvider;
/**
* Class constructor.
*/
public function __construct(PathValidatorInterface $path_validator, RouteProvider $route_provider) {
$this->pathValidator = $path_validator;
$this->routeProvider = $route_provider;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static($container
->get('path.validator'), $container
->get('router.route_provider'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/* @var \Drupal\forms_steps\FormsStepsInterface $forms_steps */
$forms_steps = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#default_value' => $this->entity
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#description' => $this
->t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
'#disabled' => !$this->entity
->isNew(),
'#default_value' => $this->entity
->id(),
'#machine_name' => [
'exists' => [
$this,
'exists',
],
'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
'source' => [
'label',
],
'error' => $this
->t('The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".'),
],
];
$header = [
'step' => $this
->t('Step'),
'form_id' => $this
->t('Entity bundle'),
'form_mode' => $this
->t('Form mode'),
'weight' => $this
->t('Weight'),
'operations' => $this
->t('Operations'),
];
$form['steps_container'] = [
'#type' => 'details',
'#title' => $this
->t('Steps'),
'#open' => TRUE,
'#collapsible' => 'FALSE',
];
$form['steps_container']['steps'] = [
'#type' => 'table',
'#header' => $header,
'#title' => $this
->t('Steps'),
'#empty' => $this
->t('There are no steps yet.'),
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'step-weight',
],
],
];
$steps = $forms_steps
->getSteps();
// Warn the user if there are no steps.
if (empty($steps)) {
$this
->messenger()
->addWarning($this
->t('This Forms Steps has no steps and will be disabled until there is at least one, <a href=":add-step">add a new step.</a>', [
':add-step' => $forms_steps
->toUrl('add-step-form')
->toString(),
]));
}
foreach ($steps as $step) {
$links = [
'edit' => [
'title' => $this
->t('Edit'),
'url' => Url::fromRoute('entity.forms_steps.edit_step_form', [
'forms_steps' => $forms_steps
->id(),
'forms_steps_step' => $step
->id(),
]),
'attributes' => [
'aria-label' => $this
->t('Edit @step step', [
'@step' => $step
->label(),
]),
],
],
];
if ($this->entity
->access('delete-step:' . $step
->id())) {
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.forms_steps.delete_step_form', [
'forms_steps' => $forms_steps
->id(),
'forms_steps_step' => $step
->id(),
]),
'attributes' => [
'aria-label' => $this
->t('Delete @step step', [
'@step' => $step
->label(),
]),
],
];
}
$form['steps_container']['steps'][$step
->id()] = [
'#attributes' => [
'class' => [
'draggable',
],
],
'step' => [
'#markup' => $step
->label(),
],
'form_id' => [
'#markup' => $step
->EntityBundle(),
],
'form_mode' => [
'#markup' => $step
->formMode(),
],
'#weight' => $step
->weight(),
'weight' => [
'#type' => 'weight',
'#title' => $this
->t('Weight for @title', [
'@title' => $step
->label(),
]),
'#title_display' => 'invisible',
'#default_value' => $step
->weight(),
'#attributes' => [
'class' => [
'step-weight',
],
],
'#delta' => 100,
],
'operations' => [
'#type' => 'operations',
'#links' => $links,
],
];
}
$form['steps_container']['step_add'] = [
'#markup' => $forms_steps
->toLink($this
->t('Add a new step'), 'add-step-form')
->toString(),
];
$form['progress_container'] = [
'#type' => 'details',
'#title' => $this
->t('Progress bar'),
'#description' => $this
->t('Define new progress steps here and assign steps to them to generate a progress bar block available for display.<br/>To configure the block display, please go to the <a href=":block-layout-url">block layout section</a>.<br/><br/><em>Note that any link set to be displayed on the first step will not be rendered, as Forms Steps starts to store progression on the first step submission.</em>', [
':block-layout-url' => Url::fromRoute('block.admin_display')
->toString(),
]),
'#open' => TRUE,
'#collapsible' => 'FALSE',
];
$header = [
'progress_step' => $this
->t('progress step'),
'routes' => $this
->t('Active for steps'),
'link' => $this
->t('Link'),
'link_visibility' => $this
->t('Link visibility'),
'weight' => $this
->t('Weight'),
'operations' => $this
->t('Operations'),
];
$form['progress_container']['progress_steps'] = [
'#type' => 'table',
'#header' => $header,
'#title' => $this
->t('progress steps'),
'#empty' => $this
->t('There are no progress steps yet.'),
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'progress-state-weight',
],
],
];
// Warn the user if there are no steps.
if (empty($steps)) {
$form['progress_container']['no_steps'] = [
'#markup' => $this
->t('This Forms Steps has no steps, no progress step can be added until there is at least one, <a href=":add-step">add a new step.</a>', [
':add-step' => $forms_steps
->toUrl('add-step-form')
->toString(),
]),
];
}
else {
$progress_steps = $forms_steps
->getProgressSteps();
foreach ($progress_steps as $progress_step) {
// Defines admin links.
$links = [
'edit' => [
'title' => $this
->t('Edit'),
'url' => Url::fromRoute('entity.forms_steps.edit_progress_step_form', [
'forms_steps' => $forms_steps
->id(),
'forms_steps_progress_step' => $progress_step
->id(),
]),
'attributes' => [
'aria-label' => $this
->t('Edit @progress_step progress step', [
'@progress_step' => $progress_step
->label(),
]),
],
],
];
if ($this->entity
->access('delete-progress-step:' . $progress_step
->id())) {
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.forms_steps.delete_progress_step_form', [
'forms_steps' => $forms_steps
->id(),
'forms_steps_progress_step' => $progress_step
->id(),
]),
'attributes' => [
'aria-label' => $this
->t('Delete @progress_step progress_step', [
'@progress_step' => $progress_step
->label(),
]),
],
];
}
// Defines active routes.
$routes = [];
$active_routes = $progress_step
->activeRoutes();
$active_routes = array_filter($active_routes);
$active_routes = $forms_steps
->getSteps($active_routes);
foreach ($active_routes as $value) {
$routes[] = $value
->label();
}
if (!count($routes)) {
$routes = $this
->t('No step assigned on this progress step');
}
else {
$routes = implode(', ', $routes);
}
// Defines link.
if (empty($progress_step
->link())) {
$link = $this
->t('No link defined');
}
else {
$step_id = $progress_step
->link();
$link = $forms_steps
->getStep($step_id)
->label();
}
// Defines link visibility.
$steps_ids = array_filter($progress_step
->linkVisibility());
if (empty($steps_ids)) {
$link_visibility = $this
->t('No link displayed');
}
else {
$steps = $forms_steps
->getSteps($steps_ids);
$link_visibility = [];
foreach ($steps as $step) {
$link_visibility[] = $step
->label();
}
$link_visibility = implode(', ', $link_visibility);
}
$form['progress_container']['progress_steps'][$progress_step
->id()] = [
'#attributes' => [
'class' => [
'draggable',
],
],
'progress_step' => [
'#markup' => $progress_step
->label(),
],
'routes' => [
'#markup' => $routes,
],
'link' => [
'#markup' => $link,
],
'link_visibility' => [
'#markup' => $link_visibility,
],
'#weight' => $progress_step
->weight(),
'weight' => [
'#type' => 'weight',
'#title' => $this
->t('Weight for @title', [
'@title' => $progress_step
->label(),
]),
'#title_display' => 'invisible',
'#default_value' => $progress_step
->weight(),
'#attributes' => [
'class' => [
'progress-state-weight',
],
],
'#delta' => 100,
],
'operations' => [
'#type' => 'operations',
'#links' => $links,
],
];
}
$form['progress_container']['progress_add'] = [
'#markup' => $forms_steps
->toLink($this
->t('Add a new progress step'), 'add-progress-step-form')
->toString(),
];
$linked_saved_only = $this->entity
->getProgressStepsLinksSavedOnly();
$linked_saved_only_next = $this->entity
->getProgressStepsLinksSavedOnlyNext();
$form['progress_container']['settings'] = [
'#type' => 'details',
'#title' => $this
->t('Progress bar settings'),
'#open' => $linked_saved_only,
];
$form['progress_container']['settings']['progress_steps_links_saved_only'] = [
'#type' => 'checkbox',
'#default_value' => $linked_saved_only,
'#title' => $this
->t('Show links only if the concerning steps have been saved in the database'),
];
$form['progress_container']['settings']['progress_steps_links_saved_only_next'] = [
'#type' => 'checkbox',
'#default_value' => $linked_saved_only_next,
'#title' => $this
->t('Also show the link of the step following the last step that was saved to the database'),
'#states' => array(
'visible' => array(
':input[name="progress_steps_links_saved_only"]' => array(
'checked' => TRUE,
),
),
),
];
}
$form['settings'] = [
'#type' => 'details',
'#title' => $this
->t('Settings'),
'#open' => FALSE,
];
$redirection_options = [
'' => $this
->t('None'),
'internal' => $this
->t('Internal path'),
'external' => $this
->t('External url'),
'route' => $this
->t('Route'),
'entity' => $this
->t('Current Entity'),
];
$form['settings']['redirection_policy'] = [
'#type' => 'select',
'#title' => $this
->t('Redirection policy'),
'#description' => $this
->t('Defines how the user should be redirected after the last step submission.<br/><strong>Internal:</strong> An internal path that is accessible to the user.<br/><strong>External:</strong> An absolute URL to an external target.<br/><strong>Route:</strong> A route name. Forms Steps current route parameters will be passed to this route. Advanced user only.<br/><strong>Current Entity:</strong> Redirects to the current Node View.'),
'#options' => $redirection_options,
'#default_value' => $this->entity
->getRedirectionPolicy(),
];
$form['settings']['redirection_target'] = [
'#type' => 'textfield',
'#title' => $this
->t('Redirection target'),
'#description' => $this
->t('Defines where the user will be redirected after the last step submission.'),
'#default_value' => $this->entity
->getRedirectionTarget(),
'#states' => [
'invisible' => [
[
':input[name="redirection_policy"]' => [
'value' => '',
],
],
[
':input[name="redirection_policy"]' => [
'value' => 'entity',
],
],
],
],
];
$form['settings']['description'] = [
'#type' => 'textarea',
'#default_value' => $this->entity
->getDescription(),
'#description' => $this
->t('Enter a description for this Forms Steps.'),
'#title' => $this
->t('Description'),
];
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Save');
$actions['cancel'] = [
'#type' => 'submit',
'#limit_validation_errors' => [
[
'locked',
],
],
'#value' => $this
->t('Cancel'),
'#submit' => [
'::cancel',
],
];
return $actions;
}
/**
* {@inheritdoc}
*/
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
// This form can only set the forms steps ID, label and the weights
// for each step.
/** @var \Drupal\forms_steps\FormsStepsInterface $entity */
$values = $form_state
->getValues();
$entity
->set('label', $values['label']);
$entity
->set('id', $values['id']);
$entity
->set('description', $values['description']);
$entity
->set('progress_steps_links_saved_only', $values['progress_steps_links_saved_only']);
$entity
->set('progress_steps_links_saved_only_next', $values['progress_steps_links_saved_only_next']);
$entity
->set('redirection_policy', $values['redirection_policy']);
$entity
->set('redirection_target', $values['redirection_target']);
if (!empty($values['steps'])) {
foreach ($values['steps'] as $step_id => $step_values) {
$entity
->setStepWeight($step_id, $step_values['weight']);
}
}
if (!empty($values['progress_steps'])) {
foreach ($values['progress_steps'] as $progress_step_id => $progress_step_values) {
$entity
->setProgressStepWeight($progress_step_id, $progress_step_values['weight']);
}
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
$form_state
->setRedirectUrl($this->entity
->toUrl('edit-form'));
$this
->messenger()
->addMessage($this
->t('Forms Steps %label has been updated.', [
'%label' => $this->entity
->label(),
]));
}
/**
* Form submission handler for the 'cancel' action.
*
* @param array $form
* Form to alter.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Forms States to alter.
*/
public function cancel(array $form, FormStateInterface $form_state) {
$this
->messenger()
->addMessage($this
->t('Canceled.'));
$form_state
->setRedirect('entity.forms_steps.collection');
}
/**
* Title callback: also display the Forms Steps label.
*
* @param \Drupal\forms_steps\Entity\FormsSteps $forms_steps
* Forms Steps to get label from.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* Translatable Title.
*/
public function getTitle(FormsSteps $forms_steps) {
return $this
->t('Edit Forms Steps "@label"', [
'@label' => $forms_steps
->label(),
]);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$values = $form_state
->getValues();
switch ($values['redirection_policy']) {
// Check that the specified internal path exists and that the current user
// has access to it.
case 'internal':
if (!$this->pathValidator
->isValid($values['redirection_target'])) {
$form_state
->setErrorByName('redirection_target', $this
->t('Invalid internal path for redirection!'));
}
break;
// Check that the specified route exists.
case 'route':
if (count($this->routeProvider
->getRoutesByNames([
$values['redirection_target'],
])) === 0) {
$form_state
->setErrorByName('redirection_target', $this
->t('Invalid route specified for redirection!'));
}
break;
// Check that the specified external URL exists.
case 'external':
if (!UrlHelper::isExternal($values['redirection_target']) || !UrlHelper::isValid($values['redirection_target'])) {
$form_state
->setErrorByName('redirection_target', $this
->t('Invalid external URL specified for redirection!'));
}
break;
}
}
}
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 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:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
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:: |
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. | |
FormsStepsEditForm:: |
protected | property | PathValidatorInterface. | |
FormsStepsEditForm:: |
protected | property | RouteProvider. | |
FormsStepsEditForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
FormsStepsEditForm:: |
public | function | Form submission handler for the 'cancel' action. | |
FormsStepsEditForm:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityForm:: |
|
FormsStepsEditForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
FormsStepsEditForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
FormsStepsEditForm:: |
public | function | Title callback: also display the Forms Steps label. | |
FormsStepsEditForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
FormsStepsEditForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
FormsStepsEditForm:: |
public | function | Class constructor. | |
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. |