Drupal 中带有分层选择小部件的内容分类字段的默认值
我试图在 hook_form_alter 中设置内容分类字段的默认值,但无法确定必要的格式。我已经尝试过这个和许多变体:
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;
}
}
谁能告诉我我错过了什么?
编辑: 为了回应 Henrik Opel(感谢您的参与),这里是表单相关字段的打印输出,其中注释掉了我对默认字段的更改,显示了我试图影响的“#default_value”字段。
它还显示我正在使用的选项小部件是分层选择(这可能是一个因素吗?)。在上面代码的 dsm() 中,可以识别对默认值的更改,但稍后不会对其进行处理。
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)
I'm trying to set the default value for a Content Taxonomy field in a hook_form_alter, but can't pin down the necessary format. I've tried this and many variations:
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 anyone tell me what I'm missing?
Edit:
In response to Henrik Opel (thanks for getting involved), here is the print out of the relevant field of the form with my changes to the default fields commented out, showing the '#default_value' field I'm trying to influence.
It also shows that the option widget I'm using is Hierarchical Select (could this be a factor?). In the dsm() in the code above, the changes to the default value are recognised, but they don't get processed later on.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明答案就在文档中:
http://drupal.org/node/319190
我已重命名该问题以明确其位置问题是。这将问题缩小到一个相当具体的用例,但是内容分类法和层次结构选择都是处理大型分类法的有用且推荐的模块。
Turns out the answer was in the documentation:
http://drupal.org/node/319190
I've renamed the question to make it clear where the problem was. This narrows the question to a rather specific use-case, but both Content Taxonomy and Hierarchical Select are useful and recommended modules for dealing with large taxonomies.
(注意:假设此处为 Drupal 6)
查看本地示例,我的内容分类字段没有“tids”属性,但我只使用“optionwidgets_buttons”类型的字段,因此如果您使用其他小部件类型,这可能会有所不同。您可以从您尝试操作的表单中发布内容分类字段结构的转储吗?
另一方面,内容分类字段有一些特殊的交互逻辑,涉及稍后在表单/节点处理中将其“转换”回标准分类数组格式,这可能会影响您。那么您的问题是您对默认值的更改根本不显示在初始表单显示上,或者它们显示了,但稍后没有得到处理?
编辑(问题更新后):抱歉回复晚了。我不确定,但查看您的字段定义,类型显示为
hierarchical_select
,这将暗示“独立”分层选择小部件。当与内容分类法结合使用时,“hs_content_taxonomy”子模块中的代码将使我期望使用content_taxonomy_hs
类型。这只是猜测,我没有时间自己在本地正确测试这一点,但您可能想检查是否使用了正确的模块组合/配置。另一件事是检查与分层选择模块相关的模块权重设置 - 也只是一个疯狂的猜测,但也许您的更改需要在分层选择发挥其魔力之前/之后进行,所以您可能想要测试调整模块上的权重。
(NOTE: Assuming Drupal 6 here)
Looking at a local example, my Content Taxonomy fields don't have a 'tids' property, but I only use fields of type 'optionwidgets_buttons', so this might be different if you use other widget types. Could you post a dump of the Content Taxonomy field structure from the form you are trying to manipulate?
On another note, Content Taxonomy fields have some special interaction logic concerning their 'conversion' back to the standard taxonomy array format later on in the form/node processing, which might affect you. So is your problem that your changes to the default values do not show up at all on initial form display, or do they show, but don't get processed later on?
Edit (after question update): Sorry for late reply. I'm not sure, but looking at your field definition, the type shows as
hierarchical_select
, which would hint on a 'standalone' hierarchical select widget. When used in conjunction with content taxonomy, the code from the 'hs_content_taxonomy' submodule would make me expect a type ofcontent_taxonomy_hs
instead. This is all just guesswork, and I do not have the time to properly test this locally myself, but you might want to check if you're using the right module combination/configuration.Another thing would be to check your modules weight settings in relation to the hierarchical select module(s) - also just a wild guess, but maybe your alteration needs to take place before/after hierarchical select does its magic, so you might want to test with adjusted weights on your module.