You are here

facetapi_pretty_paths.module in Facet API Pretty Paths 6.3

Same filename and directory in other branches
  1. 7 facetapi_pretty_paths.module

The FacetAPI Pretty Paths module.

File

facetapi_pretty_paths.module
View source
<?php

/**
 * @file
 * The FacetAPI Pretty Paths module.
 */

// Include the pretty paths url processor class.
require_once dirname(__FILE__) . '/plugins/facetapi/url_processor_pretty_paths.inc';

/**
 * Implements hook_facetapi_url_processors().
 */
function facetapi_pretty_paths_facetapi_url_processors() {
  return array(
    'pretty_paths' => array(
      'handler' => array(
        'label' => t('Pretty paths'),
        'class' => 'FacetapiUrlProcessorPrettyPaths',
      ),
    ),
  );
}

/**
 * Allows for alterations to the searcher definitions.
 *
 * @param array &$searcher_info
 *   The return values of hook_facetapi_searcher_info() implementations.
 *
 * Implements hook_facetapi_searcher_info().
 */
function facetapi_pretty_paths_facetapi_searcher_info_alter(array &$searcher_info) {
  foreach ($searcher_info as &$info) {

    // Activate pretty paths optionally per searcher, as configured.
    $id = 'facetapi_pretty_paths_searcher_' . $info['name'];
    $info['url processor'] = variable_get($id, NULL) ? 'pretty_paths' : 'standard';
    $info['facetapi pretty paths coder'] = 'default';
  }
}

/**
 * Add pretty path settings to facet configuration.
 *
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function facetapi_pretty_paths_form_facetapi_facet_display_form_alter(&$form, &$form_state) {
  if (!user_access('administer facetapi pretty paths')) {
    return;
  }

  // Get global facet settings.
  $adapter = $form['#facetapi']['adapter'];
  $processor = new FacetapiUrlProcessorPrettyPaths($adapter);
  $pretty_paths_alias = $processor
    ->getFacetPrettyPathsAlias($form['#facetapi']['facet']);

  // Add pretty paths alias option to global facet settings.
  $form['global']['pretty_paths_alias'] = array(
    '#type' => 'textfield',
    '#title' => t('Pretty path alias'),
    '#default_value' => $pretty_paths_alias,
    '#description' => t('Pretty paths will be generated as "search/url/segment1/segment2/".') . '<br/>' . t('By default, a segment will look like: "@default_segment".', array(
      '@default_segment' => '<alias>/<value>',
    )) . '<br/>' . t('For taxonomy terms it outputs the id as well: "@taxonomy_segment".', array(
      '@taxonomy_segment' => '<alias>/<term-name>-<term-id>',
    )),
  );
}

/**
 * Implements hook_menu().
 */
function facetapi_pretty_paths_menu() {
  $items = array();
  $items['admin/settings/facetapi_pretty_paths'] = array(
    'title' => 'FacetAPI Pretty Paths',
    'description' => 'Configure pretty paths settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'facetapi_pretty_paths_admin_form',
    ),
    'access arguments' => array(
      'administer facetapi pretty paths',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function facetapi_pretty_paths_permission() {
  return array(
    'administer facetapi pretty paths' => array(
      'title' => t('Administer Facet API Pretty Paths'),
    ),
  );
}

/**
 * Administration form.
 */
function facetapi_pretty_paths_admin_form() {
  $form = array();

  // Allow to enable / disable pretty paths per searcher.
  $searcher_info = facetapi_get_searcher_info();
  $form['searcher'] = array(
    '#type' => 'fieldset',
    '#title' => t('Enable pretty paths per searcher'),
  );
  foreach ($searcher_info as &$info) {
    $id = 'facetapi_pretty_paths_searcher_' . $info['name'];
    $form['searcher'][$id] = array(
      '#type' => 'checkbox',
      '#title' => $info['name'],
      '#default_value' => variable_get($id, $info['url processor'] == 'pretty_paths'),
      '#description' => t("Use pretty paths for the @searcher_label", array(
        "@searcher_label" => $info['label'],
      )),
    );
  }
  return system_settings_form($form);
}

/**
 * Implements hook_ctools_plugin_type().
 */
function facetapi_pretty_paths_ctools_plugin_type() {
  $plugins['coders'] = array(
    'use hooks' => TRUE,
  );
  return $plugins;
}

/**
 * Implements hook_facetapi_pretty_paths_coders().
 */
function facetapi_pretty_paths_facetapi_pretty_paths_coders() {
  $coders = array(
    'default' => array(
      'handler' => array(
        'label' => t('Default pretty paths coder'),
        'class' => 'FacetApiPrettyPathsCoderDefault',
      ),
    ),
    'taxonomy' => array(
      'handler' => array(
        'label' => t('Taxonomy specific pretty paths coder'),
        'class' => 'FacetApiPrettyPathsCoderTaxonomy',
      ),
    ),
  );

  // Allow other modules to alter the coder definitions.
  drupal_alter('facetapi_pretty_paths_facetapi_pretty_paths_coders', $coders);
  return $coders;
}

/**
 * Implements hook_facetapi_facet_info().
 */
function facetapi_pretty_paths_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) {
  foreach ($facet_info as &$facet) {

    // Check for Apache Solr Taxonomy Term fields.
    if (!empty($facet_info['map options']['module_name']) && $facet_info['map options']['module_name'] == 'Taxonomy') {
      $facet['facetapi pretty paths coder'] = 'taxonomy';
      continue;
    }

    // Check for Search API Taxonomy Term fields.
    if (!empty($facet['field type']) && $facet['field type'] == 'taxonomy_term') {
      $facet['facetapi pretty paths coder'] = 'taxonomy';
      continue;
    }
  }
}

/**
 * Helper function for calling coder callbacks.
 */
function facetapi_pretty_paths_coder_callback($callback, $args) {
  $id = 'default';
  if (isset($args['facet']) && isset($args['facet']['facetapi pretty paths coder'])) {
    $id = $args['facet']['facetapi pretty paths coder'];
  }
  if ($class = ctools_plugin_load_class('facetapi_pretty_paths', 'coders', $id, 'handler')) {
    $args = func_get_args();

    // Remove $callback from the arguments.
    unset($args[0]);
    $instance = new $class();
    return call_user_func_array(array(
      $instance,
      $callback,
    ), $args);
  }
}

Functions

Namesort descending Description
facetapi_pretty_paths_admin_form Administration form.
facetapi_pretty_paths_coder_callback Helper function for calling coder callbacks.
facetapi_pretty_paths_ctools_plugin_type Implements hook_ctools_plugin_type().
facetapi_pretty_paths_facetapi_facet_info_alter Implements hook_facetapi_facet_info().
facetapi_pretty_paths_facetapi_pretty_paths_coders Implements hook_facetapi_pretty_paths_coders().
facetapi_pretty_paths_facetapi_searcher_info_alter Allows for alterations to the searcher definitions.
facetapi_pretty_paths_facetapi_url_processors Implements hook_facetapi_url_processors().
facetapi_pretty_paths_form_facetapi_facet_display_form_alter Add pretty path settings to facet configuration.
facetapi_pretty_paths_menu Implements hook_menu().
facetapi_pretty_paths_permission Implements hook_permission().