View source
<?php
namespace Drupal\webform\Form;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Element\WebformAjaxElementTrait;
use Drupal\webform\Plugin\WebformHandlerMessageInterface;
use Drupal\webform\WebformRequestInterface;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformSubmissionResendForm extends FormBase {
use WebformAjaxElementTrait;
protected $webformSubmission;
protected $entity;
public function getFormId() {
return 'webform_submission_resend';
}
protected $requestHandler;
public function __construct(WebformRequestInterface $request_handler) {
$this->requestHandler = $request_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('webform.request'));
}
public function buildForm(array $form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission = NULL) {
$this->webformSubmission = $webform_submission;
$webform = $webform_submission
->getWebform();
$webform
->applyVariants($webform_submission);
$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,
],
];
$options = $this
->getMessageHandlerOptions($webform_submission);
if (!empty($form_state
->getValue('message_handler_id'))) {
$message_handler_id = $form_state
->getValue('message_handler_id');
}
else {
$message_handler_id = key($options);
}
$form['message_handler'] = [];
$form['message_handler']['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,
];
$message_handler = $this->webformSubmission
->getWebform()
->getHandler($message_handler_id);
$message = $message_handler
->getMessage($webform_submission);
$resend_form = $message_handler
->resendMessageForm($message);
$form['message'] = [
'#type' => 'details',
'#title' => $this
->t('Message'),
'#open' => TRUE,
'#tree' => TRUE,
] + $resend_form;
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Resend message'),
];
$source_entity = $this->requestHandler
->getCurrentSourceEntity('webform_submission');
$form['navigation'] = [
'#type' => 'webform_submission_navigation',
'#webform_submission' => $webform_submission,
'#weight' => -20,
];
$form['information'] = [
'#type' => 'webform_submission_information',
'#webform_submission' => $webform_submission,
'#source_entity' => $source_entity,
'#weight' => -19,
];
$form['#attached']['library'][] = 'webform/webform.admin';
$this
->buildAjaxElement('webform-message-handler', $form['message'], $form['message_handler']['message_handler_id'], $form['message_handler']);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$params = $form_state
->getValue('message');
$params['webform_submission'] = $this->webformSubmission;
$message_handler_id = $form_state
->getValue('message_handler_id');
$message_handler = $this->webformSubmission
->getWebform()
->getHandler($message_handler_id);
$message_handler
->sendMessage($this->webformSubmission, $params);
$t_args = [
'%label' => $message_handler
->label(),
];
$this
->messenger()
->addStatus($this
->t('Successfully re-sent %label.', $t_args));
}
protected function getMessageHandler(FormStateInterface $form_state) {
$message_handler_id = $form_state
->getValue('message_handler_id');
return $this->webformSubmission
->getWebform()
->getHandler($message_handler_id);
}
protected function getMessageHandlerOptions(WebformSubmissionInterface $webform_submission) {
$handlers = $webform_submission
->getWebform()
->getHandlers();
$options = [];
foreach ($handlers as $handler_id => $message_handler) {
if (!$message_handler instanceof WebformHandlerMessageInterface) {
continue;
}
$message = $message_handler
->getMessage($webform_submission);
$options[$handler_id]['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[$handler_id]['id'] = [
'data' => $message_handler
->getHandlerId(),
];
$options[$handler_id]['summary'] = [
'data' => $message_handler
->getMessageSummary($message),
];
$options[$handler_id]['status'] = $message_handler
->isEnabled() ? $this
->t('Enabled') : $this
->t('Disabled');
}
return $options;
}
public static function submitAjaxElementCallback(array $form, FormStateInterface $form_state) {
NestedArray::unsetValue($form_state
->getUserInput(), [
'message',
]);
$form_state
->unsetValue('message');
$form_state
->setRebuild();
}
}