Skip to content
Snippets Groups Projects
Commit 5a64dc07 authored by Kevin Paxman's avatar Kevin Paxman
Browse files

Merge branch...

Merge branch 'feature/ISTWCMS-5866-a5kulkar-prevent-URL-aliases-being-created-for-reserved-paths' into '1.1.x'

ISTWCMS-5866 Prevent URL aliases being created for reserved paths

See merge request !286
parents 7f6cdc22 71f6ef22
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,10 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state,
}
// 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_ct_.*_form$/', $form_id)) {
// Add custom validation for alias.
$form['#validate'][] = '_uw_cfg_common_alias_validate';
// Ensure that the node has metatag information.
if (isset($form['field_uw_meta_tags'])) {
......@@ -931,6 +934,62 @@ 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 {
// Get the values from the form state.
$values = $form_state->getValues();
if (isset($values['path'][0]['alias'])) {
// List of urls which should not be an alias.
$urls = [
'blog',
'events',
'news',
'projects',
'profile',
'profiles',
'contacts',
'service',
'opportunities',
'user',
'users',
];
// Get the alias from the form state values.
$alias = $values['path'][0]['alias'];
$orig_alias = $alias;
// Trim any surrounding slashes from the alias to
// ensure that we are getting exact matches for the
// predefined alias from above. Some users will add
// slashes before and after the alias, so just
// easier to check without slashes.
$alias = trim($alias, '/');
// Check if the alias exists if yes, sets error.
// We are checking three cases, the first is if
// the alias is in the predefined list. The second
// is if the alias is just /, or just a series of slashes,
// we have to check here because we removed all the
// slashes with the alias variable. The last is to check
// if the alias has any form of strictly admin, so /admin/,
// admin, admin/, and admin/something are not allowed,
// but something like admin-meeting would be.
if (
in_array($alias, $urls) ||
preg_match('/^\/+$/', $orig_alias) ||
preg_match('/^admin(?:\/.*)?$/', $alias)
) {
// Set an error message if alias exists.
$form_state->setError($form['path']['widget'][0]['alias'], t('The alias "@url" is a reserved path that cannot be used.', ['@url' => $orig_alias]));
}
}
}
/**
* 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