You are here

neutral_paths.module in Neutral paths 8

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

Neutral paths.

A module to have language neutral path aliases.

This is useful on a site with content in a single language with user languages different from the site language.

The module is not meant to be used with content in multiple languages, see https://www.drupal.org/node/2336037

File

neutral_paths.module
View source
<?php

/**
 * @file
 * Neutral paths.
 *
 * A module to have language neutral path aliases.
 *
 * This is useful on a site with content in a single language with user
 * languages different from the site language.
 *
 * The module is not meant to be used with content in multiple languages, see
 * https://www.drupal.org/node/2336037
 */
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function neutral_paths_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'neutral_paths.settings.form':
      $output = '<p>' . t('This page allows to select the path types to set as language neutral.') . '</br>';
      $output .= t('Language neutral paths will be accessible from any language.') . '</p>';
      $output .= '<p>' . t('Note that this setting will affect only the aliases created or updated after saving the configuration, to apply the change to old aliases use the <a href=":bulk_generate">Bulk generate</a> functionality.', [
        ':bulk_generate' => Url::fromRoute('pathauto.bulk.update.form')
          ->toString(),
      ]) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_pathauto_alias_alter().
 */
function neutral_paths_pathauto_alias_alter(&$alias, array &$context) {
  $types = \Drupal::config('neutral_paths.settings')
    ->get('neutral_paths_fix_new');
  $type = $context['pattern']
    ->getType();
  if (in_array($type, $types)) {
    $context['language'] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
  }
}