You are here

class FormsStepsEditForm in Forms Steps 8

Provides a form to edit a Forms Steps.

Hierarchy

Expanded class hierarchy of FormsStepsEditForm

File

src/Form/FormsStepsEditForm.php, line 18

Namespace

Drupal\forms_steps\Form
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 7
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 2
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 10
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::submitForm 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::submitForm 17
EntityForm::__get public function
EntityForm::__set public function
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormsStepsEditForm::$pathValidator protected property PathValidatorInterface.
FormsStepsEditForm::$routeProvider protected property RouteProvider.
FormsStepsEditForm::actions protected function Returns an array of supported actions for the current entity form. Overrides EntityForm::actions
FormsStepsEditForm::cancel public function Form submission handler for the 'cancel' action.
FormsStepsEditForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties Overrides EntityForm::copyFormValuesToEntity
FormsStepsEditForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
FormsStepsEditForm::form public function Gets the actual form array to be built. Overrides EntityForm::form
FormsStepsEditForm::getTitle public function Title callback: also display the Forms Steps label.
FormsStepsEditForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
FormsStepsEditForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
FormsStepsEditForm::__construct public function Class constructor.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.