path.module 9.38 KB
Newer Older
Steven Wittens's avatar
Steven Wittens committed
1 2 3 4 5 6 7
<?php

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

8
use Drupal\Core\Language\Language;
9
use Drupal\Core\Entity\EntityInterface;
10
use Drupal\Core\Entity\ContentEntityInterface;
11
use Drupal\Core\Field\FieldDefinition;
12

Steven Wittens's avatar
Steven Wittens committed
13
/**
14
 * Implements hook_help().
Steven Wittens's avatar
Steven Wittens committed
15
 */
16 17
function path_help($path, $arg) {
  switch ($path) {
Steven Wittens's avatar
Steven Wittens committed
18
    case 'admin/help#path':
19 20
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
21
      $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 online handbook entry for the <a href="@path">Path module</a>.', array('@path' => 'http://drupal.org/documentation/modules/path', '@pathauto' => 'http://drupal.org/project/pathauto')) . '</p>';
22 23 24
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Creating aliases') . '</dt>';
25
      $output .= '<dd>' . t('Users with sufficient <a href="@permissions">permissions</a> can create aliases under the <em>URL path settings</em> section when they create or edit content. Some examples of aliases are: ', array('@permissions' => url('admin/people/permissions', array('fragment' => 'module-path'))));
26 27 28 29
      $output .= '<ul><li>' . t('<em>member/jane-smith</em> aliased to internal path <em>user/123</em>') . '</li>';
      $output .= '<li>' . t('<em>about-us/team</em> aliased to internal path <em>node/456</em>') . '</li>';
      $output .= '</ul></dd>';
      $output .= '<dt>' . t('Managing aliases') . '</dt>';
30
      $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.', array('@aliases' => url('admin/config/search/path'))) . '</dd>';
31
      $output .= '</dl>';
Steven Wittens's avatar
Steven Wittens committed
32
      return $output;
33

34
    case 'admin/config/search/path':
35
      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>';
36

37
    case 'admin/config/search/path/add':
38
      return '<p>' . t('Enter the path you wish to create the alias for, followed by the name of the new alias.') . '</p>';
Steven Wittens's avatar
Steven Wittens committed
39 40 41
  }
}

42
/**
43
 * Implements hook_permission().
44 45 46 47 48 49 50
 */
function path_permission() {
  return array(
    'administer url aliases' => array(
      'title' => t('Administer URL aliases'),
    ),
    'create url aliases' => array(
51
      'title' => t('Create and edit URL aliases'),
52 53 54 55
    ),
  );
}

Steven Wittens's avatar
Steven Wittens committed
56
/**
57
 * Implements hook_menu().
Steven Wittens's avatar
Steven Wittens committed
58
 */
59
function path_menu() {
60
  $items['admin/config/search/path'] = array(
61 62
    'title' => 'URL aliases',
    'description' => "Change your site's URL paths by aliasing them.",
63
    'route_name' => 'path.admin_overview',
64
    'weight' => -5,
65
  );
66
  $items['admin/config/search/path/edit/%path'] = array(
67
    'title' => 'Edit alias',
68
    'route_name' => 'path.admin_edit',
69
  );
70
  $items['admin/config/search/path/delete/%path'] = array(
71
    'title' => 'Delete alias',
72
    'route_name' => 'path.delete',
73
  );
Steven Wittens's avatar
Steven Wittens committed
74 75 76 77

  return $items;
}

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/**
 * Implements hook_menu_link_defaults().
 */
function path_menu_link_defaults() {
  $links['path.admin.overview'] = array(
    'link_title' => 'URL aliases',
    'description' => "Change your site's URL paths by aliasing them.",
    'route_name' => 'path.admin_overview',
    'parent' => 'system.admin.config.search',
    'weight' => -5,
  );

  return $links;
}

Steven Wittens's avatar
Steven Wittens committed
93
/**
94 95 96
 * Implements hook_form_BASE_FORM_ID_alter() for node_form().
 *
 * @see path_form_element_validate()
Steven Wittens's avatar
Steven Wittens committed
97
 */
98
function path_form_node_form_alter(&$form, $form_state) {
99
  $node = $form_state['controller']->getEntity();
100
  $path = array();
101 102
  if (!$node->isNew()) {
    $conditions = array('source' => 'node/' . $node->id());
103 104
    if ($node->language()->id != Language::LANGCODE_NOT_SPECIFIED) {
      $conditions['langcode'] = $node->language()->id;
105
    }
106
    $path = \Drupal::service('path.crud')->load($conditions);
107 108
    if ($path === FALSE) {
      $path = array();
109 110
    }
  }
111 112
  $path += array(
    'pid' => NULL,
113
    'source' => $node->id() ? 'node/' . $node->id() : NULL,
114
    'alias' => '',
115
    'langcode' => $node->language()->id,
116 117
  );

118
  $account = \Drupal::currentUser();
119
  $form['path'] = array(
120
    '#type' => 'details',
121
    '#title' => t('URL path settings'),
122
    '#collapsed' => empty($path['alias']),
123
    '#group' => 'advanced',
124 125 126
    '#attributes' => array(
      'class' => array('path-form'),
    ),
127
    '#attached' => array(
128
      'library' => array(array('path', 'drupal.path')),
129
    ),
130
    '#access' => $account->hasPermission('create url aliases') || $account->hasPermission('administer url aliases'),
131 132 133 134 135 136 137 138 139
    '#weight' => 30,
    '#tree' => TRUE,
    '#element_validate' => array('path_form_element_validate'),
  );
  $form['path']['alias'] = array(
    '#type' => 'textfield',
    '#title' => t('URL alias'),
    '#default_value' => $path['alias'],
    '#maxlength' => 255,
140
    '#description' => t('The alternative URL for this content. Use a relative path without a trailing slash. For example, enter "about" for the about page.'),
141 142 143
  );
  $form['path']['pid'] = array('#type' => 'value', '#value' => $path['pid']);
  $form['path']['source'] = array('#type' => 'value', '#value' => $path['source']);
144
  $form['path']['langcode'] = array('#type' => 'value', '#value' => $path['langcode']);
145
}
Steven Wittens's avatar
Steven Wittens committed
146

