Default values ​​for content taxonomy fields in Drupal using a hierarchical widget

I am trying to set the default value for the Content Taxonomy field in hook_form_alter, but cannot bind the desired format. I tried this and many options:

foreach (element_children($form) as $child) { // Set $default_value. if ($form[$child]['tids']) { // This, for Content Taxonomy fields, isn't working: $form[$child]['tids']['#default_value'] = array('value' => $default_value); dsm($form[$child]['tids']['#default_value']); } else { // This, for other fields, is working: $form[$child][0]['#default_value']['value'] = $default_value; } } 

Can someone tell me what I am missing?

Edit: In response to Henrik Opel (thanks for participating), here is a printout from the corresponding form field with my changes to the default fields commented out, showing the "#default_value" field I'm trying to influence.

It also shows that the widget parameter I am using is Hierarchical choice (could this be a factor?). In the dsm () code above, changes to the default value are recognized, but they are not processed later.

 field_name_of_content_taxonomy_field (Array, 3 elements) #tree (Boolean) TRUE #weight (String, 1 characters ) 5 tids (Array, 7 elements) #title (String, 10 characters ) Vocabulary_name #type (String, 19 characters ) hierarchical_select #weight (String, 1 characters ) 5 #config (Array, 15 elements) // 15 elements here #required (String, 1 characters ) 0 #description (String, 0 characters ) #default_value (Array, 0 elements) 
+4
source share
2 answers

Turns out the answer was in the documentation: http://drupal.org/node/319190

I renamed the question so that it is clear where the problem is. This narrows the question to a rather specific use case, but both taxonomy of content and hierarchical choice are useful and recommended modules for working with large taxonomies.

+3
source

(NOTE: Assume Drupal 6 is here)

In the local example, the Content Taxonomy fields do not have the tids property, but I only use the optionswidgets_buttons type fields, so this may be different if you use other types of widgets. Could you post a dump of the structure of the field Taxonomy of content from the form you are trying to manipulate?

In another note, the “Content Taxonomy” fields have some special interaction logic regarding their “conversion” back to the standard taxonomy array format later in the / node form, which may affect you. So, your problem is that your changes to the default values ​​are not displayed at all on the initial form display or are they displayed but not processed later?


Edit (after updating the question): Sorry for the late reply. I'm not sure, but looking at your field definition, the type displays as hierarchical_select , which alludes to a "standalone" hierarchical selection widget. When used in combination with content taxonomy, the code from the "hs_content_taxonomy" submodule will make me expect the type content_taxonomy_hs . These are just guesses, and I don’t have time to correctly check this locally, but you can check if you are using the correct module combination / configuration.

One more thing - checking the weight settings of your module with respect to the hierarchical module (s) of choice is also just a wild assumption, but maybe your changes should occur before / after choosing a hierarchical selection, so you might want to test with the installed weights on your module.

+1
source

Source: https://habr.com/ru/post/1309456/


All Articles