You are here

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

Displays a list of reactions that can be added to the context.

Parameters

ContextInterface $context: The context to display available

Return value

array

1 string reference to 'ContextUIController::listReactions'
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 195

Class

ContextUIController

Namespace

Drupal\context_ui\Controller

Code

public function listReactions(ContextInterface $context) {

  // Get a list of all available conditions.
  $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 = [];

  // Add a table row for each context reaction.
  foreach ($reactions as $reaction_id => $reaction) {

    // Only add the reaction to the list of it's not already been added to
    // the context.
    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;
}