You are here

function menu_link_field_validate in Menu Link (Field) 7

Implements hook_field_validate().

Possible error codes:

  • 'menu_link_duplicate': A menu link can only be referenced once per entity per field.
  • 'menu_link_invalid_parent': Selected parent menu link does not exist or may not be selected.
  • 'menu_link_no_entity_uri': No link path is provided while entity being edited has no URI of its own.

File

./menu_link.field.inc, line 98
Defines a menu link field type.

Code

function menu_link_field_validate($entity_type, $entity, $field, $instance, $langcode, &$items, &$errors) {
  if (!empty($entity)) {
    list($id, , ) = entity_extract_ids($entity_type, $entity);
  }

  // Build an array of existing menu link IDs so they can be loaded with
  // menu_link_load_multiple();
  $mlids = array();
  foreach ($items as $delta => &$item) {
    if (menu_link_field_is_empty($item, $field)) {
      continue;
    }
    if (!empty($item['mlid']) && (int) $item['mlid'] > 0) {
      $mlids[] = (int) $item['mlid'];
    }
    if (!empty($item['plid']) && (int) $item['plid'] > 0) {
      $mlids[] = (int) $item['plid'];
    }
  }
  $menu_links = !empty($mlids) ? menu_link_load_multiple($mlids) : array();
  $mlids = array();
  foreach ($items as $delta => &$item) {
    if (menu_link_field_is_empty($item, $field)) {
      continue;
    }
    if (empty($item['menu_name']) || !in_array($item['menu_name'], $instance['settings']['menu_options'])) {
      $errors[$field['field_name']][$langcode][$delta][] = array(
        'error' => 'menu_link_invalid_parent',
        'message' => t('%name: Invalid menu name.', array(
          '%name' => t($instance['label']),
        )),
      );
    }
    if (!empty($item['plid'])) {
      if (!isset($menu_links[$item['plid']]) || !in_array($menu_links[$item['plid']]['menu_name'], $instance['settings']['menu_options'])) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'menu_link_invalid_parent',
          'message' => t('%name: Invalid parent menu link.', array(
            '%name' => t($instance['label']),
          )),
        );
      }
    }
    if (!empty($item['mlid'])) {
      if (isset($mlids[$item['mlid']])) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'menu_link_duplicate',
          'message' => t('%name: A menu link can only be referenced once per entity per field.', array(
            '%name' => t($instance['label']),
          )),
        );
      }
      else {
        $mlids[$item['mlid']] = $item['mlid'];
      }
    }
    if (!empty($field['settings']['link_path_field']) && empty($item['link_path']) && !empty($id)) {
      $uri = entity_uri($entity_type, $entity);
      if ($uri === NULL) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'menu_link_no_entity_uri',
          'message' => t('%name: No link path is provided while entity being edited has no URI of its own.', array(
            '%name' => t($instance['label']),
          )),
        );
      }
    }
  }
}