You are here

view_alias.module in View Alias 6.2

Same filename and directory in other branches
  1. 5 view_alias.module
  2. 6 view_alias.module
  3. 7 view_alias.module

Hook implementations for view alias module integration.

File

view_alias.module
View source
<?php

/**
 * @file
 * Hook implementations for view alias module integration.
 *
 * @ingroup pathauto
 */

/**
 * Implementation of hook_pathauto()
 */
function view_alias_pathauto($op) {
  switch ($op) {
    case 'settings':
      $settings = array();
      $settings['module'] = 'view_alias';
      $settings['token_type'] = 'view_alias';
      $settings['groupheader'] = t('View Alias settings');
      $settings['patterndescr'] = t('Default View Alias pattern (applies to all views with a term argument and page display)');
      $settings['patterndefault'] = '';
      $settings['bulkname'] = t('Bulk generate aliases for views with term arguments that are not aliased');
      $settings['bulkdescr'] = t('Generate aliases for all existing views with term arguments which do not already have aliases.');

      // left out patterns since we dont use them.
      $settings['placeholders'] = array(
        '[cat-raw]',
        t('The View argument taxonomy name'),
      );
      $settings['patternitems'] = array();
      return (object) $settings;
    default:
      break;
  }
}

/**
 *
 * Do the bulk updating for view aliases
 *
 */
function view_alias_pathauto_bulkupdate() {
  $aliasable = _get_aliasable_displays();
  foreach ($aliasable as $alias) {
    if (variable_get("pathauto_view_alias_{$alias->path}", FALSE)) {
      $terms = _gather_terms_for_alias($alias->varg);
      foreach ($terms as $term) {
        view_alias_create_alias($term, $alias, 'bulkupdate');
      }
    }
  }
}
function _gather_terms_for_alias($validator_vocab_ids) {
  $vocabs = explode(",", $validator_vocab_ids);
  $terms_to_alias = array();
  foreach ($vocabs as $vocab) {
    $terms = taxonomy_get_tree($vocab);
    $terms_to_alias = array_merge($terms_to_alias, $terms);
  }
  return $terms_to_alias;
}

/**
 *
 * Implementation of hook_path_alais_types from pathauto
 * allows me to hook into the bulk delete
 *
 */
function view_alias_path_alias_types() {
  $aliasable = _get_aliasable_displays();
  foreach ($aliasable as $alias) {
    $objects[$alias->path] = t('View page displays having a path of %path', array(
      '%path' => $alias->path,
    ));
  }
  return $objects;
}

/**
 *
 * Implementation of hook_form_alter
 * remove the default form settings and add our own since view alias are different from the regular
 * alaises
 *
 */
function view_alias_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'pathauto_admin_settings') {
    unset($form['view_alias']['pathauto_view_alias_pattern']);
    unset($form['view_alias']['token_help']);
    $form['view_alias']['views_to_alias'] = array(
      '#type' => 'fieldset',
      '#title' => t('Views available for aliasing'),
      '#description' => t('Check the views for which aliases should be bulk generated.'),
      '#weight' => -1,
    );
    $aliasable = _get_aliasable_displays();
    foreach ($aliasable as $alias) {
      $voc = taxonomy_vocabulary_load($alias->varg);
      $form['view_alias']['views_to_alias']["pathauto_view_alias_{$alias->path}"] = array(
        '#type' => 'checkbox',
        '#title' => t('View %view, display %display, on path %path, with %vocab arguments.', array(
          '%view' => $alias->view_name,
          '%display' => $alias->display_name,
          '%path' => $alias->path,
          '%vocab' => $voc->name,
        )),
        '#weight' => -1,
        '#default_value' => variable_get("pathauto_view_alias_{$alias->path}", 0),
      );
    }
  }
}

/**
 * Implementation of hook_taxonomy().
 */
function view_alias_taxonomy($op, $type, $array = NULL) {
  if ($type == 'term') {
    $term = (object) $array;
    $aliasable = _get_aliasable_displays();
    foreach ($aliasable as $alias) {
      if ($alias->varg == $term->vid) {
        if ($op == 'delete') {
          view_alias_delete_alias($term, $alias);
        }
        else {
          view_alias_create_alias($term, $alias, $op);
        }
      }
    }
  }
}

/**
 * Given a term, generate its view aliases.
 */
