You are here

function advanced_link_validator in Advanced Link 6

Provides field validation by additional filtering options.

1 string reference to 'advanced_link_validator'
advanced_link_elements in ./advanced_link.module
Implements hook_elements().

File

./advanced_link.module, line 176
Defines simple advanced_link widget.

Code

function advanced_link_validator($element, &$form_state) {
  $urls_allowed = isset($element['#field']['widget']['urls_allowed']) ? $element['#field']['widget']['urls_allowed'] : ADVANCED_LINK_BOTH;
  $urls_filter = isset($element['#field']['widget']['urls_filter']) ? trim($element['#field']['widget']['urls_filter']) : NULL;
  $values = $element['#value'];
  $url = trim($values['url']);
  if ($url) {
    if ($urls_allowed != ADVANCED_LINK_BOTH) {

      // Getting link type - internal or external.
      if ($urls_allowed != link_validate_url($url)) {
        form_set_error($element['#field_name'], t('URLs need to be @url_allowed', array(
          '@url_allowed' => drupal_strtoupper($urls_allowed),
        )));
      }
    }

    // Checking link URL by URL filter patterns.
    if ($urls_filter) {
      $match = drupal_match_path($url, $urls_filter);
      if (!$match) {
        form_set_error($element['#field_name'], t("You can't use such URLs."));
      }
    }
  }
}