You are here

function autoassignrole_page_form_validate in Auto Assign Role 7.2

Same name and namespace in other branches
  1. 7 autoassignrole.admin.inc \autoassignrole_page_form_validate()

Implements hook_form_validate().

Check path to see if page already exists.

File

./autoassignrole.admin.inc, line 303
Administrative functionality for auto assign role.

Code

function autoassignrole_page_form_validate($form_id, &$form_state) {
  $path = $form_state['values']['path'];

  // @todo need to check if the path already exists.
  if ($path) {
    if (preg_match('/[^A-Za-z0-9\\/_-]+/', $path) || strpos(trim($path), '/') === 0) {
      form_set_error('path', '\'' . check_plain($path) . '\' ' . t('is not a valid path'));
    }
    else {
      $query = db_select('menu_links', 'm')
        ->fields('m', array(
        'mlid',
      ))
        ->condition('link_path', $path, 'like');
      $count = $query
        ->countQuery()
        ->execute()
        ->fetchField();
      if (drupal_lookup_path('source', $path) || $count > 0) {

        // The menu item exists so need to check if the path has something other
        // than autoassign_role_path registered otherwise throw an error.
        $menu_item = menu_get_item($path);
        if (!isset($menu_item['page_callback']) && $menu_item['page_callback'] != 'autoassignrole_path') {
          form_set_error('path', '\'' . check_plain($path) . '\' ' . t('is already in use as a path'));
        }
      }
    }
  }
}