class WebformSubmissionResendForm in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Form/WebformSubmissionResendForm.php \Drupal\webform\Form\WebformSubmissionResendForm
Defines a webform that resends webform submission.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\webform\Form\WebformSubmissionResendForm uses WebformAjaxElementTrait
Expanded class hierarchy of WebformSubmissionResendForm
2 string references to 'WebformSubmissionResendForm'
- webform.routing.yml in ./
webform.routing.yml - webform.routing.yml
- webform_node.routing.yml in modules/
webform_node/ webform_node.routing.yml - modules/webform_node/webform_node.routing.yml
File
- src/
Form/ WebformSubmissionResendForm.php, line 16
Namespace
Drupal\webform\FormView source
class WebformSubmissionResendForm extends FormBase {
use WebformAjaxElementTrait;
/**
* The webform request handler.
*
* @var \Drupal\webform\WebformRequestInterface
*/
protected $requestHandler;
/**
* A webform submission.
*
* @var \Drupal\webform\WebformSubmissionInterface
*/
protected $webformSubmission;
/**
* The source entity.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'webform_submission_resend';
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->requestHandler = $container
->get('webform.request');
return $instance;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission = NULL) {
$this->webformSubmission = $webform_submission;
// Apply variants to the webform.
$webform = $webform_submission
->getWebform();
$webform
->applyVariants($webform_submission);
// 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 = $this
->getMessageHandlerOptions($webform_submission);
// Get message handler id from form state or use the first message handler.
if (!empty($form_state
->getValue('message_handler_id'))) {
$message_handler_id = $form_state
->getValue('message_handler_id');
}
else {
$message_handler_id = key($options);
}
// Display message handler with change message Ajax submit button.
$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.
$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;
// Add resend button.
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Resend message'),
];
// Add submission navigation.
$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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$params = $form_state
->getValue('message');
// Add webform submission.
$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));
}
/**
* Get message handler from webform state.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return \Drupal\webform\Plugin\WebformHandlerMessageInterface
* The current message handler.
*/
protected function getMessageHandler(FormStateInterface $form_state) {
$message_handler_id = $form_state
->getValue('message_handler_id');
return $this->webformSubmission
->getWebform()
->getHandler($message_handler_id);
}
/****************************************************************************/
// Helper methods.
/****************************************************************************/
/**
* Get a webform submission's message handlers as options.
*
* @param \Drupal\webform\WebformSubmissionInterface $webform_submission
* A webform submission.
*
* @return array
* An associative array containing a webform submission's message handlers
* as table select options.
*/
protected function getMessageHandlerOptions(WebformSubmissionInterface $webform_submission) {
$handlers = $webform_submission
->getWebform()
->getHandlers();
// Get options.
$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;
}
/****************************************************************************/
// Change message ajax callbacks.
/****************************************************************************/
/**
* {@inheritdoc}
*/
public static function submitAjaxElementCallback(array $form, FormStateInterface $form_state) {
// Unset the message so that it can be completely rebuilt.
NestedArray::unsetValue($form_state
->getUserInput(), [
'message',
]);
$form_state
->unsetValue('message');
// Rebuild the form.
$form_state
->setRebuild();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
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. | 3 |
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. | |
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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
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. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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. | 4 |
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. | |
WebformAjaxElementTrait:: |
public | function | Build an Ajax element. | |
WebformAjaxElementTrait:: |
public | function | Build an Ajax element's trigger. | |
WebformAjaxElementTrait:: |
public | function | Build an Ajax element's update (submit) button. | |
WebformAjaxElementTrait:: |
public | function | Build an Ajax element's wrapper. | |
WebformAjaxElementTrait:: |
public | function | Get Ajax element update class. | |
WebformAjaxElementTrait:: |
public | function | Get Ajax element update name. | |
WebformAjaxElementTrait:: |
public | function | Get Ajax element wrapper id. | |
WebformAjaxElementTrait:: |
protected static | function | Get ajax element wrapper. | |
WebformAjaxElementTrait:: |
public static | function | Ajax element update callback. | |
WebformAjaxElementTrait:: |
public static | function | Ajax element validate callback. | |
WebformSubmissionResendForm:: |
protected | property | The source entity. | |
WebformSubmissionResendForm:: |
protected | property | The webform request handler. | |
WebformSubmissionResendForm:: |
protected | property | A webform submission. | |
WebformSubmissionResendForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
WebformSubmissionResendForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
WebformSubmissionResendForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
WebformSubmissionResendForm:: |
protected | function | Get message handler from webform state. | |
WebformSubmissionResendForm:: |
protected | function | Get a webform submission's message handlers as options. | |
WebformSubmissionResendForm:: |
public static | function |
Ajax element submit callback. Overrides WebformAjaxElementTrait:: |
|
WebformSubmissionResendForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |