diff --git a/uw_cfg_common.module b/uw_cfg_common.module index 60585146cebc5190297588111d4eba8a7712c472..c911d562c43c836eef4d96184191b4a57f37c789 100644 --- a/uw_cfg_common.module +++ b/uw_cfg_common.module @@ -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. - 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. if (isset($form['field_uw_meta_tags'])) { @@ -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(). */