147
/**
148
 * Form element validation handler for URL alias form element.
149 150
 *
 * @see path_form_node_form_alter()
151
 */
152 153 154 155 156
function path_form_element_validate($element, &$form_state, $complete_form) {
  if (!empty($form_state['values']['path']['alias'])) {
    // Trim the submitted value.
    $alias = trim($form_state['values']['path']['alias']);
    form_set_value($element['alias'], $alias, $form_state);
157 158 159 160
    // Node language needs special care. Since the language of the URL alias
    // depends on the node language, and the node language can be switched
    // right within the same form, we need to conditionally overload the
    // originally assigned URL alias language.
161 162
    // @todo Remove this after converting Path module to a field, and, after
    //   stopping Locale module from abusing the content language system.
163 164
    if (isset($form_state['values']['langcode'])) {
      form_set_value($element['langcode'], $form_state['values']['langcode'], $form_state);
165
    }
166 167 168 169 170 171

    $path = $form_state['values']['path'];

    // Ensure that the submitted alias does not exist yet.
    $query = db_select('url_alias')
      ->condition('alias', $path['alias'])
172
      ->condition('langcode', $path['langcode']);
173 174 175 176 177 178
    if (!empty($path['source'])) {
      $query->condition('source', $path['source'], '<>');
    }
    $query->addExpression('1');
    $query->range(0, 1);
    if ($query->execute()->fetchField()) {
179
      form_error($element, $form_state, t('The alias is already in use.'));
180
    }
181 182
  }
}
Steven Wittens's avatar
Steven Wittens committed
183

184
/**
185
 * Implements hook_form_FORM_ID_alter() for taxonomy_term_form().
186
 */
187
function path_form_taxonomy_term_form_alter(&$form, $form_state) {
188
  $account = \Drupal::currentUser();
189 190
  // Make sure this does not show up on the delete confirmation form.
  if (empty($form_state['confirm_delete'])) {
191
    $term = $form_state['controller']->getEntity();
192
    $path = ($term->id() ? \Drupal::service('path.crud')->load(array('source' => 'taxonomy/term/' . $term->id())) : array());
193 194
    if ($path === FALSE) {
      $path = array();
195
    }
196 197
    $path += array(
      'pid' => NULL,
198
      'source' => $term->id() ? 'taxonomy/term/' . $term->id() : NULL,
199
      'alias' => '',
200
      'langcode' => Language::LANGCODE_NOT_SPECIFIED,
201
    );
202
    $form['path'] = array(
203
      '#access' => $account->hasPermission('create url aliases') || $account->hasPermission('administer url aliases'),
204 205 206
      '#tree' => TRUE,
      '#element_validate' => array('path_form_element_validate'),
    );
207
    $form['path']['alias'] = array(
208 209
      '#type' => 'textfield',
      '#title' => t('URL alias'),
210
      '#default_value' => $path['alias'],
211 212 213 214
      '#maxlength' => 255,
      '#weight' => 0,
      '#description' => t("Optionally specify an alternative URL by which this term can be accessed. Use a relative path and don't add a trailing slash or the URL alias won't work."),
    );
215 216
    $form['path']['pid'] = array('#type' => 'value', '#value' => $path['pid']);
    $form['path']['source'] = array('#type' => 'value', '#value' => $path['source']);
217
    $form['path']['langcode'] = array('#type' => 'value', '#value' => $path['langcode']);
218 219 220
  }
}

221 222 223 224
/**
 * Implements hook_entity_field_info().
 */
function path_entity_field_info($entity_type) {
225
  if ($entity_type === 'taxonomy_term' || $entity_type === 'node') {
226 227 228 229
    $info['definitions']['path'] = FieldDefinition::create('path')
      ->setLabel(t('The path alias'))
      ->setComputed(TRUE);

230 231 232 233
    return $info;
  }
}

234 235 236 237 238 239
/**
 * Implements hook_library_info().
 */
function path_library_info() {
  $libraries['drupal.path'] = array(
    'title' => 'Path',
240
    'version' => \Drupal::VERSION,
241 242 243 244 245 246 247 248 249 250 251 252
    'js' => array(
      drupal_get_path('module', 'path') . '/path.js' => array(),
    ),
    'dependencies' => array(
      array('system', 'jquery'),
      array('system', 'drupal'),
      array('system', 'drupal.form'),
    ),
  );

  return $libraries;
}