I'm trying to add labels to each hierarchical depth and after updating to the dev version, I am trying to use the hook supplied in shs.api.php. The nodes field which has the SHS widget applied is called field_make

Here is my custom custom.info file:

name = Simple Hierarchal Select Custom labels
description = "Custom labels for SHS fields"
core = 7.x

files[] = custom.module

And here is my custom.module file:

<?php 

function custom_shs_field_make_js_settings_alter(&$settings_js, $field_name, $vocabulary_identifier) {
  foreach ($settings_js['shs'] as $field => &$container) {
    foreach ($container as $identifier => &$settings) {
      // Define labels for each level.
      $settings['labels'] = array(
        FALSE, // No label for first level.
        t('Country'),
        t('City'),
      );
      // Small speed-up for anmiations (defaults to 400ms).
      $settings['display']['animationSpeed'] = 100;
    }
  }
}

After clearing the cache and checking the HTML to see if the label is hidden, it still isn't there. What am I doing wrong?

CommentFileSizeAuthor
#8 2015-11-27 07-57-00.png33.07 KBstBorchert
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

techn0guy created an issue. See original summary.

stBorchert’s picture

Priority: Major » Normal

Hm, I've tested the code right now in a custom module and it works as expected.
Could you debug into your implementation or add a print_r($settings_js['shs']) at the first line to see if the function is called?

techn0guy’s picture

It says

Notice: Undefined variable: settings_js in include_once() (line 3 of /home/blackleaf/public_html/tekmotive/sites/all/modules/custom/custom.module).

techn0guy’s picture

Actually I may have put it in the wrong location, I received that notice when I put print_r($settings_js['shs']); above the function. I received no notice when I put it right after the opening { of the function.

techn0guy’s picture

I just put drupal_set_message(t('An error occurred and processing did not complete.'), 'error');as the first line of the function and no message displayed. Does this mean that my field name is incorrect?

stBorchert’s picture

Please try using hook_shs_js_settings_alter() and print out the $field_name variable.
I guess your field is named field_field_make ;)

techn0guy’s picture

I just realized that it has been working this whole time, however it is only working on the node creation form. The labels are showing in the HTML. However, I was trying to get the labels to show in the exposed filter of the view, the labels are still not showing there. That was my intention, not the node creation form. How do I get them to show in the view?

stBorchert’s picture

FileSize
33.07 KB

I had to look also how this could be done :)
It's very simple: the name of the field used in the views filter is different from the "real" name. In my case (field_location) the filter field has the name "field_location_tid".

Just print out the field name in hook_shs_js_settings_alter() and you will get the correct name.
 labels per level in views filter

techn0guy’s picture

Awesome! How would I print out the field name in hook_shs_js_settings_alter()?

techn0guy’s picture

Never mind I just figured it out, for anyone who is wondering it is

function custom_shs_js_settings_alter(&$settings_js, $field_name, $vocabulary_identifier) {
  echo($field_name);
}
stBorchert’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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