You are here

public function ContextUIController::addCondition 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::addCondition()
  2. 8.0 modules/context_ui/src/Controller/ContextUIController.php \Drupal\context_ui\Controller\ContextUIController::addCondition()

Add the specified condition to the context.

Parameters

Request $request: The current request.

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

$condition_id: The ID of the condition to add.

Return value

AjaxResponse|RedirectResponse

1 string reference to 'ContextUIController::addCondition'
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 343

Class

ContextUIController

Namespace

Drupal\context_ui\Controller

Code

public function addCondition(Request $request, ContextInterface $context, $condition_id) {
  if ($context
    ->hasCondition($condition_id)) {
    throw new HttpException(403, 'The specified condition had already been added to the context.');
  }

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

  // Prevent adding "Current theme" condition, if "Theme" reaction is already set.
  // Else this will cause an infinite loop when checking for active contexts.
  if ($condition_id == 'current_theme') {
    $reactions = $context
      ->getReactions();
    foreach ($reactions as $reaction) {
      if ($reaction
        ->getPluginId() == 'theme') {
        if ($request
          ->isXmlHttpRequest()) {
          $response = new AjaxResponse();
          $response
            ->addCommand(new CloseModalDialogCommand());
          $response
            ->addCommand(new OpenModalDialogCommand($this
            ->t("Current theme condition"), $this
            ->t("You can not set Current theme condition if Theme reaction is set."), [
            'width' => '700',
          ]));
          return $response;
        }
      }
    }
  }
  $context
    ->addCondition($condition
    ->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-conditions', $contextForm['conditions']));
    return $response;
  }
  $url = $context
    ->urlInfo('edit-form');
  return $this
    ->redirect($url
    ->getRouteName(), $url
    ->getRouteParameters());
}