Skip to content
Snippets Groups Projects
Commit 89375c97 authored by Anuprita Kulkarni's avatar Anuprita Kulkarni Committed by Kevin Paxman
Browse files

ISTWCMS-5866 Prevent URL aliases being created for reserved paths

parent 7f6cdc22
No related branches found
No related tags found
3 merge requests!286ISTWCMS-5866 Prevent URL aliases being created for reserved paths,!274Draft: ISTWCMS-5551: fixing office hours display,!260Feature/istwcms 5668 a5kulkar rename references to publications
...@@ -851,7 +851,14 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state, ...@@ -851,7 +851,14 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state,
} }
// Ensure that we are on a UW content type node. // Ensure that we are on a UW content type node.
if (preg_match('/node_uw.*add_form/', $form_id) || preg_match('/node_uw.*edit_form/', $form_id)) { if (
preg_match('/node_uw.*add_form/', $form_id) ||
preg_match('/node_uw.*edit_form/', $form_id) ||
preg_match('/node_uw.*_form/', $form_id)
) {
// Add custom validation for alias.
$form['#validate'][] = '_uw_cfg_common_alias_validate';
// Ensure that the node has metatag information. // Ensure that the node has metatag information.
if (isset($form['field_uw_meta_tags'])) { if (isset($form['field_uw_meta_tags'])) {
...@@ -931,6 +938,40 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state, ...@@ -931,6 +938,40 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state,
} }
} }
/**
* Validates submission values for alias on nodes.
*/
function _uw_cfg_common_alias_validate(array &$form, FormStateInterface $form_state): void {
// List of urls which should not be repeat in alias.
$urls = [
'blog',
'events',
'news',
'projects',
'profiles',
'contacts',
'service',
'opportunities',
'user',
'users',
];
$values = $form_state->getValues();
$alias = $values['path'][0]['alias'];
$alias = str_replace('/', '', $alias);
// Check if the alias exists if yes, sets error.
if (
in_array($alias, $urls) ||
$alias == '/' ||
preg_match('/^\/?admin\/.*/', $values['path'][0]['alias'])
) {
// Set an error message if alias exists.
$form_state->setError($form['path']['widget'][0], t('@url cannot be an alias', ['@url' => $alias]));
}
}
/** /**
* Implements hook_field_widget_WIDGET_TYPE_form_alter(). * Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment