You are here

public function SuggestionSearchForm::buildForm in Autocomplete Search Suggestions 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/SuggestionSearchForm.php \Drupal\suggestion\Form\SuggestionSearchForm::buildForm()
  2. 8 src/Form/SuggestionSearchForm.php \Drupal\suggestion\Form\SuggestionSearchForm::buildForm()

The suggestion search form.

Parameters

array $form: A drupal form array.

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

string $ngram: The search string.

Return value

array A Drupal form array.

Overrides FormInterface::buildForm

File

src/Form/SuggestionSearchForm.php, line 60

Class

SuggestionSearchForm
Ngram search form.

Namespace

Drupal\suggestion\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $ngram = '') {
  $ngram = trim($ngram);
  $opts = [
    'query' => $this->redirect
      ->getAsArray(),
  ];
  $rows = [];
  $rpp = Helper::getConfig('rpp');
  $header = [
    $this
      ->t('N-Gram'),
    $this
      ->t('Source'),
    $this
      ->t('Atoms'),
    $this
      ->t('Quantity'),
    $this
      ->t('Density'),
    $this
      ->t('Edit'),
  ];
  if ($ngram) {
    $pattern = '%' . $this->dbh
      ->escapeLike($ngram) . '%';
    $page = $this->pagerMgr
      ->createPager(Storage::getCount($pattern), $rpp);
    $suggestions = Storage::search($pattern, $page
      ->getCurrentPage() * $rpp, $rpp);
  }
  else {
    $page = $this->pagerMgr
      ->createPager(Storage::getCount(), $rpp);
    $suggestions = Storage::getAllSuggestions($page
      ->getCurrentPage() * $rpp, $rpp);
  }
  foreach ($suggestions as $obj) {
    $rows[$obj->ngram] = [
      $obj->ngram,
      $obj->src,
      $obj->atoms,
      $obj->qty,
      $obj->density,
      Link::fromTextAndUrl($this
        ->t('Edit'), Url::fromUri("internal:/admin/config/suggestion/edit/{$obj->ngram}", $opts)),
    ];
  }
  $form['ngram'] = [
    '#type' => 'textfield',
    '#autocomplete_route_name' => 'suggestion.autocomplete',
    '#default_value' => $ngram,
    '#weight' => 10,
  ];
  $form['search'] = [
    '#type' => 'submit',
    '#name' => 'search',
    '#value' => $this
      ->t('Search'),
    '#submit' => [
      '::submitForm',
    ],
    '#weight' => 20,
  ];
  $form['list'] = [
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#empty' => $this
      ->t('Nothing found.'),
    '#weight' => 60,
  ];
  if (count($rows)) {
    $form['src'] = [
      '#title' => $this
        ->t('Source'),
      '#type' => 'select',
      '#options' => Storage::getSrcOptions(),
      '#multiple' => TRUE,
      '#weight' => 30,
    ];
    $form['update'] = [
      '#type' => 'submit',
      '#name' => 'update',
      '#value' => $this
        ->t('Update'),
      '#submit' => [
        '::submitUpdateForm',
        '::submitForm',
      ],
      '#validate' => [
        '::validateUpdateForm',
      ],
      '#weight' => 40,
    ];
    $form['pager_head'] = [
      '#type' => 'pager',
      '#weight' => 50,
    ];
    $form['pager_foot'] = [
      '#type' => 'pager',
      '#weight' => 70,
    ];
  }
  return $form;
}