访问 post 变量时仅获取第一个字符

发布于 2024-09-15 14:20:54 字数 5960 浏览 5 评论 0原文

我在 Drupal 中处理事务的常用方法是创建 CCK 类型,然后创建自定义提交处理程序以在提交节点时对 CCK 字段执行逻辑。但是,当 CCK 字段类型为日期字段时,我无法获取该值,因为数组的结构不同。

更新 - João Guilherme 有一个更简单的方法,但我仍然无法从下面发布的数组中获取日期值。

$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 正在打印 Sat 8/21/2010 的第一个字符“S”,这是我想要的值 ,但我想要整个值。我知道这与我访问数组的方式有关,但我无法弄清楚。

这就是我正在处理的内容 - 我向我尝试访问的值添加了注释:

Which 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
            )

    )

A common way I handle things in Drupal is to create a CCK type, and then create a custom submit handler to perform logic on the CCK fields when the node is submitted. However, I have been unable to get the value when the CCK field type is a Date field because the array is structured differently.

UPDATE - João Guilherme had a much simpler approach, but I still couldn't get the date value out of the array posted below.

$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
        )

)

I then tried adding these which printed null. I also tried the "#" before date and value.

['value']['date']

*************************Original Post**********************************

<?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 is printing the first character "S" of Sat 8/21/2010 which is the value I want , but I want the whole value. I know this has something do with how I'm accessing the array but I can't figure it out.

This is what I'm working with - I added a comment to the value I'm trying to access:

Which 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
            )

    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最美的太阳 2024-09-22 14:20:54

您应该使用$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);
}

You should use the value submitted by the user which is on the $form_state;

I tested with a single date field, instead of a datefield with 2 values. But this should work just fine anyways. I might have the naming of your field incorrect, but you can check out the values on $form_state['values'], where you should find your field listed.

Anyways the code would look something like this:

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);
}
对不⑦ 2024-09-22 14:20:54

您得到的返回是因为您正在访问字符串,而不是数组。

$string = "ABCDEFG";
// This would print "A".
print $string[0];

请注意,提交处理程序从 $form_state['values'] 获取提交的值,而不是从 $form 获取。

The return you get is because you are accessing a string, and not an array.

$string = "ABCDEFG";
// This would print "A".
print $string[0];

To notice then that a submission handler takes the submitted values from $form_state['values'], not from $form.

梦里人 2024-09-22 14:20:54

试试这个,当我真正查看代码并发现我之前已经偏离了方向时,我删除了之前的答案。似乎创建了很多冗余数组,在我看来,这些数组只是让问题变得混乱。这有效吗?

$fromDate = $form[0]['#post']['field_bill_start'][0]['value']['date'];
drupal_set_message("From Date = ".$fromDate);

编辑:

更新代码以再试一次

Try this instead, I deleted my previous answer once I really looked at the code and noticed I was way off before. There seems to be a lot of redundant arrays created that in my opinion are just confusing the issue. Does this work?

$fromDate = $form[0]['#post']['field_bill_start'][0]['value']['date'];
drupal_set_message("From Date = ".$fromDate);

EDIT:

Updated the code to have another try

折戟 2024-09-22 14:20:54

我认为您只需要打印 $form['field_bill_start'][0]['#value']

更新:直接打印$form['field_bill_start'][0]['#value'],不要将其添加到变量或数组中。

I think you just need to print $form['field_bill_start'][0]['#value'].

Update: print directly the $form['field_bill_start'][0]['#value'], don't add it to a variable or array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文