You are here

handler_field_saved_search_name.inc in Search API Saved Searches 7

Contains the SearchApiSavedSearchesViewsHandlerFieldName class.

File

views/handler_field_saved_search_name.inc
View source
<?php

/**
 * @file
 * Contains the SearchApiSavedSearchesViewsHandlerFieldName class.
 */

/**
 * Views field handler for displaying a saved search's name, optionally linked to the search page.
 */
class SearchApiSavedSearchesViewsHandlerFieldName extends views_handler_field {

  /**
   * The ID of the currently rendered search.
   *
   * @var int|null
   */
  protected $currentSearchId;
  public function option_definition() {
    $options = parent::option_definition();
    $options['link_to_page'] = array(
      'default' => TRUE,
    );
    return $options;
  }
  public function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form['link_to_page'] = array(
      '#type' => 'checkbox',
      '#title' => t('Link to search page'),
      '#default_value' => $this->options['link_to_page'],
    );
  }
  public function render($values) {
    $this->currentSearchId = isset($values->id) ? $values->id : NULL;
    return parent::render($values);
  }
  public function render_text($alter) {
    if ($this->options['link_to_page'] && $this->currentSearchId) {
      $search = search_api_saved_search_load($this->currentSearchId);
      if ($search && search_api_saved_search_edit_access(NULL, $search) && !empty($search->options['page'])) {
        $alter['make_link'] = TRUE;
        $page = $search->options['page'] + array(
          'path' => NULL,
          'query' => array(),
        );
        $alter['path'] = $page['path'];
        $alter['query'] = $page['query'];
      }
    }
    return parent::render_text($alter);
  }

  /**
   * Render this field as a link, with info from a fieldset set by the user.
   *
   * Overwritten to fix query parameters for some setups.
   *
   * @see https://www.drupal.org/project/views/issues/2909709#comment-12275897
   */
  public function render_as_link($alter, $text, $tokens) {
    $value = '';
    if (!empty($alter['prefix'])) {
      $value .= filter_xss_admin(strtr($alter['prefix'], $tokens));
    }
    $options = array(
      'html' => TRUE,
      'absolute' => !empty($alter['absolute']) ? TRUE : FALSE,
    );

    // $path will be run through check_url() by l() so we do not need to
    // sanitize it ourselves.
    $path = $alter['path'];

    // strip_tags() removes <front>, so check whether its different to front.
    if ($path != '<front>') {

      // Use strip tags as there should never be HTML in the path. However, we
      // need to preserve special characters like " that were removed by
      // check_plain().
      $path = strip_tags(decode_entities(strtr($path, $tokens)));
      if (!empty($alter['path_case']) && $alter['path_case'] != 'none') {
        $path = $this
          ->case_transform($path, $this->options['alter']['path_case']);
      }
      if (!empty($alter['replace_spaces'])) {
        $path = str_replace(' ', '-', $path);
      }
      if (!empty($alter['unwanted_characters'])) {
        foreach (explode(' ', $alter['unwanted_characters']) as $unwanted) {
          $path = str_replace($unwanted, '', $path);
        }
      }
    }

    // Parse the URL and move any query and fragment parameters out of the path.
    $url = parse_url($path);

    // Seriously malformed URLs may return FALSE or empty arrays.
    if (empty($url)) {
      return $text;
    }

    // If the path is empty do not build a link around the given text and return
    // it as is. http://www.example.com URLs will not have a $url['path'], so
    // check host as well.
    if (empty($url['path']) && empty($url['host']) && empty($url['fragment'])) {
      return $text;
    }

    // If no scheme is provided in the $path, assign the default 'http://'.
    // This allows a url of 'www.example.com' to be converted to
    // 'http://www.example.com'. Only do this on for external URLs.
    if ($alter['external']) {
      if (!isset($url['scheme'])) {

        // There is no scheme, add the default 'http://' to the $path.
        $path = "http://{$path}";

        // Reset the $url array to include the new scheme.
        $url = parse_url($path);
      }
    }
    if (isset($url['query'])) {
      $path = strtr($path, array(
        '?' . $url['query'] => '',
      ));
      $query = drupal_get_query_array($url['query']);

      // Remove query parameters that were assigned a query string replacement
      // token for which there is no value available.
      foreach ($query as $param => $val) {
        if ($val == '%' . $param) {
          unset($query[$param]);
        }
      }
      $options['query'] = $query;
    }
    if (isset($url['fragment'])) {
      $path = strtr($path, array(
        '#' . $url['fragment'] => '',
      ));

      // If the path is empty we want to have a fragment for the current site.
      if ($path == '') {
        $options['external'] = TRUE;
      }
      $options['fragment'] = $url['fragment'];
    }
    $alt = strtr($alter['alt'], $tokens);

    // Set the title attribute of the link only if it improves accessibility
    if ($alt && $alt != $text) {
      $options['attributes']['title'] = decode_entities($alt);
    }
    $class = strtr($alter['link_class'], $tokens);
    if ($class) {
      $options['attributes']['class'] = array(
        $class,
      );
    }
    if (!empty($alter['rel']) && ($rel = strtr($alter['rel'], $tokens))) {
      $options['attributes']['rel'] = $rel;
    }
    $target = check_plain(trim(strtr($alter['target'], $tokens)));
    if (!empty($target)) {
      $options['attributes']['target'] = $target;
    }

    // Allow the addition of arbitrary attributes to links. Additional
    // attributes currently can only be altered in preprocessors and not within
    // the UI.
    if (isset($alter['link_attributes']) && is_array($alter['link_attributes'])) {
      foreach ($alter['link_attributes'] as $key => $attribute) {
        if (!isset($options['attributes'][$key])) {
          $options['attributes'][$key] = strtr($attribute, $tokens);
        }
      }
    }

    // If the query and fragment were programmatically assigned overwrite any
    // parsed values.
    if (isset($alter['query'])) {

      // Convert the query to a string, perform token replacement, and then
      // convert back to an array form for l().
      $options['query'] = http_build_query($alter['query']);
      $options['query'] = strtr($options['query'], $tokens);
      parse_str($options['query'], $options['query']);
    }
    if (isset($alter['alias'])) {

      // Alias is a boolean field, so no token.
      $options['alias'] = $alter['alias'];
    }
    if (isset($alter['fragment'])) {
      $options['fragment'] = strtr($alter['fragment'], $tokens);
    }
    if (isset($alter['language'])) {
      $options['language'] = $alter['language'];
    }

    // If the url came from entity_uri(), pass along the required options.
    if (isset($alter['entity'])) {
      $options['entity'] = $alter['entity'];
    }
    if (isset($alter['entity_type'])) {
      $options['entity_type'] = $alter['entity_type'];
    }
    $value .= l($text, $path, $options);
    if (!empty($alter['suffix'])) {
      $value .= filter_xss_admin(strtr($alter['suffix'], $tokens));
    }
    return $value;
  }

}

Classes

Namesort descending Description
SearchApiSavedSearchesViewsHandlerFieldName Views field handler for displaying a saved search's name, optionally linked to the search page.