You are here

protected function ChannelForm::buildSearchesTable in Entity Share 8.3

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

Helper function to generate search 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::buildSearchesTable()
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 523

Class

ChannelForm
Entity form for the channel entity.

Namespace

Drupal\entity_share_server\Form

Code

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

  /** @var \Drupal\entity_share_server\Entity\ChannelInterface $channel */
  $channel = $this->entity;
  $channel_searches = $channel
    ->get('channel_searches');
  if (is_null($channel_searches)) {
    $channel_searches = [];
  }
  $form['channel_searches'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Searches'),
  ];

  // Add a warning message.
  $form['channel_searches']['warning_message'] = [
    '#theme' => 'status_messages',
    '#message_list' => [
      'warning' => [
        $this
          ->t('The label of the entity if it exists is automatically searchable. Do not add a search for that.'),
      ],
    ],
  ];
  if ($channel
    ->isNew()) {
    $form['channel_searches']['search_message'] = [
      '#markup' => $this
        ->t("It will be possible to add searches after the channel's creation."),
    ];
  }
  else {
    $form['channel_searches']['search_actions'] = [
      '#type' => 'actions',
      '#weight' => -5,
    ];
    $form['channel_searches']['search_actions']['search_add'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Add a new search'),
      '#url' => Url::fromRoute('entity_share_server.search_add_form', [
        'channel' => $channel
          ->id(),
      ]),
      '#attributes' => [
        'class' => [
          'button',
          'button--primary',
        ],
      ],
    ];
    $header = [
      'id' => [
        'data' => $this
          ->t('ID'),
      ],
      'path' => [
        'data' => $this
          ->t('Path'),
      ],
      'label' => [
        'data' => $this
          ->t('Label'),
      ],
      'operations' => [
        'data' => $this
          ->t('Operations'),
      ],
    ];
    $rows = [];
    foreach ($channel_searches as $search_id => $search) {
      $operations = [
        '#type' => 'dropbutton',
        '#links' => [
          'edit' => [
            'title' => $this
              ->t('Edit'),
            'url' => Url::fromRoute('entity_share_server.search_edit_form', [
              'channel' => $channel
                ->id(),
              'search' => $search_id,
            ]),
          ],
          'delete' => [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('entity_share_server.search_delete_form', [
              'channel' => $channel
                ->id(),
              'search' => $search_id,
            ]),
          ],
        ],
      ];
      $row = [
        'id' => $search_id,
        'path' => $search['path'],
        'label' => $search['label'],
        'operations' => $this->renderer
          ->render($operations),
      ];
      if (isset($search['value'])) {
        $value = [
          '#theme' => 'item_list',
          '#items' => $search['value'],
        ];
        $row['value'] = $this->renderer
          ->render($value);
      }
      $rows[] = $row;
    }
    $form['channel_searches']['search_table'] = [
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => $this
        ->t('There is currently no search for this channel.'),
    ];
  }
}