You are here

public function PhotosImageSearch::buildSearchUrlQuery in Album Photos 8.5

Same name and namespace in other branches
  1. 6.0.x src/Plugin/Search/PhotosImageSearch.php \Drupal\photos\Plugin\Search\PhotosImageSearch::buildSearchUrlQuery()

Builds the URL GET query parameters array for search.

When the search form is submitted, a redirect is generated with the search input as GET query parameters. Plugins using the searchFormAlter() method to add form elements to the search form will need to override this method to gather the form input and add it to the GET query parameters.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The form state, with submitted form information.

Return value

array An array of GET query parameters containing all relevant form values to process the search. The 'keys' element must be present in order to trigger generation of search results, even if it is empty or unused by the search plugin.

Overrides SearchPluginBase::buildSearchUrlQuery

See also

SearchInterface::searchFormAlter()

File

src/Plugin/Search/PhotosImageSearch.php, line 704

Class

PhotosImageSearch
Handles searching for photos_image entities using the Search module index.

Namespace

Drupal\photos\Plugin\Search

Code

public function buildSearchUrlQuery(FormStateInterface $form_state) {

  // Read keyword and advanced search information from the form values,
  // and put these into the GET parameters.
  $keys = trim($form_state
    ->getValue('keys'));
  $advanced = FALSE;

  // Collect extra filters.
  $filters = [];
  if ($form_state
    ->hasValue('album_id') && is_array($form_state
    ->getValue('album_id'))) {

    // Retrieve selected album_ids - Form API sets the value of unselected
    // checkboxes to 0.
    foreach ($form_state
      ->getValue('album_id') as $albumId) {
      if ($albumId) {
        $advanced = TRUE;
        $filters[] = 'album_id:' . $albumId;
      }
    }
  }
  if ($form_state
    ->hasValue('language') && is_array($form_state
    ->getValue('language'))) {
    foreach ($form_state
      ->getValue('language') as $language) {
      if ($language) {
        $advanced = TRUE;
        $filters[] = 'language:' . $language;
      }
    }
  }
  if ($form_state
    ->getValue('or') != '') {
    if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' ' . $form_state
      ->getValue('or'), $matches)) {
      $keys .= ' ' . implode(' OR ', $matches[1]);
      $advanced = TRUE;
    }
  }
  if ($form_state
    ->getValue('negative') != '') {
    if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' ' . $form_state
      ->getValue('negative'), $matches)) {
      $keys .= ' -' . implode(' -', $matches[1]);
      $advanced = TRUE;
    }
  }
  if ($form_state
    ->getValue('phrase') != '') {
    $keys .= ' "' . str_replace('"', ' ', $form_state
      ->getValue('phrase')) . '"';
    $advanced = TRUE;
  }
  $keys = trim($keys);

  // Put the keywords and advanced parameters into GET parameters. Make sure
  // to put keywords into the query even if it is empty, because the page
  // controller uses that to decide it's time to check for search results.
  $query = [
    'keys' => $keys,
  ];
  if ($filters) {
    $query['f'] = $filters;
  }

  // Record that the person used the advanced search form, if they did.
  if ($advanced) {
    $query[self::ADVANCED_FORM] = '1';
  }
  return $query;
}