summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/path/path.module
blob: c307f7e6403c9b959198084c882c5a8c350a2541 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

/**
 * @file
 * Enables users to rename URLs.
 */

use Drupal\Core\Url;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\path\PathAliasForm;
use Drupal\path\PathAliasListBuilder;

/**
 * Implements hook_help().
 */
function path_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.path':
      $output = '';
      $output .= '<h2>' . t('About') . '</h2>';
      $output .= '<p>' . t('The Path module allows you to specify an alias, or custom URL, for any existing internal system path. Aliases should not be confused with URL redirects, which allow you to forward a changed or inactive URL to a new URL. In addition to making URLs more readable, aliases also help search engines index content more effectively. Multiple aliases may be used for a single internal system path. To automate the aliasing of paths, you can install the contributed module <a href=":pathauto">Pathauto</a>. For more information, see the <a href=":path">online documentation for the Path module</a>.', [':path' => 'https://www.drupal.org/documentation/modules/path', ':pathauto' => 'https://www.drupal.org/project/pathauto']) . '</p>';
      $output .= '<h2>' . t('Uses') . '</h2>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Creating aliases') . '</dt>';
      $output .= '<dd>' . t('If you create or edit a taxonomy term you can add an alias (for example <em>music/jazz</em>) in the field "URL alias". When creating or editing content you can add an alias (for example <em>about-us/team</em>) under the section "URL path settings" in the field "URL alias". Aliases for any other path can be added through the page <a href=":aliases">URL aliases</a>. To add aliases a user needs the permission <a href=":permissions">Create and edit URL aliases</a>.', [':aliases' => Url::fromRoute('entity.path_alias.collection')->toString(), ':permissions' => Url::fromRoute('user.admin_permissions.module', ['modules' => 'path'])->toString()]) . '</dd>';
      $output .= '<dt>' . t('Managing aliases') . '</dt>';
      $output .= '<dd>' . t('The Path module provides a way to search and view a <a href=":aliases">list of all aliases</a> that are in use on your website. Aliases can be added, edited and deleted through this list.', [':aliases' => Url::fromRoute('entity.path_alias.collection')->toString()]) . '</dd>';
      $output .= '</dl>';
      return $output;

    case 'entity.path_alias.collection':
      return '<p>' . t("An alias defines a different name for an existing URL path - for example, the alias 'about' for the URL path 'node/1'. A URL path can have multiple aliases.") . '</p>';

    case 'entity.path_alias.add_form':
      return '<p>' . t('Enter the path you wish to create the alias for, followed by the name of the new alias.') . '</p>';
  }
}

/**
 * Implements hook_entity_type_alter().
 */
function path_entity_type_alter(array &$entity_types) {
  // @todo Remove the conditional once core fully supports "path_alias" as an
  //   optional module. See https://drupal.org/node/3092090.
  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  if (isset($entity_types['path_alias'])) {
    $entity_types['path_alias']->setFormClass('default', PathAliasForm::class);
    $entity_types['path_alias']->setFormClass('delete', ContentEntityDeleteForm::class);
    $entity_types['path_alias']->setHandlerClass('route_provider', ['html' => AdminHtmlRouteProvider::class]);
    $entity_types['path_alias']->setListBuilderClass(PathAliasListBuilder::class);
    $entity_types['path_alias']->setLinkTemplate('collection', '/admin/config/search/path');
    $entity_types['path_alias']->setLinkTemplate('add-form', '/admin/config/search/path/add');
    $entity_types['path_alias']->setLinkTemplate('edit-form', '/admin/config/search/path/edit/{path_alias}');
    $entity_types['path_alias']->setLinkTemplate('delete-form', '/admin/config/search/path/delete/{path_alias}');
  }
}

/**
 * Implements hook_entity_base_field_info_alter().
 */
function path_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
  if ($entity_type->id() === 'path_alias') {
    $fields['langcode']->setDisplayOptions('form', [
      'type' => 'language_select',
      'weight' => 0,
      'settings' => [
        'include_locked' => FALSE,
      ],
    ]);

    $fields['path']->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => 5,
      'settings' => [
        'size' => 45,
      ],
    ]);

    $fields['alias']->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => 10,
      'settings' => [
        'size' => 45,
      ],
    ]);
  }
}

/**
 * Implements hook_entity_base_field_info().
 */
function path_entity_base_field_info(EntityTypeInterface $entity_type) {
  if (in_array($entity_type->id(), ['taxonomy_term', 'node', 'media'], TRUE)) {
    $fields['path'] = BaseFieldDefinition::create('path')
      ->setLabel(t('URL alias'))
      ->setTranslatable(TRUE)
      ->setDisplayOptions('form', [
        'type' => 'path',
        'weight' => 30,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setComputed(TRUE);

    return $fields;
  }
}

/**
 * Implements hook_entity_translation_create().
 */
function path_entity_translation_create(ContentEntityInterface $translation) {
  foreach ($translation->getFieldDefinitions() as $field_name => $field_definition) {
    if ($field_definition->getType() === 'path' && $translation->get($field_name)->pid) {
      // If there are values and a path ID, update the langcode and unset the
      // path ID to save this as a new alias.
      $translation->get($field_name)->langcode = $translation->language()->getId();
      $translation->get($field_name)->pid = NULL;
    }
  }
}

/**
 * Implements hook_field_widget_single_element_form_alter().
 */
function path_field_widget_single_element_form_alter(&$element, FormStateInterface $form_state, $context) {
  $field_definition = $context['items']->getFieldDefinition();
  $field_name = $field_definition->getName();
  $entity_type = $field_definition->getTargetEntityTypeId();
  $widget_name = $context['widget']->getPluginId();

  if ($entity_type === 'path_alias') {
    if (($field_name === 'path' || $field_name === 'alias') && $widget_name === 'string_textfield') {
      $element['value']['#field_prefix'] = \Drupal::service('router.request_context')->getCompleteBaseUrl();
    }

    if ($field_name === 'langcode') {
      $element['value']['#description'] = t('A path alias set for a specific language will always be used when displaying this page in that language, and takes precedence over path aliases set as <em>- Not specified -</em>.');
      $element['value']['#empty_value'] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
      $element['value']['#empty_option'] = t('- Not specified -');
    }

    if ($field_name === 'path') {
      $element['value']['#description'] = t('Specify the existing path you wish to alias. For example: /node/28, /media/1, /taxonomy/term/1.');
    }

    if ($field_name === 'alias') {
      $element['value']['#description'] = t('Specify an alternative path by which this data can be accessed. For example, type "/about" when writing an about page.');
    }
  }
}