function view_alias_create_alias($term, $alias, $op) {
  module_load_include('inc', 'pathauto');

  // switch around the pattern based on if the term id is contained within the new alias
  if (strpos($alias->path, '%') === FALSE) {
    $source = "{$alias->path}/{$term->tid}";
    $GLOBALS['conf']['pathauto_view_alias_pattern'] = $alias->path . "/[cat-raw]";
  }
  else {
    $source = str_replace('%', $term->tid, $alias->path);
    $GLOBALS['conf']['pathauto_view_alias_pattern'] = str_replace('%', '[cat-raw]', $alias->path);
  }
  $placeholders = array(
    'tokens' => array(
      '[cat-raw]',
    ),
    'values' => array(
      pathauto_cleanstring($term->name),
    ),
  );
  $alias_path = pathauto_create_alias('view_alias', $op, $placeholders, $source, $alias->path);
  unset($GLOBALS['conf']['pathauto_view_alias_pattern']);
  return $alias_path;
}

/**
 * Delete an alias set by View Alias.
 */
function view_alias_delete_alias($term, $alias) {
  $alias_path = view_alias_create_alias($term, $alias, 'update');
  path_set_alias(NULL, $alias_path);
}

/**
 *
 * find the views that can be aliased.
 * that means have a path url and use a term id as an argument
 * build and array of objects, keyed with the view name,
 * having the view path, and the vocab id for the terms used
 * array(
 *   0 =>
 *     object (
 *       'view_name' -> 'viewname'
 *       'display_name' -> 'display name'
 *       'path' -> 'view url path'
 *       'varg' -> 'vocabulary id'
 *     )
 * )
 */
function _get_aliasable_displays() {
  $aliasable_views = array();
  $views = views_get_all_views();
  foreach ($views as $view) {
    if (!empty($view->disabled)) {
      continue;
    }
    if (empty($view->display)) {
      continue;
    }
    $alias = new stdClass();
    $alias->view_name = $view->name;
    foreach ($view->display as $key => $display) {

      // check default for args and save for later
      if ($key == 'default') {
        $default_varg = _find_view_arguments($display);
        continue;
      }

      // Skip displays with no path
      if (empty($display->display_options['path'])) {
        continue;
      }

      // Add the display name, path and replace overridden args.
      $alias->display_name = $key;
      $alias->path = $display->display_options['path'];
      if (isset($display->display_options['defaults']['arguments']) && $display->display_options['defaults']['arguments'] === FALSE) {
        $alias->varg = _find_view_arguments($display);
      }
      else {

        // Restore default varg -- previous displays may have been overridden
        $alias->varg = $default_varg;
      }
      if (!empty($alias->path) && !empty($alias->varg)) {
        $aliasable_views[] = drupal_clone($alias);
      }
    }
  }
  return $aliasable_views;
}

/**
 * helper to dig out the view arguments.
 */
function _find_view_arguments($display) {

  // No arguments?  Return FALSE.
  if (empty($display->display_options['arguments'])) {
    return FALSE;
  }

  // Scan the display for the first term arg.
  // (No multiple argument support, yet)
  $view_args = FALSE;
  foreach ($display->display_options['arguments'] as $arg_name => $argument) {

    // If we don't support all arguments, we can't create an alias.
    if ($argument['validate_type'] != 'taxonomy_term') {
      return FALSE;
    }
    $active_vocabularies = array_filter($argument['validate_argument_vocabulary']);
    $view_args = implode(',', $active_vocabularies);
  }
  return $view_args ? $view_args : FALSE;
}

Functions

Namesort descending Description
view_alias_create_alias Given a term, generate its view aliases.
view_alias_delete_alias Delete an alias set by View Alias.
view_alias_form_alter Implementation of hook_form_alter remove the default form settings and add our own since view alias are different from the regular alaises
view_alias_pathauto Implementation of hook_pathauto()
view_alias_pathauto_bulkupdate Do the bulk updating for view aliases
view_alias_path_alias_types Implementation of hook_path_alais_types from pathauto allows me to hook into the bulk delete
view_alias_taxonomy Implementation of hook_taxonomy().
_find_view_arguments helper to dig out the view arguments.
_gather_terms_for_alias
_get_aliasable_displays find the views that can be aliased. that means have a path url and use a term id as an argument build and array of objects, keyed with the view name, having the view path, and the vocab id for the terms used array( 0 => object…