I'm looking for any examples of creating a SHS enabled widget programatically via the Form API. I've looked in the documentation, the module code, and done some searches, but to no avail.
I have a two level vocabulary of options that I need to display on a Commerce checkout pane which visitors should be able to select from. Whether these options appear depends on the product in their cart, and this logic is handled in a custom module. Because it's custom code there isn't an entity to add a straight term reference field to.
I was hoping that maybe there is some way to create the widget via the Form API, something like (broken code):
// Create custom checkout pane for visitors without a referal code
$form['custom_referrer_field'] = array(
'#type' => 'shs_default', // also tried 'shs'
'#title' => t('Please select an option'),
'#size' => 1,
'#shs_settings' => array(
'node_count' => 0,
'create_new_terms' => 0,
'create_new_levels' => 0,
'force_deepest' => 1,
'required' => TRUE,
),
'#shs_vocabulary' => $vocabulary, // Vocabulary ID
'#required' => 1,
'#attributes' => array(
'class' => array('shs-enabled'),
),
);
But I can't seem to find the right incantation of options to get the element to render.
Is this possible, are there any examples I can look at?
Comments
Comment #1
stBorchertNo, this isn't possible with Simple hierarchical select since it transforms term-reference widgets and doesn't create a widget on its own.
You'll need to create the elements using #ajax. See module Examples for in-depth details on how to do this.
Comment #1.0
garrettc CreditAttribution: garrettc commentedChanged
to <?php
Comment #2
geophysicist CreditAttribution: geophysicist commentedIt is possible.
Comment #3
geophysicist CreditAttribution: geophysicist commentedMain idea:
Comment #4
dmudie CreditAttribution: dmudie commentedHi - did you ever get this to work? I'm not having any luck.
Comment #5
Tаo CreditAttribution: Tаo as a volunteer commentedComment #6
gmangones CreditAttribution: gmangones commentedHow do that in drupal 8?
Thanks.
Comment #7
pratip.ghosh CreditAttribution: pratip.ghosh commentedWant to know too, how to do this in D8
@gmangones, did you find the way?
Comment #8
lykyd CreditAttribution: lykyd commentedFor D8 integration it is tricky.
I had great pains to get the selected value sent in the submit callback, as options were set in JS and would disappear from the values of the form_state.
The solution was to manually send in the field declaration the options (that are also generated by SHS in JS) and split the declaration of the "container" and the "widget" form fields.
Comment #9
khan_ahtesham CreditAttribution: khan_ahtesham commented@pratip.ghosh did you find a way to implement this in D8?
Comment #10
khan_ahtesham CreditAttribution: khan_ahtesham commentedAnyone looking for the solution in D8 can try this :
Created two form fields and populated first field with parent items and based on that value wrote a callback to load all children items which will be populated to the second drop down using jquery.
Comment #11
Nikit CreditAttribution: Nikit commentedIn comment #8 just change
to
and it will work, tested for shs-8.x-1.x-dev version.
Comment #12
DiegoUzc CreditAttribution: DiegoUzc as a volunteer commentedComment #13
DiegoUzc CreditAttribution: DiegoUzc as a volunteer commentedDrupal 8 - Create SHS enabled widget programatically through Form API
For Drupal 8 with unlimited multi-value SHS field in Custom Form
buildForm() method in example of custom form
submitForm() method in example of custom form
Tested and works in version 8.x-1.0-alpha4
Comment #14
gmangones CreditAttribution: gmangones commentedHi, comment #8 and #11 combined work for my.
Comment #15
aadeshvermaster@gmail.com CreditAttribution: aadeshvermaster@gmail.com as a volunteer and commentedHi,
comment #8 and #11 combined work for me.
And i also checked #13, its also working if we need multiple values, only issue when we add more all values hide then show again.
Thanks