You are here

function _nodehierarchy_widgets_autocomplete_parent_validate in Node Hierarchy 7.4

Same name and namespace in other branches
  1. 6.3 nodehierarchy_widgets/nodehierarchy_widgets.module \_nodehierarchy_widgets_autocomplete_parent_validate()
  2. 6.2 nodehierarchy_widgets/nodehierarchy_widgets.module \_nodehierarchy_widgets_autocomplete_parent_validate()
  3. 7.2 nodehierarchy_widgets/nodehierarchy_widgets.module \_nodehierarchy_widgets_autocomplete_parent_validate()

Validate a node hierarchy autocomplete in the format 'Tile [nid:xx]' or '[nid:xx]' or 'Title'.

1 string reference to '_nodehierarchy_widgets_autocomplete_parent_validate'
nodehierarchy_widgets_nodehierarchy_get_parent_selector in nodehierarchy_widgets/nodehierarchy_widgets.module
Implements hook_nodehierarchy_get_parent_selector().

File

nodehierarchy_widgets/nodehierarchy_widgets.module, line 168
Alternative parent selector widgets for Node Hierarchy.

Code

function _nodehierarchy_widgets_autocomplete_parent_validate($element, &$form_state) {
  $child_type = $element['#child_type'];
  $exclude = $element['#exclude'];
  $value = $element['#value'];
  $nid = NULL;
  if (!empty($value)) {
    preg_match('/^(?:\\s*|(.*) )?\\[\\s*nid\\s*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
    if (!empty($matches)) {

      // Explicit [nid:n].
      list(, $title, $nid) = $matches;
      if (isset($form_state['values']['nid'])) {
        if ($err = _nodehierarchy_widgets_is_invalid_parent($nid, $form_state['values']['nid'], $form_state['values']['type'])) {
          form_error($element, $err);
        }
      }
    }
    else {

      // No explicit nid.
      $options = _nodehierarchy_widgets_parent_autocomplete_options($form_state['values']['type'], $form_state['values']['nid'], trim($value));
      if (empty($options)) {
        form_error($element, t('There are no available parents called \'%name\'', array(
          '%name' => $value,
        )));
      }
      else {
        $key = key($options);
        if ($err = _nodehierarchy_widgets_is_invalid_parent($options[$key]['nid'], $form_state['values']['nid'], $form_state['values']['type'])) {
          form_error($element, $err);
        }
        else {
          $nid = $key;
        }
      }
    }
  }
  form_set_value($element, $nid, $form_state);
}