function path_field_widget_form_alter in Drupal 8
Implements hook_field_widget_form_alter().
File
- core/
modules/ path/ path.module, line 133 - Enables users to rename URLs.
Code
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 <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, /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.');
}
}
}