You are here

public function SuggestionSearchForm::buildForm in Autocomplete Search Suggestions 8.2

Same name and namespace in other branches
  1. 8 src/Form/SuggestionSearchForm.php \Drupal\suggestion\Form\SuggestionSearchForm::buildForm()
  2. 3.0.x 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 65

Class

SuggestionSearchForm
Ngram search form.

Namespace

Drupal\suggestion\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $ngram = '') {
  $langcode = $this->langMgr
    ->getCurrentLanguage()
    ->getId();
  $languages = $this->langMgr
    ->getLanguages();
  $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('Language'),
    $this
      ->t('Quantity'),
    $this
      ->t('Density'),
    $this
      ->t('Edit'),
  ];
  if ($ngram) {
    $pattern = '%' . $this->dbh
      ->escapeLike($ngram) . '%';
    $page = $this->pagerMgr
      ->createPager(Storage::getCount($langcode, $pattern), $rpp);
    $suggestions = Storage::search($pattern, $langcode, $page * $rpp, $rpp);
  }
  else {
    $page = $this->pagerMgr
      ->createPager(Storage::getCount($langcode), $rpp);
    $suggestions = Storage::getAllSuggestions($langcode, $page * $rpp, $rpp);
  }
  foreach ($suggestions as $obj) {
    $rows[$obj->ngram] = [
      $obj->ngram,
      $obj->src,
      $obj->atoms,
      !empty($languages[$obj->langcode]) ? $languages[$obj->langcode]
        ->getName() : $this
        ->t('Undefined'),
      $obj->qty,
      $obj->density,
      Link::fromTextAndUrl($this
        ->t('Edit'), Url::fromUri("internal:/admin/config/suggestion/edit/{$obj->ngram}", $opts)),
    ];
  }
  if ($this->langMgr
    ->isMultilingual()) {
    $form += $this
      ->multiLinks($languages);
  }
  $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;
}