You are here

protected function ChannelForm::buildFiltersTable in Entity Share 8

Same name and namespace in other branches
  1. 8.3 modules/entity_share_server/src/Form/ChannelForm.php \Drupal\entity_share_server\Form\ChannelForm::buildFiltersTable()
  2. 8.2 modules/entity_share_server/src/Form/ChannelForm.php \Drupal\entity_share_server\Form\ChannelForm::buildFiltersTable()

Helper function to generate filter form elements.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

Throws

\LogicException

\Exception

1 call to ChannelForm::buildFiltersTable()
ChannelForm::form in modules/entity_share_server/src/Form/ChannelForm.php
Gets the actual form array to be built.

File

modules/entity_share_server/src/Form/ChannelForm.php, line 408

Class

ChannelForm
Class ChannelForm.

Namespace

Drupal\entity_share_server\Form

Code

protected function buildFiltersTable(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\entity_share_server\Entity\ChannelInterface $channel */
  $channel = $this->entity;
  $channel_filters = $channel
    ->get('channel_filters');
  if (is_null($channel_filters)) {
    $channel_filters = [];
  }
  $form['channel_filters'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Filters'),
  ];
  if ($channel
    ->isNew()) {
    $form['channel_filters']['filter_message'] = [
      '#markup' => $this
        ->t("It will be possible to add filters after the channel's creation."),
    ];
  }
  else {
    $form['channel_filters']['filter_actions'] = [
      '#type' => 'actions',
      '#weight' => -5,
    ];
    $form['channel_filters']['filter_actions']['filter_add'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Add a new filter'),
      '#url' => Url::fromRoute('entity_share_server.filter_add_form', [
        'channel' => $channel
          ->id(),
      ]),
      '#attributes' => [
        'class' => [
          'button',
          'button--primary',
        ],
      ],
    ];
    $header = [
      'id' => [
        'data' => $this
          ->t('ID'),
      ],
      'path' => [
        'data' => $this
          ->t('Path'),
      ],
      'operator' => [
        'data' => $this
          ->t('Operator'),
      ],
      'value' => [
        'data' => $this
          ->t('Value'),
      ],
      'group' => [
        'data' => $this
          ->t('Group'),
      ],
      'operations' => [
        'data' => $this
          ->t('Operations'),
      ],
    ];
    $rows = [];
    foreach ($channel_filters as $filter_id => $filter) {
      $operations = [
        '#type' => 'dropbutton',
        '#links' => [
          'edit' => [
            'title' => $this
              ->t('Edit'),
            'url' => Url::fromRoute('entity_share_server.filter_edit_form', [
              'channel' => $channel
                ->id(),
              'filter' => $filter_id,
            ]),
          ],
          'delete' => [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('entity_share_server.filter_delete_form', [
              'channel' => $channel
                ->id(),
              'filter' => $filter_id,
            ]),
          ],
        ],
      ];
      $row = [
        'id' => $filter_id,
        'path' => $filter['path'],
        'operator' => $filter['operator'],
        'value' => '',
        'filter' => isset($filter['memberof']) ? $filter['memberof'] : '',
        'operations' => $this->renderer
          ->render($operations),
      ];
      if (isset($filter['value'])) {
        $value = [
          '#theme' => 'item_list',
          '#items' => $filter['value'],
        ];
        $row['value'] = $this->renderer
          ->render($value);
      }
      $rows[] = $row;
    }
    $form['channel_filters']['filter_table'] = [
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => $this
        ->t('There is currently no filter for this channel.'),
    ];
  }
}