You are here

public function ContextListBuilder::buildForm in Context 8.4

Same name and namespace in other branches
  1. 8 modules/context_ui/src/ContextListBuilder.php \Drupal\context_ui\ContextListBuilder::buildForm()
  2. 8.0 modules/context_ui/src/ContextListBuilder.php \Drupal\context_ui\ContextListBuilder::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

modules/context_ui/src/ContextListBuilder.php, line 106

Class

ContextListBuilder
Provides a class to crate the Context List.

Namespace

Drupal\context_ui

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $groups = $this->contextManager
    ->getContextsByGroup();
  $form['contexts'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Context'),
      $this
        ->t('Description'),
      $this
        ->t('Group'),
      $this
        ->t('Weight'),
      $this
        ->t('Operations'),
    ],
    '#empty' => $this
      ->t('There are no contexts defined.'),
    '#attributes' => [
      'id' => 'contexts',
    ],
  ];
  $group_options = [];

  // @todo Make this a bit prettier.
  foreach ($groups as $group => $contexts) {
    $group_options[$group] = $group === 'not_grouped' ? $this
      ->t('Not grouped') : $group;
  }

  // Count the number of entities to get a good delta for the weights.
  $weight_delta = round(count($this
    ->getEntityIds()) / 2);
  foreach ($groups as $group => $contexts) {
    $group_class = Html::getClass($group);
    $form['contexts']['#tabledrag'][] = [
      'action' => 'match',
      'relationship' => 'sibling',
      'group' => 'context-group-select',
      'subgroup' => 'context-group-' . $group_class,
      'hidden' => FALSE,
    ];
    $form['contexts']['#tabledrag'][] = [
      'action' => 'order',
      'relationship' => 'sibling',
      'group' => 'context-weight',
      'subgroup' => 'context-weight-' . $group_class,
    ];
    $form['contexts']['group-' . $group_class] = [
      '#attributes' => [
        'class' => [
          'group-label',
          'group-label-' . $group_class,
        ],
        'no_striping' => TRUE,
      ],
    ];
    $form['contexts']['group-' . $group_class] = [
      '#attributes' => [
        'class' => [
          'region-title',
        ],
      ],
      'title' => [
        '#markup' => $group === 'not_grouped' ? $this
          ->t('Not grouped') : $group,
        '#wrapper_attributes' => [
          'colspan' => 5,
        ],
      ],
    ];

    /** @var \Drupal\context\ContextInterface $context */
    foreach ($contexts as $context_id => $context) {
      $operations = [
        'edit' => [
          'title' => $this
            ->t('Edit'),
          'url' => $context
            ->toUrl('edit-form'),
        ],
        'duplicate' => [
          'title' => $this
            ->t('Duplicate'),
          'url' => $context
            ->toUrl('duplicate-form'),
          'attributes' => $this
            ->getAjaxAttributes(),
        ],
        'delete' => [
          'title' => $this
            ->t('Delete'),
          'url' => $context
            ->toUrl('delete-form'),
          'attributes' => $this
            ->getAjaxAttributes(),
        ],
        'disable' => [
          'title' => $context
            ->disabled() ? $this
            ->t('Enable') : $this
            ->t('Disable'),
          'url' => $context
            ->toUrl('disable-form'),
          'attributes' => $this
            ->getAjaxAttributes(),
        ],
      ];
      $form['contexts'][$context_id] = [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'label' => [
          '#markup' => $context
            ->getLabel(),
          '#wrapper_attributes' => $context
            ->disabled() ? [
            'style' => 'opacity:0.6',
          ] : NULL,
        ],
        'description' => [
          '#markup' => $context
            ->getDescription(),
        ],
        'group' => [
          '#type' => 'select',
          '#title' => $this
            ->t('Group for @context context', [
            '@context' => $context
              ->getLabel(),
          ]),
          '#title_display' => 'invisible',
          '#default_value' => $context
            ->getGroup(),
          '#options' => $group_options,
          '#attributes' => [
            'class' => [
              'context-group-select',
              'context-group-' . $group_class,
            ],
          ],
        ],
        'weight' => [
          '#type' => 'weight',
          '#title' => $this
            ->t('Weight for @context context', [
            '@context' => $context
              ->getLabel(),
          ]),
          '#default_value' => $context
            ->getWeight(),
          '#delta' => $weight_delta,
          '#title_display' => 'invisible',
          '#attributes' => [
            'class' => [
              'context-weight',
              'context-weight-' . $group_class,
            ],
          ],
        ],
        'operations' => [
          '#type' => 'operations',
          '#links' => $operations,
        ],
      ];
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  if (count($groups) > 0) {
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save contexts'),
      '#button_type' => 'primary',
    ];
  }
  return $form;
}