You are here

public static function WebformEntityVariantsForm::ajaxOperation in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/WebformEntityVariantsForm.php \Drupal\webform\WebformEntityVariantsForm::ajaxOperation()

Calls a method on a webform variant and reloads the webform variants form.

Parameters

\Drupal\webform\WebformInterface $webform: The webform being acted upon.

string $webform_variant: THe webform variant id.

string $operation: The operation to perform, e.g., 'enable' or 'disable'.

\Symfony\Component\HttpFoundation\Request $request: The current request.

Return value

\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.

1 string reference to 'WebformEntityVariantsForm::ajaxOperation'
webform.routing.yml in ./webform.routing.yml
webform.routing.yml

File

src/WebformEntityVariantsForm.php, line 416

Class

WebformEntityVariantsForm
Provides a webform to manage submission variants.

Namespace

Drupal\webform

Code

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);
}