You are here

public static function WebformEntityHandlersForm::ajaxOperation in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformEntityHandlersForm.php \Drupal\webform\WebformEntityHandlersForm::ajaxOperation()

Calls a method on a webform handler and reloads the webform handlers form.

Parameters

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

string $webform_handler: THe webform handler 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 handlers page, or redirects back to the webform's handlers page.

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

File

src/WebformEntityHandlersForm.php, line 278

Class

WebformEntityHandlersForm
Provides a webform to manage submission handlers.

Namespace

Drupal\webform

Code

public static function ajaxOperation(WebformInterface $webform, $webform_handler, $operation, Request $request) {

  // Perform the handler disable/enable operation.
  $handler = $webform
    ->getHandler($webform_handler);
  $handler
    ->{$operation}();

  // Save the webform.
  $webform
    ->save();

  // Display message.
  $t_args = [
    '@label' => $handler
      ->label(),
    '@op' => $operation === 'enable' ? t('enabled') : t('disabled'),
  ];
  \Drupal::messenger()
    ->addStatus(t('This @label handler was @op.', $t_args));

  // Get the webform's handlers form URL.
  $url = $webform
    ->toUrl('handlers', [
    'query' => [
      'update' => $webform_handler,
    ],
  ])
    ->toString();

  // If the request is via AJAX, return the webform handlers form.
  if ($request->request
    ->get('js')) {
    $response = new AjaxResponse();
    $response
      ->addCommand(new WebformRefreshCommand($url));
    return $response;
  }

  // Otherwise, redirect back to the webform handlers form.
  return new RedirectResponse($url);
}