You are here

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

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

Parameters

ContextInterface $context: The context to display available conditions for.

Return value

array

1 string reference to 'ContextUIController::listConditions'
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 120

Class

ContextUIController

Namespace

Drupal\context_ui\Controller

Code

public function listConditions(ContextInterface $context) {

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

  // Add a table row for each condition.
  foreach ($conditions as $condition_id => $condition) {

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