You are here

public function ContextUIController::addCondition in Context 8.0

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 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 325

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