View source
<?php
namespace Drupal\context_ui\Controller;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\context\ContextManager;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\context\ContextInterface;
use Drupal\context\ContextReactionManager;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Component\Plugin\Exception\PluginException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContextUIController extends ControllerBase {
protected $contextReactionManager;
protected $contextManager;
protected $conditionManager;
function __construct(ContextManager $contextManager, ContextReactionManager $contextReactionManager, ConditionManager $conditionManager) {
$this->contextManager = $contextManager;
$this->contextReactionManager = $contextReactionManager;
$this->conditionManager = $conditionManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('context.manager'), $container
->get('plugin.manager.context_reaction'), $container
->get('plugin.manager.condition'));
}
public function groupsAutocomplete(Request $request) {
$query = $request->query
->get('q');
$matches = [];
foreach ($this->contextManager
->getContexts() as $context) {
if (stripos($context
->getGroup(), $query) === 0) {
$matches[] = $context
->getGroup();
}
}
$response = [];
foreach (array_unique($matches) as $match) {
$response[] = [
'value' => $match,
'label' => Html::escape($match),
];
}
return new JsonResponse($response);
}
public function listConditions(ContextInterface $context) {
$conditions = $this->conditionManager
->getDefinitions();
$header = [
$this
->t('Condition'),
];
$build['filter'] = [
'#type' => 'search',
'#title' => $this
->t('Filter'),
'#title_display' => 'invisible',
'#size' => 30,
'#placeholder' => $this
->t('Filter by condition name'),
'#attributes' => [
'class' => [
'context-table-filter',
],
'data-element' => '.condition-add-table',
'title' => $this
->t('Enter a part of the condition name to filter by.'),
],
];
$rows = [];
foreach ($conditions as $condition_id => $condition) {
if ($context
->hasCondition($condition_id)) {
continue;
}
$rows[] = [
'condition' => [
'data' => [
'#type' => 'link',
'#title' => $condition['label'] . ' (' . $condition['provider'] . ')',
'#url' => Url::fromRoute('context.condition_add', [
'context' => $context
->id(),
'condition_id' => $condition_id,
]),
'#attributes' => [
'class' => [
'use-ajax',
'context-table-filter-text-source',
],
],
'#options' => [
'html' => TRUE,
],
],
],
];
}
$build['conditions'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this
->t('There are no conditions left that can be added to this context.'),
'#attributes' => [
'class' => [
'condition-add-table',
],
],
];
$build['#attached']['library'][] = 'context_ui/admin';
return $build;
}
public function listReactions(ContextInterface $context) {
$reactions = $this->contextReactionManager
->getDefinitions();
$header = [
$this
->t('Reactions'),
];
$build['filter'] = [
'#type' => 'search',
'#title' => $this
->t('Filter'),
'#title_display' => 'invisible',
'#size' => 30,
'#placeholder' => $this
->t('Filter by reaction name'),
'#attributes' => [
'class' => [
'context-table-filter',
],
'data-element' => '.reaction-add-table',
'title' => $this
->t('Enter a part of the reaction name to filter by.'),
],
];
$rows = [];
foreach ($reactions as $reaction_id => $reaction) {
if ($context
->hasReaction($reaction_id)) {
continue;
}
$rows[] = [
'reaction' => [
'data' => [
'#type' => 'link',
'#title' => $reaction['label'],
'#url' => Url::fromRoute('context.reaction_add', [
'context' => $context
->id(),
'reaction_id' => $reaction_id,
]),
'#attributes' => [
'class' => [
'use-ajax',
'context-table-filter-text-source',
],
],
'#options' => [
'html' => TRUE,
],
'#ajax' => TRUE,
],
],
];
}
$build['reactions'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this
->t('There are no reactions left that can be added to this context.'),
'#attributes' => [
'class' => [
'reaction-add-table',
],
],
];
$build['#attached']['library'][] = 'context_ui/admin';
return $build;
}
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.');
}
try {
$reaction = $this->contextReactionManager
->createInstance($reaction_id);
} catch (PluginException $e) {
throw new HttpException(400, $e
->getMessage());
}
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 ($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());
}
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.');
}
try {
$condition = $this->conditionManager
->createInstance($condition_id);
} catch (PluginException $e) {
throw new HttpException(400, $e
->getMessage());
}
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 ($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());
}
}