You are here

public function ContextUIController::addReaction in Context 8

Same name and namespace in other branches
  1. 8.4 modules/context_ui/src/Controller/ContextUIController.php \Drupal\context_ui\Controller\ContextUIController::addReaction()
  2. 8.0 modules/context_ui/src/Controller/ContextUIController.php \Drupal\context_ui\Controller\ContextUIController::addReaction()

Add the specified reaction to the context.

Parameters

Request $request: The current request.

ContextInterface $context: The context to add the reaction to.

$reaction_id: The ID of the reaction to add.

Return value

AjaxResponse|RedirectResponse

1 string reference to 'ContextUIController::addReaction'
context_ui.routing.yml in modules/context_ui/context_ui.routing.yml
modules/context_ui/context_ui.routing.yml

File

modules/context_ui/src/Controller/ContextUIController.php, line 277

Class

ContextUIController

Namespace

Drupal\context_ui\Controller

Code

public function addReaction(Request $request, ContextInterface $context, $reaction_id) {
  if ($context
    ->hasReaction($reaction_id)) {
    throw new HttpException(403, 'The specified reaction had already been added to the context.');
  }

  // Create an instance of the reaction and add it to the context.
  try {
    $reaction = $this->contextReactionManager
      ->createInstance($reaction_id);
  } catch (PluginException $e) {
    throw new HttpException(400, $e
      ->getMessage());
  }

  // If one of the condition is "Current theme", prevent adding Theme reaction.
  // Else this will cause an infinite loop when checking for active contexts.
  if ($reaction_id == 'theme') {
    $conditions = $context
      ->getConditions();
    foreach ($conditions as $condition) {
      if ($condition
        ->getPluginId() == 'current_theme') {
        if ($request
          ->isXmlHttpRequest()) {
          $response = new AjaxResponse();
          $response
            ->addCommand(new CloseModalDialogCommand());
          $response
            ->addCommand(new OpenModalDialogCommand($this
            ->t("Theme reaction"), $this
            ->t("You can not place Theme reaction if Current theme condition is set."), [
            'width' => '700',
          ]));
          return $response;
        }
      }
    }
  }
  $context
    ->addReaction($reaction
    ->getConfiguration());
  $context
    ->save();

  // If the request is an AJAX request then return an AJAX response with
  // commands to replace the content on the page.
  if ($request
    ->isXmlHttpRequest()) {
    $response = new AjaxResponse();
    $contextForm = $this->contextManager
      ->getForm($context, 'edit');
    $response
      ->addCommand(new CloseModalDialogCommand());
    $response
      ->addCommand(new ReplaceCommand('#context-reactions', $contextForm['reactions']));
    return $response;
  }
  $url = $context
    ->urlInfo('edit-form');
  return $this
    ->redirect($url
    ->getRouteName(), $url
    ->getRouteParameters());
}