SHS does not work in embedded field collections, because it looks for field value in hard coded location. Actual field values may be deeper in $form_state.

Comments

henrijs.seso’s picture

Status: Active » Needs review
StatusFileSize
new881 bytes

Here is patch. Instead of relaying on field value to be found always in same place, patch uses #parents array to go directly to correct field correct value. This makes SHS work in field collections and will probably fix other issues along the way.

CSoft’s picture

Also validation function shs_field_widget_validate does not work correctly with field collections.

I suggest the following solution:

<?php
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;
  }
  $field = field_widget_field($element, $form_state);
  $settings = empty($instance['widget']['settings']['shs']) ? array() : $instance['widget']['settings']['shs'];

  // 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'];
  $value = empty($element['#value']) ? 0 : $element['#value'];
  if ($force_deepest_level && $value) {
    // Get vocabulary.
    $allowed_values = reset($field['settings']['allowed_values']);
    if (empty($allowed_values['vocabulary']) || ($vocabulary = taxonomy_vocabulary_machine_name_load($allowed_values['vocabulary'])) === FALSE) {
      // No vocabulary selected yet or vocabulary not found.
      form_error($element, t('Vocabulary %machine_name is configured as source for field %field_name but could not be found.', array('%machine_name' => $allowed_values['vocabulary'], '%field_name' => $field_name)));
    }
    // 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.'));
    }
  }
}
?>
henrijs.seso’s picture

@CSoft What did not work in validation? I ask to re-roll patch with your changes and to see what to test.

CSoft’s picture

StatusFileSize
new1.53 KB

What did not work in validation?

I'm using SHS within the module inline entity form, which is inside the field collections. In this case, the validation function shs_field_widget_validate does not work correctly. My code above solves this problem.

The patch is attached.

henrijs.seso’s picture

StatusFileSize
new2.41 KB

Uploading combined patch from both previous patches.

henrijs.seso’s picture

StatusFileSize
new2.41 KB

Uploading correct combined patch from both previous patches.

stborchert’s picture

Wow, thanks for your patches.
I have committed the patch and some additional changes so Simple hierarchical select works in field collections and in inline entity forms.

stborchert’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

scott.whittaker’s picture

Issue summary: View changes

Just a quick update to note that this was committed to the dev branch over a year ago, but has not made it to the stable branch yet. Dev branch seems to be working for me so far. Any idea when stable will be updated?