1 redirect.admin.inc redirect_element_validate_source($element, &$form_state)

Element validate handler; validate the source of an URL redirect.

See also

redirect_edit_form()

File

core/modules/redirect/redirect.admin.inc, line 479
Administrative page callbacks for the Redirect module.

Code

function redirect_element_validate_source($element, &$form_state) {
  // Check that the source contains no URL fragment.
  if (strpos($element['#value'], '#') !== FALSE) {
    form_error($element, t('The source path cannot contain an URL fragment anchor.'));
  }

  $parsed_value = _redirect_extract_url_options($element, $form_state);

  // Disallow redirects from the front page.
  if ($parsed_value['path'] === '<front>') {
    form_error($element, t('The source path cannot be the front page.'));
  }

  // Cannot create redirects for valid paths.
  if (empty($form_state['values']['override'])) {
    // Check if this is an alias.
    $alias = path_load(array('alias' => $parsed_value['path']));
    if ($alias) {
      $form_state['storage']['override_messages']['existing-alias'] = t('The source path !path is an existing URL alias. You may prefer to <a href="@url-alias">edit this URL alias</a> rather than using a redirect.', array('!path' => '<em>' . l($parsed_value['path'], $parsed_value['path']) . '</em>', '@url-alias' => url('admin/config/urls/path/edit/' . $alias['pid'])));
      $form_state['rebuild'] = TRUE;
      return $element;
    }
    $path = backdrop_get_normal_path($parsed_value['path'], $form_state['values']['langcode']);
    $menu_item = menu_get_item($path);
    if ($menu_item && $menu_item['page_callback'] != 'redirect_redirect') {
      $form_state['storage']['override_messages']['valid-path'] = t('The source path !path is an existing path. Creating a redirect may make it inaccessible. You may prefer to <a href="@url-alias">create a URL alias</a> for this path rather than a redirect.', array('!path' => '<em>' . l($parsed_value['path'], $parsed_value['path']) . '</em>', '@url-alias' => url('admin/config/urls/path/add')));
      $form_state['rebuild'] = TRUE;
      return $element;
    }
  }

  return $element;
}