diff --git a/uw_cfg_common.module b/uw_cfg_common.module index 8575a207841b829b53e6bd91530e4c87184b0791..0bae4d338487a3d1f439815468ee250ce5be8d6d 100644 --- a/uw_cfg_common.module +++ b/uw_cfg_common.module @@ -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. - if (preg_match('/node_uw_ct_.*_form/', $form_id)) { + if (preg_match('/^node_uw_ct_.*_form$/', $form_id)) { // Add custom validation for alias. $form['#validate'][] = '_uw_cfg_common_alias_validate'; @@ -938,53 +938,54 @@ 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 { + 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. - $urls = [ - 'blog', - 'events', - 'news', - 'projects', - 'profile', - 'profiles', - 'contacts', - 'service', - 'opportunities', - 'user', - 'users', - ]; - - // Get the values from the form state. - $values = $form_state->getValues(); - - // 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) - ) { + // Get the values from the form state. + $values = $form_state->getValues(); + + // 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])); + // 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])); + } } }