class WebformEntityVariantsForm in Webform 6.x
Same name and namespace in other branches
- 8.5 src/WebformEntityVariantsForm.php \Drupal\webform\WebformEntityVariantsForm
Provides a webform to manage submission variants.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\webform\WebformEntityVariantsForm uses WebformEntityAjaxFormTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of WebformEntityVariantsForm
File
- src/
WebformEntityVariantsForm.php, line 21
Namespace
Drupal\webformView source
class WebformEntityVariantsForm extends EntityForm {
use WebformEntityAjaxFormTrait;
/**
* The webform.
*
* @var \Drupal\webform\WebformInterface
*/
protected $entity;
/**
* The webform element manager.
*
* @var \Drupal\webform\Plugin\WebformElementManagerInterface
*/
protected $elementManager;
/**
* The webform variant manager.
*
* @var \Drupal\webform\Plugin\WebformVariantManagerInterface
*/
protected $variantManager;
/**
* Constructs a WebformEntityVariantsForm.
*
* @param \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager
* The webform element manager.
* @param \Drupal\webform\Plugin\WebformVariantManagerInterface $variant_manager
* The webform variant manager.
*/
public function __construct(WebformElementManagerInterface $element_manager, WebformVariantManagerInterface $variant_manager) {
$this->elementManager = $element_manager;
$this->variantManager = $variant_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.webform.element'), $container
->get('plugin.manager.webform.variant'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$user_input = $form_state
->getUserInput();
// Hard code the form id.
$form['#id'] = 'webform-variants-form';
// Build table header.
$header = [
[
'data' => $this
->t('Title / Notes'),
],
[
'data' => $this
->t('ID'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
[
'data' => $this
->t('Element'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
[
'data' => $this
->t('Summary'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
[
'data' => $this
->t('Status'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
[
'data' => $this
->t('Weight'),
'class' => [
'webform-tabledrag-hide',
],
],
[
'data' => $this
->t('Operations'),
],
];
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $this
->getEntity();
// Build table rows for variants.
$variants = $webform
->getVariants();
$rows = [];
foreach ($variants as $variant_id => $variant) {
$offcanvas_dialog_attributes = WebformDialogHelper::getOffCanvasDialogAttributes($variant
->getOffCanvasWidth());
$row['#attributes']['class'][] = 'draggable';
$row['#attributes']['data-webform-key'] = $variant_id;
$row['#weight'] = isset($user_input['variants']) && isset($user_input['variants'][$variant_id]) ? $user_input['variants'][$variant_id]['weight'] : NULL;
$row['title'] = [
'#tree' => FALSE,
'data' => [
'label' => [
'#type' => 'link',
'#title' => $variant
->label(),
'#url' => Url::fromRoute('entity.webform.variant.edit_form', [
'webform' => $webform
->id(),
'webform_variant' => $variant_id,
]),
'#attributes' => $offcanvas_dialog_attributes,
],
'notes' => [
'#prefix' => '<br/>',
'#markup' => $variant
->getNotes(),
],
],
];
$row['id'] = [
'#markup' => $variant_id,
];
$variant_element_key = $variant
->getElementKey();
$variant_element = $webform
->getElement($variant_element_key);
if ($variant_element) {
$webform_element = $this->elementManager
->getElementInstance($variant_element);
$row['element'] = [
'#markup' => $webform_element
->getAdminLabel($variant_element),
];
}
else {
$row['element'] = [
'data' => [
'#markup' => $this
->t("'@element_key' is missing.", [
'@element_key' => $variant_element_key,
]),
'#prefix' => '<b class="color-error">',
'#suffix' => '</b>',
],
];
}
$row['summary'] = $variant
->getSummary();
if ($variant
->isDisabled()) {
$status = $this
->t('Disabled');
}
else {
$status = $this
->t('Enabled');
}
$row['status'] = [
'data' => [
'#markup' => $status,
],
];
$row['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for @title', [
'@title' => $variant
->label(),
]),
'#title_display' => 'invisible',
'#delta' => 50,
'#default_value' => $variant
->getWeight(),
'#attributes' => [
'class' => [
'webform-variant-order-weight',
],
],
'#wrapper_attributes' => [
'class' => [
'webform-tabledrag-hide',
],
],
];
$operations = [];
// Edit.
$operations['edit'] = [
'title' => $this
->t('Edit'),
'url' => Url::fromRoute('entity.webform.variant.edit_form', [
'webform' => $webform
->id(),
'webform_variant' => $variant_id,
]),
'attributes' => $offcanvas_dialog_attributes,
];
// Duplicate.
$operations['duplicate'] = [
'title' => $this
->t('Duplicate'),
'url' => Url::fromRoute('entity.webform.variant.duplicate_form', [
'webform' => $webform
->id(),
'webform_variant' => $variant_id,
]),
'attributes' => $offcanvas_dialog_attributes,
];
if ($variant_element && $variant
->isEnabled()) {
// If #prepopulate is disabled use '_webform_variant'
// querystring parameter for view and test operations.
// @see \Drupal\webform\Entity\Webform::getSubmissionForm
$query = [
$variant_element_key => $variant_id,
];
if (empty($variant_element['#prepopulate'])) {
$query = [
'_webform_variant' => $query,
];
}
// View.
$operations['view'] = [
'title' => $this
->t('View'),
'url' => Url::fromRoute('entity.webform.canonical', [
'webform' => $webform
->id(),
], [
'query' => $query,
]),
];
// Test.
if ($webform
->access('test')) {
$operations['test'] = [
'title' => $this
->t('Test'),
'url' => Url::fromRoute('entity.webform.test_form', [
'webform' => $webform
->id(),
], [
'query' => $query,
]),
];
}
// Share.
if ($this->moduleHandler
->moduleExists('webform_share') && $webform
->access('update') && $webform
->getSetting('share', TRUE)) {
$operations['share'] = [
'title' => $this
->t('Share'),
'url' => Url::fromRoute('entity.webform.share_embed', [
'webform' => $webform
->id(),
], [
'query' => $query,
]),
];
}
}
// Apply.
$operations['apply'] = [
'title' => $this
->t('Apply'),
'url' => Url::fromRoute('entity.webform.variant.apply_form', [
'webform' => $webform
->id(),
], [
'query' => [
'variant_id' => $variant_id,
],
]),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
];
// Add AJAX functionality to enable/disable operations.
$operations['status'] = [
'title' => $variant
->isEnabled() ? $this
->t('Disable') : $this
->t('Enable'),
'url' => Url::fromRoute('entity.webform.variant.' . ($variant
->isEnabled() ? 'disable' : 'enable'), [
'webform' => $webform
->id(),
'webform_variant' => $variant_id,
]),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, [
'use-ajax',
]),
];
// Delete.
$operations['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.webform.variant.delete_form', [
'webform' => $webform
->id(),
'webform_variant' => $variant_id,
]),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
];
$row['operations'] = [
'#type' => 'operations',
'#links' => $operations,
'#prefix' => '<div class="webform-dropbutton">',
'#suffix' => '</div>',
];
$rows[$variant_id] = $row;
}
// Add test multiple variants.
if ($webform
->getVariants()
->count() > 1 && count($webform
->getElementsVariant()) > 1) {
$row = [];
$row['#attributes']['class'] = [
'webform-variant-table-test-multiple',
];
$row[] = [
'#wrapper_attributes' => [
'colspan' => 6,
],
];
$operations = [];
// View variants.
$operations['view'] = [
'title' => $this
->t('View variants'),
'url' => Url::fromRoute('entity.webform.variant.view_form', [
'webform' => $webform
->id(),
]),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
];
// Test variants.
if ($webform
->access('test')) {
$operations['test'] = [
'title' => $this
->t('Test variants'),
'url' => Url::fromRoute('entity.webform.variant.test_form', [
'webform' => $webform
->id(),
]),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
];
}
// Share variants.
if ($this->moduleHandler
->moduleExists('webform_share') && $webform
->access('update') && $webform
->getSetting('share', TRUE)) {
$operations['share'] = [
'title' => $this
->t('Share variants'),
'url' => Url::fromRoute('entity.webform.variant.share_form', [
'webform' => $webform
->id(),
]),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
];
}
// Apply variants.
$operations['apply'] = [
'title' => $this
->t('Apply variants'),
'url' => Url::fromRoute('entity.webform.variant.apply_form', [
'webform' => $webform
->id(),
]),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
];
$row['operations'] = [
'#type' => 'operations',
'#prefix' => '<div class="webform-dropbutton">',
'#suffix' => '</div>',
'#links' => $operations,
];
$rows[] = $row;
}
// Build the list of existing webform variants for this webform.
$form['variants'] = [
'#type' => 'table',
'#header' => $header,
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'webform-variant-order-weight',
],
],
'#attributes' => [
'id' => 'webform-variants',
'class' => [
'webform-variants-table',
],
],
'#empty' => $this
->t('There are currently no variants setup for this webform.'),
] + $rows;
// Must preload libraries required by (modal) dialogs.
WebformDialogHelper::attachLibraries($form);
$form['#attached']['library'][] = 'webform/webform.admin.tabledrag';
return parent::form($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function actionsElement(array $form, FormStateInterface $form_state) {
$form = parent::actionsElement($form, $form_state);
$form['submit']['#value'] = $this
->t('Save variants');
unset($form['delete']);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Update webform variant weights.
if (!$form_state
->isValueEmpty('variants')) {
$this
->updateVariantWeights($form_state
->getValue('variants'));
}
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $this
->getEntity();
$webform
->save();
$context = [
'@label' => $webform
->label(),
'link' => $webform
->toLink($this
->t('Edit'), 'variants')
->toString(),
];
$this
->logger('webform')
->notice('Webform @label variant saved.', $context);
$this
->messenger()
->addStatus($this
->t('Webform %label variant saved.', [
'%label' => $webform
->label(),
]));
}
/**
* Updates webform variant weights.
*
* @param array $variants
* Associative array with variants having variant ids as keys and array
* with variant data as values.
*/
protected function updateVariantWeights(array $variants) {
foreach ($variants as $variant_id => $variant_data) {
if ($this->entity
->getVariants()
->has($variant_id)) {
$this->entity
->getVariant($variant_id)
->setWeight($variant_data['weight']);
}
}
}
/**
* Calls a method on a webform variant and reloads the webform variants form.
*
* @param \Drupal\webform\WebformInterface $webform
* The webform being acted upon.
* @param string $webform_variant
* THe webform variant id.
* @param string $operation
* The operation to perform, e.g., 'enable' or 'disable'.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return \Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse
* Either returns an AJAX response that refreshes the webform's variants
* page, or redirects back to the webform's variants page.
*/
public static function ajaxOperation(WebformInterface $webform, $webform_variant, $operation, Request $request) {
// Perform the variant disable/enable operation.
$variant = $webform
->getVariant($webform_variant);
$variant
->{$operation}();
// Save the webform.
$webform
->save();
// Display message.
$t_args = [
'@label' => $variant
->label(),
'@op' => $operation === 'enable' ? t('enabled') : t('disabled'),
];
\Drupal::messenger()
->addStatus(t('This @label variant was @op.', $t_args));
// Get the webform's variants form URL.
$url = $webform
->toUrl('variants', [
'query' => [
'update' => $webform_variant,
],
])
->toString();
// If the request is via AJAX, return the webform variants form.
if ($request->request
->get('js')) {
$response = new AjaxResponse();
$response
->addCommand(new WebformRefreshCommand($url));
return $response;
}
// Otherwise, redirect back to the webform variants form.
return new RedirectResponse($url);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
3 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties. | 9 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
6 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
3 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
12 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
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. | |
WebformAjaxFormTrait:: |
protected | function | Queue announcement with Ajax response. | |
WebformAjaxFormTrait:: |
protected | function | Add Ajax support to a form. | |
WebformAjaxFormTrait:: |
protected | function | Create an AjaxResponse or WebformAjaxResponse object. | |
WebformAjaxFormTrait:: |
protected | function | Get announcements. | |
WebformAjaxFormTrait:: |
protected | function | Get redirect URL from the form's state. | |
WebformAjaxFormTrait:: |
protected | function | Get the form's Ajax wrapper id. | 1 |
WebformAjaxFormTrait:: |
protected | function | Determine if Ajax callback is callable. | |
WebformAjaxFormTrait:: |
protected | function | Is the current request for an Ajax modal/dialog. | |
WebformAjaxFormTrait:: |
protected | function | Is the current request for an off canvas dialog. | |
WebformAjaxFormTrait:: |
protected | function | Handle missing Ajax callback. | |
WebformAjaxFormTrait:: |
public | function | Empty submit callback used to only have the submit button to use an #ajax submit callback. | 1 |
WebformAjaxFormTrait:: |
protected | function | Reset announcements. | |
WebformAjaxFormTrait:: |
protected | function | Set announcements. | |
WebformAjaxFormTrait:: |
public | function | Submit form #ajax callback. | 1 |
WebformAjaxFormTrait:: |
public | function | Validate form #ajax callback. | 1 |
WebformEntityAjaxFormTrait:: |
protected | function | ||
WebformEntityAjaxFormTrait:: |
public | function | 1 | |
WebformEntityAjaxFormTrait:: |
public | function |
Cancel form #ajax callback. Overrides WebformAjaxFormTrait:: |
|
WebformEntityAjaxFormTrait:: |
protected | function |
Get default ajax callback settings. Overrides WebformAjaxFormTrait:: |
|
WebformEntityAjaxFormTrait:: |
protected | function |
Returns if webform is using Ajax. Overrides WebformAjaxFormTrait:: |
|
WebformEntityAjaxFormTrait:: |
protected | function | Determine if dialogs are disabled. | |
WebformEntityAjaxFormTrait:: |
protected | function |
Replace form via an Ajax response. Overrides WebformAjaxFormTrait:: |
|
WebformEntityVariantsForm:: |
protected | property | The webform element manager. | |
WebformEntityVariantsForm:: |
protected | property |
The webform. Overrides EntityForm:: |
|
WebformEntityVariantsForm:: |
protected | property | The webform variant manager. | |
WebformEntityVariantsForm:: |
protected | function |
Returns the action form element for the current entity form. Overrides EntityForm:: |
|
WebformEntityVariantsForm:: |
public static | function | Calls a method on a webform variant and reloads the webform variants form. | |
WebformEntityVariantsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
WebformEntityVariantsForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
WebformEntityVariantsForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
WebformEntityVariantsForm:: |
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 EntityForm:: |
|
WebformEntityVariantsForm:: |
protected | function | Updates webform variant weights. | |
WebformEntityVariantsForm:: |
public | function | Constructs a WebformEntityVariantsForm. |