' . t('About') . ''; $output .= '
' . 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 Pathauto. For more information, see the online documentation for the Path module.', [':path' => 'https://www.drupal.org/documentation/modules/path', ':pathauto' => 'https://www.drupal.org/project/pathauto']) . '
'; $output .= '' . 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.") . '
'; case 'entity.path_alias.add_form': return '' . t('Enter the path you wish to create the alias for, followed by the name of the new alias.') . '
'; } } /** * 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 $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */ 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_form_alter(). */ function path_field_widget_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 - Not specified -.'); $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, /forum/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.'); } } }