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

ISTWMCS-5866: only do alias stuff if an alias exists (tests were failing)

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