You are here

public function NodeSearch::searchFormAlter in Zircon Profile 8.0

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::form(). 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 512
Contains \Drupal\node\Plugin\Search\NodeSearch.

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'] : array();
    $defaults = $this
      ->parseAdvancedDefaults($f, $keys);
  }
  else {
    $defaults = array(
      'keys' => $keys,
    );
  }
  $form['basic']['keys']['#default_value'] = $defaults['keys'];

  // Add advanced search keyword-related boxes.
  $form['advanced'] = array(
    '#type' => 'details',
    '#title' => t('Advanced search'),
    '#attributes' => array(
      'class' => array(
        'search-advanced',
      ),
    ),
    '#access' => $this->account && $this->account
      ->hasPermission('use advanced search'),
    '#open' => $used_advanced,
  );
  $form['advanced']['keywords-fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Keywords'),
  );
  $form['advanced']['keywords'] = array(
    '#prefix' => '<div class="criterion">',
    '#suffix' => '</div>',
  );
  $form['advanced']['keywords-fieldset']['keywords']['or'] = array(
    '#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'] = array(
    '#type' => 'textfield',
    '#title' => t('Containing the phrase'),
    '#size' => 30,
    '#maxlength' => 255,
    '#default_value' => isset($defaults['phrase']) ? $defaults['phrase'] : '',
  );
  $form['advanced']['keywords-fieldset']['keywords']['negative'] = array(
    '#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(array(
    '\\Drupal\\Component\\Utility\\Html',
    'escape',
  ), node_type_get_names());
  $form['advanced']['types-fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Types'),
  );
  $form['advanced']['types-fieldset']['type'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Only of the type(s)'),
    '#prefix' => '<div class="criterion">',
    '#suffix' => '</div>',
    '#options' => $types,
    '#default_value' => isset($defaults['type']) ? $defaults['type'] : array(),
  );
  $form['advanced']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Advanced search'),
    '#prefix' => '<div class="action">',
    '#suffix' => '</div>',
    '#weight' => 100,
  );

  // Add languages.
  $language_options = array();
  $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 -', array(
      '@name' => $language
        ->getName(),
    )) : $language
      ->getName();
  }
  if (count($language_options) > 1) {
    $form['advanced']['lang-fieldset'] = array(
      '#type' => 'fieldset',
      '#title' => t('Languages'),
    );
    $form['advanced']['lang-fieldset']['language'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Languages'),
      '#prefix' => '<div class="criterion">',
      '#suffix' => '</div>',
      '#options' => $language_options,
      '#default_value' => isset($defaults['language']) ? $defaults['language'] : array(),
    );
  }
}