You are here

function shs_field_widget_validate in Simple hierarchical select 7

Validation handler for widgets of type "taxonomy_shs".

1 string reference to 'shs_field_widget_validate'
shs_field_widget_form in ./shs.module
Implements hook_field_widget_form().

File

./shs.module, line 627
Provides an additional widget for term fields to create hierarchical selects.

Code

function shs_field_widget_validate($element, &$form_state, $form) {
  $field_name = $element['#field_name'];
  $instance = field_widget_instance($element, $form_state);
  if (empty($instance['widget'])) {
    return;
  }

  // Load default settings.
  $settings = empty($instance['widget']['settings']['shs']) ? array() : $instance['widget']['settings']['shs'];
  if (!empty($element['#shs_settings'])) {

    // Use settings directly applied to widget (possibly overridden in
    // hook_field_widget_form_alter() or
    // hook_field_widget_WIDGET_TYPE_form_alter()).
    $settings = $element['#shs_settings'];
  }

  // Do we want to force the user to select terms from the deepest level?
  $force_deepest_level = empty($settings['force_deepest']) ? FALSE : $settings['force_deepest'];
  $field = field_widget_field($element, $form_state);
  $value = empty($element['#value']) ? '_none' : $element['#value'];
  if ($value == '_none') {
    unset($element['#value']);
    form_set_value($element, NULL, $form_state);
  }
  if ($element['#required'] && $value == '_none') {
    $element_name = empty($element['#title']) ? $instance['label'] : $element['#title'];
    form_error($element, t('!name field is required.', array(
      '!name' => $element_name,
    )));
    return;
  }
  if ($force_deepest_level && $value) {

    // Get vocabulary names from allowed values.
    if ($field['type'] == 'entityreference') {
      $vocabulary_names = $field['settings']['handler_settings']['target_bundles'];
    }
    else {
      $allowed_values = reset($field['settings']['allowed_values']);
      $vocabulary_names = empty($allowed_values['vocabulary']) ? FALSE : $allowed_values['vocabulary'];
    }
    if (empty($vocabulary_names)) {

      // No vocabulary selected yet.
      form_error($element, t('No vocabulary is configured as source for field %field_name.', array(
        '%field_name' => $instance['label'],
        '%field_machine_name' => $field_name,
      )));
    }
    if (!is_array($vocabulary_names)) {
      $vocabulary_names = array(
        $vocabulary_names,
      );
    }
    foreach ($vocabulary_names as $vocabulary_name) {
      if (($vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name)) === FALSE) {

        // Vocabulary not found. Stop here.
        $t_args = array(
          '%machine_name' => $vocabulary_name,
          '%field_name' => $instance['label'],
          '%field_machine_name' => $field_name,
        );
        form_error($element, t('Vocabulary %machine_name is configured as source for field %field_name but could not be found.', $t_args));
        return;
      }

      // Does the selected term has any children?
      $children = shs_term_get_children($vocabulary->vid, $value);
      if (count($children)) {
        form_error($element, t('You need to select a term from the deepest level in field %field_name.', array(
          '%field_name' => $instance['label'],
          '%field_machine_name' => $field_name,
        )));
        return;
      }
    }
  }
}