You are here

views_url_alias_node.module in Views URL alias 6

Same filename and directory in other branches
  1. 8 views_url_alias_node.module
  2. 7 views_url_alias_node.module

Allows node related Views to be filtered by path aliases.

File

views_url_alias_node.module
View source
<?php

/**
 * @file
 * Allows node related Views to be filtered by path aliases.
 */

/**
 * Implementation of hook_views_api().
 */
function views_url_alias_node_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'views_url_alias_node'),
  );
}

/**
 * Implementation of hook_menu().
 */
function views_url_alias_node_menu() {
  $items['admin/build/views/tools/alias/node'] = array(
    'title' => 'Rebuild alias node table',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'views_url_alias_node_admin',
    ),
    'access arguments' => array(
      'administer views',
    ),
    'file' => 'views_url_alias_node.admin.inc',
    'file path' => drupal_get_path('module', 'views_url_alias_node'),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

////////////////////////////////////////////////////////////////////////////////

// Pathauto hooks

////////////////////////////////////////////////////////////////////////////////

/**
 * Implementation of hook_pathauto_alias_alter().
 */
function views_url_alias_node_pathauto_alias_alter(&$alias, array $context) {

  // Save node alias during bulk update.
  if ($context['module'] == 'node' && $context['op'] == 'bulkupdate' && preg_match('#^node/(\\d+)$#', $context['source'], $match)) {
    $nid = $match[1];
    views_url_alias_node_save($nid, $alias);
  }
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function views_url_alias_node_form_pathauto_admin_delete_alter(&$form, &$form_state) {
  $form['#submit'][] = 'views_url_alias_node_form_pathauto_admin_delete_submit';
}

/**
 * Submit processing for bulk delete of aliases.
 */
function views_url_alias_node_form_pathauto_admin_delete_submit(&$form, &$form_state) {

  // Purge view_url_alias_node table if delete all or node alises is requested.
  if ($form_state['values']['all_aliases'] || $form_state['values']['node/']) {
    db_query("TRUNCATE TABLE {views_url_alias_node}");
  }
}

////////////////////////////////////////////////////////////////////////////////

// Node hooks

////////////////////////////////////////////////////////////////////////////////

/**
 * Implementation of hook_nodeapi().
 */
function views_url_alias_node_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
      $alias = isset($node->path) ? $node->path : NULL;
      views_url_alias_node_save($node->nid, $alias);
      return;
    case 'delete':
      views_url_alias_node_delete($node->nid);
      return;
    default:
      return;
  }
}

////////////////////////////////////////////////////////////////////////////////

// API functions

////////////////////////////////////////////////////////////////////////////////

/**
 * Save node URL alias.
 */
function views_url_alias_node_save($nid, $alias = NULL) {
  db_query('DELETE FROM {views_url_alias_node} WHERE nid=%d', $nid);
  if (empty($alias)) {
    return;
  }
  db_query("INSERT INTO {views_url_alias_node} (nid, dst) VALUE (%d, '%s')", $nid, $alias);
}

/**
 * Delete's node URL alias.
 */
function views_url_alias_node_delete($nid) {
  views_url_alias_node_save($nid);
}

/**
 * Rebuild view_url_alias_node table.
 *
 * This function typically only needs to be called when this module is installed.
 */
function views_url_alias_node_rebuild() {

  // Purge view_url_alias_node table.
  db_query("TRUNCATE TABLE {views_url_alias_node}");

  // Populate view_url_alias_node table.
  db_query("INSERT INTO {views_url_alias_node} (nid, dst)\n    SELECT n.nid, ua.dst FROM {url_alias} ua INNER JOIN node AS n ON CONCAT('node/', n.nid) = ua.src WHERE ua.language=''");

  // Display message
  drupal_set_message(t('The %table table has been successfully rebuilt.', array(
    '%table' => 'views_url_alias_node',
  )));
}

Functions