You are here

public function ContextEditForm::processConditions in Context 8

Same name and namespace in other branches
  1. 8.4 modules/context_ui/src/Form/ContextEditForm.php \Drupal\context_ui\Form\ContextEditForm::processConditions()
  2. 8.0 modules/context_ui/src/Form/ContextEditForm.php \Drupal\context_ui\Form\ContextEditForm::processConditions()

Process function for the conditions.

Parameters

$element: The element to process.

FormStateInterface $form_state: The current form state.

Return value

array

File

modules/context_ui/src/Form/ContextEditForm.php, line 83

Class

ContextEditForm

Namespace

Drupal\context_ui\Form

Code

public function processConditions(&$element, FormStateInterface $form_state) {
  $conditions = $this->entity
    ->getConditions();
  $element['add_condition'] = array(
    '#type' => 'link',
    '#title' => $this
      ->t('Add condition'),
    '#url' => Url::fromRoute('context.conditions_library', [
      'context' => $this->entity
        ->id(),
    ]),
    '#attributes' => [
      'class' => [
        'use-ajax',
        'button',
        'button--small',
      ],
      'data-dialog-type' => 'modal',
      'data-dialog-options' => Json::encode([
        'width' => 700,
      ]),
    ],
  );
  if (!count($conditions)) {
    $element['reactions']['empty'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('No conditions has been added. When there are no added conditions the context will be considered sitewide.'),
    ];
  }
  $element['condition_tabs'] = [
    '#type' => 'vertical_tabs',
    '#parents' => [
      'condition_tabs',
    ],
  ];
  foreach ($conditions as $condition_id => $condition) {
    $element['condition-' . $condition_id] = [
      '#type' => 'details',
      '#title' => $condition
        ->getPluginDefinition()['label'],
      '#group' => 'condition_tabs',
    ];
    $element['condition-' . $condition_id]['options'] = $condition
      ->buildConfigurationForm([], $form_state);
    $element['condition-' . $condition_id]['options']['#parents'] = [
      'conditions',
      $condition_id,
    ];
    $element['condition-' . $condition_id]['remove'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Remove condition'),
      '#url' => Url::fromRoute('context.condition_delete', [
        'context' => $this->entity
          ->id(),
        'condition_id' => $condition_id,
      ]),
      '#attributes' => [
        'class' => [
          'use-ajax',
          'button',
          'button--small',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 700,
        ]),
      ],
    ];
  }
  return $element;
}