You are here

public function ThemeSwitcherController::ajaxOperation in Theme Switcher Rules 8

Handles AJAX operations from the overview form.

Parameters

\Drupal\theme_switcher\ThemeSwitcherRuleInterface $theme_switcher_rule: A Theme Switcher Rule object.

string|null $op: The operation being performed, either 'enable' to enable the Theme Switcher Rule, or 'disable' to disable the Theme Switcher Rule.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse A redirect response to redirect back to the theme switcher rule list.

See also

\Drupal\theme_switcher\Controller\ThemeSwitcherRuleListBuilder

1 string reference to 'ThemeSwitcherController::ajaxOperation'
theme_switcher.routing.yml in ./theme_switcher.routing.yml
theme_switcher.routing.yml

File

src/Controller/ThemeSwitcherController.php, line 70

Class

ThemeSwitcherController
Controller routines for AJAX callbacks for domain actions.

Namespace

Drupal\theme_switcher\Controller

Code

public function ajaxOperation(ThemeSwitcherRuleInterface $theme_switcher_rule, $op = NULL) {
  $message = $this
    ->t("The operation '%op' to '%label' failed.", [
    '%op' => $op,
    '%label' => $theme_switcher_rule
      ->label(),
  ]);
  try {
    switch ($op) {
      case 'enable':
        $theme_switcher_rule
          ->enable();
        $message = $this
          ->t("The Theme Switcher Rule '%label' has been enabled.", [
          '%label' => $theme_switcher_rule
            ->label(),
        ]);
        break;
      case 'disable':
        $theme_switcher_rule
          ->disable();
        $message = $this
          ->t("The Theme Switcher Rule '%label' has been disabled.", [
          '%label' => $theme_switcher_rule
            ->label(),
        ]);
        break;
    }
    $theme_switcher_rule
      ->save();
    $this->messenger
      ->addStatus($message);
    $this->logger
      ->notice($message);
  } catch (EntityStorageException $e) {
    $this->messenger
      ->addStatus($message);
    $this->logger
      ->error($message);
  }

  // Return to the invoking page.
  $url = Url::fromRoute('theme_switcher.admin', [], [
    'absolute' => TRUE,
  ]);
  return new RedirectResponse($url
    ->toString(), 302);
}