You are here

class WebformEntitySettingsConfirmationForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/EntitySettings/WebformEntitySettingsConfirmationForm.php \Drupal\webform\EntitySettings\WebformEntitySettingsConfirmationForm

Webform confirmation settings.

Hierarchy

Expanded class hierarchy of WebformEntitySettingsConfirmationForm

File

src/EntitySettings/WebformEntitySettingsConfirmationForm.php, line 13

Namespace

Drupal\webform\EntitySettings
View source
class WebformEntitySettingsConfirmationForm extends WebformEntitySettingsBaseForm {

  /**
   * The webform token manager.
   *
   * @var \Drupal\webform\WebformTokenManagerInterface
   */
  protected $tokenManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->tokenManager = $container
      ->get('webform.token_manager');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\webform\WebformInterface $webform */
    $webform = $this->entity;
    $elements = $webform
      ->getElementsDecoded();
    if (!empty($elements['#method'])) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('Form is being posted using a custom method. Confirmation page must be handled by the <a href=":href">custom form action</a>.', [
        ':href' => $webform
          ->toUrl('settings-form')
          ->toString(),
      ]));
      return $form;
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\webform\WebformInterface $webform */
    $webform = $this->entity;
    $settings = $webform
      ->getSettings();

    // Confirmation type.
    $form['confirmation_type'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Confirmation type'),
    ];
    $form['confirmation_type']['ajax_confirmation'] = [
      '#type' => 'webform_message',
      '#message_type' => 'warning',
      '#message_message' => $this
        ->t("Only 'Inline', 'Message', 'Modal', and 'None' confirmation types are fully supported by Ajax."),
      '#access' => $settings['ajax'],
      '#states' => [
        'invisible' => [
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_INLINE,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_MESSAGE,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_MODAL,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_NONE,
            ],
          ],
        ],
      ],
    ];
    $form['confirmation_type']['confirmation_type'] = [
      '#title' => $this
        ->t('Confirmation type'),
      '#type' => 'radios',
      '#options' => [
        WebformInterface::CONFIRMATION_PAGE => $this
          ->t('Page (redirects to new page and displays the confirmation message)'),
        WebformInterface::CONFIRMATION_INLINE => $this
          ->t('Inline (reloads the current page and replaces the webform with the confirmation message)'),
        WebformInterface::CONFIRMATION_MESSAGE => $this
          ->t('Message (reloads the current page/form and displays the confirmation message at the top of the page)'),
        WebformInterface::CONFIRMATION_MODAL => $this
          ->t('Modal (reloads the current page/form and displays the confirmation message in a modal dialog)'),
        WebformInterface::CONFIRMATION_URL => $this
          ->t('URL (redirects to a custom path or URL)'),
        WebformInterface::CONFIRMATION_URL_MESSAGE => $this
          ->t('URL with message (redirects to a custom path or URL and displays the confirmation message at the top of the page)'),
        WebformInterface::CONFIRMATION_NONE => $this
          ->t('None (reloads the current page and does not display a confirmation message)'),
      ],
      '#default_value' => $settings['confirmation_type'],
    ];

    // Page.
    if ($webform
      ->isResultsDisabled()) {
      $form['confirmation_type']['page'] = [
        '#type' => 'webform_message',
        '#message_type' => 'warning',
        '#message_close' => TRUE,
        '#message_storage' => WebformMessage::STORAGE_SESSION,
        '#message_message' => $this
          ->t("Because the saving of submissions is disabled, the <code>[webform_submission:values]</code> token will not be available to the confirmation page's message."),
        '#states' => [
          'visible' => [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_PAGE,
            ],
          ],
        ],
      ];
    }

    // None.
    $form['confirmation_type']['none'] = [
      '#type' => 'webform_message',
      '#message_type' => 'warning',
      '#message_message' => $this
        ->t('This setting assumes that a webform handler will manage the displaying of a confirmation message.'),
      '#states' => [
        'visible' => [
          ':input[name="confirmation_type"]' => [
            'value' => WebformInterface::CONFIRMATION_NONE,
          ],
        ],
      ],
    ];
    $form['confirmation_type']['confirmation_update'] = [
      '#title' => $this
        ->t('Display confirmation when submission is updated'),
      '#description' => $this
        ->t('If checked this select confirmation type and message will be displayed when the submission is updated. Otherwise, a status message will be displayed at the top the page when a submission is updated.'),
      '#type' => 'checkbox',
      '#return_type' => TRUE,
      '#default_value' => $settings['confirmation_update'],
    ];

    // Confirmation url.
    $form['confirmation_url'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Confirmation URL'),
      '#open' => TRUE,
      '#states' => [
        'visible' => [
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_PAGE,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_URL,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_URL_MESSAGE,
            ],
          ],
        ],
      ],
    ];
    $form['confirmation_url']['confirmation_url'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Confirmation URL'),
      '#description' => $this
        ->t('The URL or path to redirect the user to upon successful submission.') . '<br/>' . t('Paths beginning with a forward slash (/) will redirect be treated as root-relative. Paths without a forward slash (/) will redirect be treated as Drupal relative path.'),
      '#default_value' => $settings['confirmation_url'],
      '#maxlength' => NULL,
      '#states' => [
        'visible' => [
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_URL,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_URL_MESSAGE,
            ],
          ],
        ],
        'required' => [
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_URL,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_URL_MESSAGE,
            ],
          ],
        ],
      ],
    ];
    $form['confirmation_url']['confirmation_exclude_query'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Exclude query string from Confirmation URL'),
      '#description' => $this
        ->t('If checked, all query string parameters will be removed from the Confirmation URL.'),
      '#default_value' => $settings['confirmation_exclude_query'],
    ];
    $form['confirmation_url']['confirmation_exclude_token'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Exclude token from Confirmation URL'),
      '#description' => $this
        ->t('If checked, to submissions token will be removed from the Confirmation URL and the [webform_submission] tokens will not be available within the confirmation message.'),
      '#default_value' => $settings['confirmation_exclude_token'],
      '#access' => !$webform
        ->isResultsDisabled(),
    ];
    $form['confirmation_url']['token_tree_link'] = $this->tokenManager
      ->buildTreeElement([
      'webform',
      'webform_submission',
      'webform_handler',
    ], $this
      ->t('You may use tokens to pass query string parameters. Make sure all tokens include the urlencode suffix. (i.e. [webform_submission:values:email:urlencode])'));

    // Confirmation settings.
    $form['confirmation_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Confirmation settings'),
      '#open' => TRUE,
      '#states' => [
        'invisible' => [
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_URL,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_NONE,
            ],
          ],
        ],
      ],
    ];
    $form['confirmation_settings']['confirmation_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Confirmation title'),
      '#description' => $this
        ->t('Page title to be shown upon successful submission.'),
      '#default_value' => $settings['confirmation_title'],
      '#states' => [
        'visible' => [
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_PAGE,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_MODAL,
            ],
          ],
        ],
      ],
    ];
    $form['confirmation_settings']['confirmation_message'] = [
      '#type' => 'webform_html_editor',
      '#title' => $this
        ->t('Confirmation message'),
      '#description' => $this
        ->t('Message to be shown upon successful submission.'),
      '#default_value' => $settings['confirmation_message'],
      '#states' => [
        'invisible' => [
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_URL,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_NONE,
            ],
          ],
        ],
      ],
    ];
    $form['confirmation_settings']['token_tree_link'] = $this->tokenManager
      ->buildTreeElement();

    // Attributes.
    $form['confirmation_attributes_container'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Confirmation attributes'),
      '#open' => TRUE,
      '#states' => [
        'visible' => [
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_PAGE,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_INLINE,
            ],
          ],
        ],
      ],
    ];
    $form['confirmation_attributes_container']['confirmation_attributes'] = [
      '#type' => 'webform_element_attributes',
      '#title' => $this
        ->t('Confirmation'),
      '#classes' => $this
        ->config('webform.settings')
        ->get('settings.confirmation_classes'),
      '#default_value' => $settings['confirmation_attributes'],
    ];

    // Back.
    $form['back'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Confirmation back link'),
      '#states' => [
        'visible' => [
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_PAGE,
            ],
          ],
          'or',
          [
            ':input[name="confirmation_type"]' => [
              'value' => WebformInterface::CONFIRMATION_INLINE,
            ],
          ],
        ],
      ],
    ];
    $form['back']['confirmation_back'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Display back to webform link'),
      '#return_value' => TRUE,
      '#default_value' => $settings['confirmation_back'],
    ];
    $form['back']['back_container'] = [
      '#type' => 'container',
      '#states' => [
        'visible' => [
          [
            ':input[name="confirmation_back"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ],
    ];
    $form['back']['back_container']['confirmation_back_label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Confirmation back link label'),
      '#size' => 20,
      '#default_value' => $settings['confirmation_back_label'],
    ];
    $form['back']['back_container']['confirmation_back_attributes_container'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Confirmation back link attributes'),
    ];
    $form['back']['back_container']['confirmation_back_attributes_container']['confirmation_back_attributes'] = [
      '#type' => 'webform_element_attributes',
      '#title' => $this
        ->t('Confirmation back link'),
      '#classes' => $this
        ->config('webform.settings')
        ->get('settings.confirmation_back_classes'),
      '#default_value' => $settings['confirmation_back_attributes'],
    ];
    $form['back']['back_container']['token_tree_link'] = $this->tokenManager
      ->buildTreeElement();
    $this->tokenManager
      ->elementValidate($form);
    return parent::form($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();

    /** @var \Drupal\webform\WebformInterface $webform */
    $webform = $this
      ->getEntity();

    // Set settings.
    $webform
      ->setSettings($values);
    parent::save($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 11
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::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 3
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties. 9
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 6
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 3
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 12
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::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 20
FormBase::$configFactory protected property The config factory. 3
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. 3
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.
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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. 4
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.
WebformEntitySettingsBaseForm::actions protected function Returns an array of supported actions for the current entity form. Overrides EntityForm::actions
WebformEntitySettingsBaseForm::appendBehaviors protected function Append behavior checkboxes to element.
WebformEntitySettingsBaseForm::setElementDescriptionsRecursive protected function Append [none] message and default value to an element's description.
WebformEntitySettingsConfirmationForm::$tokenManager protected property The webform token manager.
WebformEntitySettingsConfirmationForm::buildForm public function Form constructor. Overrides EntityForm::buildForm
WebformEntitySettingsConfirmationForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
WebformEntitySettingsConfirmationForm::form public function Gets the actual form array to be built. Overrides WebformEntitySettingsBaseForm::form
WebformEntitySettingsConfirmationForm::save public function Form submission handler for the 'save' action. Overrides WebformEntitySettingsBaseForm::save