You are here

class YamlFormSubmissionResendForm in YAML Form 8

Defines a form that resends form submission.

Hierarchy

Expanded class hierarchy of YamlFormSubmissionResendForm

2 string references to 'YamlFormSubmissionResendForm'
yamlform.routing.yml in ./yamlform.routing.yml
yamlform.routing.yml
yamlform_node.routing.yml in modules/yamlform_node/yamlform_node.routing.yml
modules/yamlform_node/yamlform_node.routing.yml

File

src/Form/YamlFormSubmissionResendForm.php, line 15

Namespace

Drupal\yamlform\Form
View source
class YamlFormSubmissionResendForm extends FormBase {

  /**
   * A form submission.
   *
   * @var \Drupal\yamlform\YamlFormSubmissionInterface
   */
  protected $yamlformSubmission;

  /**
   * The source entity.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'yamlform_submission_resend';
  }

  /**
   * Form request handler.
   *
   * @var \Drupal\yamlform\YamlFormRequestInterface
   */
  protected $requestHandler;

  /**
   * Constructs a new YamlFormResultsDeleteBaseForm object.
   *
   * @param \Drupal\yamlform\YamlFormRequestInterface $request_handler
   *   The form request handler.
   */
  public function __construct(YamlFormRequestInterface $request_handler) {
    $this->requestHandler = $request_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('yamlform.request'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, YamlFormSubmissionInterface $yamlform_submission = NULL) {
    $this->yamlformSubmission = $yamlform_submission;
    $handlers = $yamlform_submission
      ->getYamlForm()
      ->getHandlers();

    /** @var \Drupal\yamlform\YamlFormHandlerMessageInterface[] $message_handlers */
    $message_handlers = [];
    foreach ($handlers as $handler_id => $handler) {
      if ($handler instanceof EmailYamlFormHandler) {
        $message_handlers[$handler_id] = $handler;
      }
    }

    // Get header.
    $header = [];
    $header['title'] = [
      'data' => $this
        ->t('Title / Description'),
    ];
    $header['id'] = [
      'data' => $this
        ->t('ID'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ];
    $header['summary'] = [
      'data' => $this
        ->t('summary'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ];
    $header['status'] = [
      'data' => $this
        ->t('Status'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ];

    // Get options.
    $options = [];
    foreach ($message_handlers as $index => $message_handler) {
      $message = $message_handler
        ->getMessage($this->yamlformSubmission);
      $options[$index]['title'] = [
        'data' => [
          'label' => [
            '#type' => 'label',
            '#title' => $message_handler
              ->label() . ': ' . $message_handler
              ->description(),
            '#title_display' => NULL,
            '#for' => 'edit-message-handler-id-' . str_replace('_', '-', $message_handler
              ->getHandlerId()),
          ],
        ],
      ];
      $options[$index]['id'] = [
        'data' => $message_handler
          ->getHandlerId(),
      ];
      $options[$index]['summary'] = [
        'data' => $message_handler
          ->getMessageSummary($message),
      ];
      $options[$index]['status'] = $message_handler
        ->isEnabled() ? $this
        ->t('Enabled') : $this
        ->t('Disabled');
    }

    // Get message handler id.
    if (empty($form_state
      ->getValue('message_handler_id'))) {
      reset($options);
      $message_handler_id = key($options);
      $form_state
        ->setValue('message_handler_id', $message_handler_id);
    }
    else {
      $message_handler_id = $form_state
        ->getValue('message_handler_id');
    }
    $message_handler = $this
      ->getMessageHandler($form_state);
    $form['message_handler_id'] = [
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options,
      '#js_select' => TRUE,
      '#empty' => $this
        ->t('No messages are available.'),
      '#multiple' => FALSE,
      '#default_value' => $message_handler_id,
      '#ajax' => [
        'callback' => '::updateMessage',
        'wrapper' => 'edit-yamlform-message-wrapper',
      ],
    ];

    // Message.
    $form['message'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Message'),
      '#open' => TRUE,
      '#tree' => TRUE,
      '#prefix' => '<div id="edit-yamlform-message-wrapper">',
      '#suffix' => '</div>',
    ];
    $message = $message_handler
      ->getMessage($yamlform_submission);
    $form['message'] += $message_handler
      ->resendMessageForm($message);

    // Add resend button.
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Resend message'),
    ];

    // Add submission navigation.
    $source_entity = $this->requestHandler
      ->getCurrentSourceEntity('yamlform_submission');
    $form['navigation'] = [
      '#theme' => 'yamlform_submission_navigation',
      '#yamlform_submission' => $yamlform_submission,
      '#weight' => -20,
    ];
    $form['information'] = [
      '#theme' => 'yamlform_submission_information',
      '#yamlform_submission' => $yamlform_submission,
      '#source_entity' => $source_entity,
      '#weight' => -19,
    ];
    $form['#attached']['library'][] = 'yamlform/yamlform.admin';
    return $form;
  }

  /**
   * Handles switching between messages.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   An associative array containing an email message.
   */
  public function updateMessage(array $form, FormStateInterface $form_state) {
    $message_handler = $this
      ->getMessageHandler($form_state);
    $message = $message_handler
      ->getMessage($this->yamlformSubmission);
    foreach ($message as $key => $value) {
      $form['message'][$key]['#value'] = $value;
    }
    return $form['message'];
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $params = $form_state
      ->getValue('message');
    $message_handler = $this
      ->getMessageHandler($form_state);
    $message_handler
      ->sendMessage($params);
    $t_args = [
      '%label' => $message_handler
        ->label(),
    ];
    drupal_set_message($this
      ->t('Successfully re-sent %label.', $t_args));
  }

  /**
   * Get message handler from form state.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return \Drupal\yamlform\YamlFormHandlerMessageInterface
   *   The current message handler.
   */
  protected function getMessageHandler(FormStateInterface $form_state) {
    $message_handler_id = $form_state
      ->getValue('message_handler_id');
    return $this->yamlformSubmission
      ->getYamlForm()
      ->getHandler($message_handler_id);
  }

}

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
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.
YamlFormSubmissionResendForm::$entity protected property The source entity.
YamlFormSubmissionResendForm::$requestHandler protected property Form request handler.
YamlFormSubmissionResendForm::$yamlformSubmission protected property A form submission.
YamlFormSubmissionResendForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
YamlFormSubmissionResendForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
YamlFormSubmissionResendForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
YamlFormSubmissionResendForm::getMessageHandler protected function Get message handler from form state.
YamlFormSubmissionResendForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
YamlFormSubmissionResendForm::updateMessage public function Handles switching between messages.
YamlFormSubmissionResendForm::__construct public function Constructs a new YamlFormResultsDeleteBaseForm object.