You are here

public function NodeSearch::searchFormAlter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::searchFormAlter()

Alters the search form when being built for a given plugin.

The core search module only invokes this method on active module plugins when building a form for them in \Drupal\search\Form\SearchPageForm::buildForm(). A plugin implementing this will also need to implement the buildSearchUrlQuery() method.

Parameters

array $form: Nested array of form elements that comprise the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. The arguments that \Drupal::formBuilder()->getForm() was originally called with are available in the array $form_state->getBuildInfo()['args'].

Overrides SearchPluginBase::searchFormAlter

See also

SearchInterface::buildSearchUrlQuery()

File

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

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' => 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' => t('Keywords'),
  ];
  $form['advanced']['keywords'] = [
    '#prefix' => '<div class="criterion">',
    '#suffix' => '</div>',
  ];
  $form['advanced']['keywords-fieldset']['keywords']['or'] = [
    '#type' => 'textfield',
    '#title' => t('Containing any of the words'),
    '#size' => 30,
    '#maxlength' => 255,
    '#default_value' => isset($defaults['or']) ? $defaults['or'] : '',
  ];
  $form['advanced']['keywords-fieldset']['keywords']['phrase'] = [
    '#type' => 'textfield',
    '#title' => t('Containing the phrase'),
    '#size' => 30,
    '#maxlength' => 255,
    '#default_value' => isset($defaults['phrase']) ? $defaults['phrase'] : '',
  ];
  $form['advanced']['keywords-fieldset']['keywords']['negative'] = [
    '#type' => 'textfield',
    '#title' => t('Containing none of the words'),
    '#size' => 30,
    '#maxlength' => 255,
    '#default_value' => isset($defaults['negative']) ? $defaults['negative'] : '',
  ];

  // Add node types.
  $types = array_map([
    '\\Drupal\\Component\\Utility\\Html',
    'escape',
  ], node_type_get_names());
  $form['advanced']['types-fieldset'] = [
    '#type' => 'fieldset',
    '#title' => t('Types'),
  ];
  $form['advanced']['types-fieldset']['type'] = [
    '#type' => 'checkboxes',
    '#title' => t('Only of the type(s)'),
    '#prefix' => '<div class="criterion">',
    '#suffix' => '</div>',
    '#options' => $types,
    '#default_value' => isset($defaults['type']) ? $defaults['type'] : [],
  ];
  $form['advanced']['submit'] = [
    '#type' => 'submit',
    '#value' => 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() ? t('- @name -', [
      '@name' => $language
        ->getName(),
    ]) : $language
      ->getName();
  }
  if (count($language_options) > 1) {
    $form['advanced']['lang-fieldset'] = [
      '#type' => 'fieldset',
      '#title' => t('Languages'),
    ];
    $form['advanced']['lang-fieldset']['language'] = [
      '#type' => 'checkboxes',
      '#title' => t('Languages'),
      '#prefix' => '<div class="criterion">',
      '#suffix' => '</div>',
      '#options' => $language_options,
      '#default_value' => isset($defaults['language']) ? $defaults['language'] : [],
    ];
  }
}