我在Drupal中处理事物的一种常见方法是创建一个CCK类型,然后创建一个自定义提交处理程序,以便在提交节点时在CCK字段上执行逻辑。但是,当CCK字段类型是Date字段时,我无法获取该值,因为该数组的结构不同。
更新 - JoãoGuilherme有一个更简单的方法,但我仍然无法从下面发布的数组中获取日期值。
$fromDate = $form['field_bill_start'][0]['#value'];
drupal_set_message('<pre>'. print_r($fromDate, TRUE) .'</pre>');
//Which then prints this.
Array
(
[value] => Array
(
[date] => Tue, 08/24/2010
)
)
然后我尝试添加打印null的这些。我还在日期和价值之前尝试了“#”。
['value']['date']
*************************原帖******************** **************
<?php
function billing_form_billing_node_form_alter(&$form, &$form_state) {
//drupal_set_message('We are hooking it!');
$form['#submit'][] = 'billing_submit_function';
}
function billing_submit_function($form, &$form_state) {
$fromDatePRE = array();
$fromDatePRE[] = $form['field_bill_start'][0]['value']['date'];
$fromDate[] = $fromDatePRE[0]['#value']['#date'];
drupal_set_message('<pre>'. print_r($fromDatePRE[0], TRUE) .'</pre>');
drupal_set_message("From Date = ".$fromDate[0]);
drupal_set_message("From Date2 = ".$fromDatePRE[0]);
$ fromDate正在打印星期六8/21/2010的第一个字符“S”,这是我想要的值,但我想要整个值。我知道这与我如何访问阵列有关,但我无法弄明白。
这就是我正在使用的内容 - 我对我正在尝试访问的值添加了评论:
哪个Yiedls:
Array
(
[0] => Array
(
[#type] => textfield
[#default_value] => Sat, 08/21/2010
[#id] => edit-field-bill-start-0-value-datepicker-popup-0
[#size] => 20
[#maxlength] => 30
[#attributes] => Array
(
)
[#description] => Format: Sat, 08/21/2010
[#post] => Array
(
[title] => llllllllllllll
[menu] => Array
(
[link_title] =>
[parent] => Array
(
[hierarchical_select] => Array
(
[selects] => Array
(
[0] => primary-links:0
[1] => label_1
)
)
)
[weight] => 0
)
[hs_form_build_id] => hs_form_7bf97c3c59ba2a064d6e841ae405dd30
[changed] =>
[form_build_id] => form-1f928faaa990f3809da391165d51981a
[form_token] => 616f6e8a7bb9c4ffc2aec65174c0817d
[form_id] => billing_node_form
[log] =>
[pathauto_perform_alias] => 1
[print_display] => 1
[print_display_urllist] => 1
[print_mail_display] => 1
[print_mail_display_urllist] => 1
[print_pdf_display] => 1
[print_pdf_display_urllist] => 1
[field_bill_start] => Array
(
[0] => Array
(
[value] => Array
(
//This is the value I need
[date] => Sat, 08/21/2010
)
)
)
[field_bill_end] => Array
(
[0] => Array
(
[value] => Array
(
[date] => 08/25/2010
)
)
)
[name] => administrator
[date] =>
[status] => 1
[promote] => 1
[op] => Save
)
[#programmed] =>
[#tree] => 1
[#parents] => Array
(
[0] => field_bill_start
[1] => 0
[2] => value
[3] => date
)
[#array_parents] => Array
(
[0] => field_bill_start
[1] => 0
[2] => value
[3] => date
)
[#weight] => 0
[#processed] => 1
[#required] =>
[#input] => 1
[#autocomplete_path] =>
[#process] => Array
(
[0] => form_expand_ahah
)
[#name] => field_bill_start[0][value][date]
[#value] => Sat, 08/21/2010
[#needs_validation] => 1
[#defaults_loaded] => 1
[#sorted] => 1
)
)
答案 0 :(得分:2)
您应该使用$form_state
;
我使用单个日期字段进行测试,而不是具有2个值的日期字段。但无论如何这应该工作得很好。我的字段命名可能不正确,但您可以查看$form_state['values']
上的值,您应该在哪里找到您的字段。
无论如何,代码看起来像这样:
function billing_submit_function($form, &$form_state) {
$from_date = $form_state['values']['field_bill_start'][0]['value'];
$from_date_string = format_date(strtotime($from_date), 'custom', 'D d/m/Y');
$from_date_2 = $form_state['values']['field_bill_start'][0]['value2'];
$from_date_string_2 = format_date(strtotime($from_date_2), 'custom', 'D d/m/Y');
drupal_set_message("From Date = ".$from_date_string);
drupal_set_message("From Date2 = ".$from_date_string_2);
}
答案 1 :(得分:1)
您获得的回报是因为您正在访问字符串,而不是数组。
$string = "ABCDEFG";
// This would print "A".
print $string[0];
要注意,提交处理程序会从$form_state['values']
获取提交的值,而不是$form
。
答案 2 :(得分:1)
尝试这个,我删除了我之前的答案,一旦我真的看了代码,发现我之前已经离开了。似乎有很多冗余阵列在我看来只是混淆了这个问题。这有用吗?
$fromDate = $form[0]['#post']['field_bill_start'][0]['value']['date'];
drupal_set_message("From Date = ".$fromDate);
编辑:
更新了代码以再试一次
答案 3 :(得分:1)
我认为您只需要打印$form['field_bill_start'][0]['#value']
。
更新:直接打印$form['field_bill_start'][0]['#value']
,不要将其添加到变量或数组中。