You are here

views_url_alias_node.module in Views URL alias 8

Same filename and directory in other branches
  1. 6 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.
 */
use Drupal\Core\Form\FormStateInterface;

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

// Pathauto hooks

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

/**
 * Implements 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);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function views_url_alias_node_form_pathauto_admin_delete_alter(&$form, FormStateInterface $form_state, $form_id) {
  $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, FormStateInterface $form_state) {

  // Purge view_url_alias_node table if delete all or node alises is requested.
  if ($form_state
    ->getValue('delete')['all_aliases'] || isset($form_state
    ->getValue('delete')['plugins']['canonical_entities:node']) && $form_state
    ->getValue('delete')['plugins']['canonical_entities:node']) {
    $database = \Drupal::database();
    $database
      ->truncate('views_url_alias_node')
      ->execute();
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function views_url_alias_node_form_path_admin_add_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#submit'][] = 'views_url_alias_node_form_path_admin_add_submit';
}
function views_url_alias_node_form_path_admin_add_submit($form, FormStateInterface $form_state) {
  $source = $form_state
    ->getValue('source');
  $alias = $form_state
    ->getValue('alias');
  if (preg_match('#^/node/(\\d+)$#', $source, $match)) {
    $nid = $match[1];
    views_url_alias_node_save($nid, $alias);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function views_url_alias_node_form_path_alias_delete_alter(&$form, FormStateInterface $form_state, $form_id) {

  // we need info from the alias record before it's deleted.
  array_unshift($form['#submit'], 'views_url_alias_node_form_path_alias_delete_submit');
}
function views_url_alias_node_form_path_alias_delete_submit($form, FormStateInterface $form_state) {
  $build_info = $form_state
    ->getBuildInfo();
  if (isset($build_info['args'][0])) {
    $alias_id = $build_info['args'][0];

    /** @var \Drupal\Core\Path\AliasStorage $alias_storage */
    $alias_storage = \Drupal::service('path.alias_storage');
    $alias = $alias_storage
      ->load([
      'pid' => $alias_id,
    ]);
    if (preg_match('#^/node/(\\d+)$#', $alias['source'], $match)) {
      $nid = $match[1];
      views_url_alias_node_delete($nid);
    }
  }
}

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

// Node hooks

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

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function views_url_alias_node_node_insert(Drupal\Core\Entity\EntityInterface $entity) {
  views_url_alias_node_node_update($entity);
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function views_url_alias_node_node_update(Drupal\Core\Entity\EntityInterface $entity) {
  try {
    $alias = $entity
      ->toUrl()
      ->toString();
  } catch (\Drupal\Core\Entity\EntityMalformedException $e) {
    watchdog_exception('views_url_alias_node', $e);
    return;
  }
  views_url_alias_node_save($entity
    ->id(), $alias);
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function views_url_alias_node_node_delete(Drupal\Core\Entity\EntityInterface $entity) {
  views_url_alias_node_delete($entity
    ->id());
}

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

// API functions

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

/**
 * Save node URL alias.
 */
function views_url_alias_node_save($nid, $alias = NULL) {
  $database = \Drupal::database();
  $database
    ->delete('views_url_alias_node')
    ->condition('nid', $nid)
    ->execute();
  if (empty($alias)) {
    return;
  }
  try {
    $database
      ->insert('views_url_alias_node')
      ->fields([
      'alias' => $alias,
      'nid' => $nid,
    ])
      ->execute();
  } catch (Exception $e) {
    watchdog_exception('views_url_alias_node', $e);
  }
}

/**
 * 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() {
  $database = \Drupal::database();

  // Purge view_url_alias_node table.
  $database
    ->truncate('views_url_alias_node')
    ->execute();

  // Build select query for insert.
  $query = $database
    ->select('url_alias', 'ua');
  $query
    ->join('node', 'n', "CONCAT('/node/', n.nid) = ua.source");
  $query
    ->fields('n', array(
    'nid',
  ))
    ->fields('ua', array(
    'alias',
  ));

  // Populate view_url_alias_node table.
  try {
    $database
      ->insert('views_url_alias_node')
      ->from($query)
      ->execute();
  } catch (Exception $e) {
    watchdog_exception('views_url_alias_node', $e);
  }

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