You are here

function social_core_path_validate in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/social_core.module \social_core_path_validate()
  2. 8 modules/social_features/social_core/social_core.module \social_core_path_validate()
  3. 8.2 modules/social_features/social_core/social_core.module \social_core_path_validate()
  4. 8.3 modules/social_features/social_core/social_core.module \social_core_path_validate()
  5. 8.4 modules/social_features/social_core/social_core.module \social_core_path_validate()
  6. 8.5 modules/social_features/social_core/social_core.module \social_core_path_validate()
  7. 8.6 modules/social_features/social_core/social_core.module \social_core_path_validate()
  8. 8.7 modules/social_features/social_core/social_core.module \social_core_path_validate()
  9. 10.3.x modules/social_features/social_core/social_core.module \social_core_path_validate()
  10. 10.0.x modules/social_features/social_core/social_core.module \social_core_path_validate()
  11. 10.1.x modules/social_features/social_core/social_core.module \social_core_path_validate()
  12. 10.2.x modules/social_features/social_core/social_core.module \social_core_path_validate()

Form element validation handler for URL alias form element.

Ensures that a path alias entered by a user is always a relative internal URL that starts with a leading slash. If the user did not enter a leading slash then we add it for them.

Parameters

array $element: The form element.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

1 string reference to 'social_core_path_validate'
social_core_field_widget_form_alter in modules/social_features/social_core/social_core.module
Implements hook_field_widget_form_alter().

File

modules/social_features/social_core/social_core.module, line 561
The Social core module.

Code

function social_core_path_validate(array &$element, FormStateInterface $form_state) {
  $alias = trim($element['alias']['#value'], " \\/");
  $parsed_url = parse_url($alias);
  if (isset($parsed_url['host']) || isset($parsed_url['scheme']) || !isset($parsed_url['path'])) {
    $form_state
      ->setError($element, t('The URL alias must be a relative URL.'));
  }
  elseif (trim($element['alias']['#value']) !== "/") {

    // $alias is already trimmed of leading/trailing slashes.
    // We use rtrim here to ensure we don't set the value to an empty slash
    // during form submission.
    // The following line only changes the value of the rendered form element.
    // If we have a non-empty alias PathWidget::validateFormElement will
    // update the actual stored value to include the leading slash.
    $element['alias']['#value'] = rtrim('/' . $alias, '/');
    PathWidget::validateFormElement($element, $form_state);
  }
}