function NodeSearch::searchFormAlter

Same name in other branches
  1. 9 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::searchFormAlter()
  2. 8.9.x core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::searchFormAlter()
  3. 11.x core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::searchFormAlter()

Overrides SearchPluginBase::searchFormAlter

File

core/modules/node/src/Plugin/Search/NodeSearch.php, line 571

Class

NodeSearch
Handles searching for node entities using the Search module index.

Namespace

Drupal\node\Plugin\Search

Code

public function searchFormAlter(array &$form, FormStateInterface $form_state) {
    $parameters = $this->getParameters();
    $keys = $this->getKeywords();
    $used_advanced = !empty($parameters[self::ADVANCED_FORM]);
    if ($used_advanced) {
        $f = isset($parameters['f']) ? (array) $parameters['f'] : [];
        $defaults = $this->parseAdvancedDefaults($f, $keys);
    }
    else {
        $defaults = [
            'keys' => $keys,
        ];
    }
    $form['basic']['keys']['#default_value'] = $defaults['keys'];
    // Add advanced search keyword-related boxes.
    $form['advanced'] = [
        '#type' => 'details',
        '#title' => $this->t('Advanced search'),
        '#attributes' => [
            'class' => [
                'search-advanced',
            ],
        ],
        '#access' => $this->account && $this->account
            ->hasPermission('use advanced search'),
        '#open' => $used_advanced,
    ];
    $form['advanced']['keywords-fieldset'] = [
        '#type' => 'fieldset',
        '#title' => $this->t('Keywords'),
    ];
    $form['advanced']['keywords'] = [
        '#prefix' => '<div class="criterion">',
        '#suffix' => '</div>',
    ];
    $form['advanced']['keywords-fieldset']['keywords']['or'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Containing any of the words'),
        '#size' => 30,
        '#maxlength' => 255,
        '#default_value' => $defaults['or'] ?? '',
    ];
    $form['advanced']['keywords-fieldset']['keywords']['phrase'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Containing the phrase'),
        '#size' => 30,
        '#maxlength' => 255,
        '#default_value' => $defaults['phrase'] ?? '',
    ];
    $form['advanced']['keywords-fieldset']['keywords']['negative'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Containing none of the words'),
        '#size' => 30,
        '#maxlength' => 255,
        '#default_value' => $defaults['negative'] ?? '',
    ];
    // Add node types.
    $types = array_map([
        '\\Drupal\\Component\\Utility\\Html',
        'escape',
    ], node_type_get_names());
    $form['advanced']['types-fieldset'] = [
        '#type' => 'fieldset',
        '#title' => $this->t('Types'),
    ];
    $form['advanced']['types-fieldset']['type'] = [
        '#type' => 'checkboxes',
        '#title' => $this->t('Only of the type(s)'),
        '#prefix' => '<div class="criterion">',
        '#suffix' => '</div>',
        '#options' => $types,
        '#default_value' => $defaults['type'] ?? [],
    ];
    $form['advanced']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Advanced search'),
        '#prefix' => '<div class="action">',
        '#suffix' => '</div>',
        '#weight' => 100,
    ];
    // Add languages.
    $language_options = [];
    $language_list = $this->languageManager
        ->getLanguages(LanguageInterface::STATE_ALL);
    foreach ($language_list as $langcode => $language) {
        // Make locked languages appear special in the list.
        $language_options[$langcode] = $language->isLocked() ? $this->t('- @name -', [
            '@name' => $language->getName(),
        ]) : $language->getName();
    }
    if (count($language_options) > 1) {
        $form['advanced']['lang-fieldset'] = [
            '#type' => 'fieldset',
            '#title' => $this->t('Languages'),
        ];
        $form['advanced']['lang-fieldset']['language'] = [
            '#type' => 'checkboxes',
            '#title' => $this->t('Languages'),
            '#prefix' => '<div class="criterion">',
            '#suffix' => '</div>',
            '#options' => $language_options,
            '#default_value' => $defaults['language'] ?? [],
        ];
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.