提交时从表单日期元素中提取值
我正在编写一个 Drupal 模块,并且遇到了一个应该是微不足道的问题;
考虑一个“人”类型节点,其中包含“出生日期”属性。我已经将数据库表配置为使用 int 字段以 UNIX 格式存储日期(与 Drupal Core 相同),但当然我需要提供一种方法让用户以更友好的格式指定值,即 dd/毫米/年。
在我的模块 Person_form 函数中,我这样配置了日期元素:
$form['dob'] = array(
'#type' => 'date',
'#title' => 'Date of Birth',
'#required' => TRUE,
'#default_value' => array(
'day' => format_date($node->dob, 'custom', 'j'),
'month' => format_date($node->dob, 'custom', 'n'),
'year' => format_date($node->dob, 'custom', 'Y'),
),
);
它在三个下拉列表中显示数据库中的 UNIX 日期,每个下拉列表分别代表日、月和年 - 这正是我想要的,到目前为止一切顺利。
现在,当用户单击“保存”按钮时,我需要能够访问这些元素值 - 但我现在有两个问题:
- _form 函数的签名是 Person_form($node) 那么如何访问 $form[ 'dob']['日'/'月'/'年'] 值。 $form 不作为参数传递给函数。
- 获得这些值后,生成 UNIX 日期值的最佳方法是什么?
我已经在 Google 上搜索了一段时间,但术语“drupal”和“date”似乎只是将我指向日期模块扩展 - 当然我不需要扩展来处理这样的简单日期:-o
提前致谢
I'm writing a Drupal module and have run into what should be a trivial problem;
Consider a 'Person' type node which includes a 'Date of Birth' property. I've configured the database table to use an int field to store the date in UNIX format (same as Drupal Core) but of course I need to provide a method for the user to specify the value in a more friendly format, i.e. dd/mm/yyyy.
In my modules Person_form function I have configured the date element thus:
$form['dob'] = array(
'#type' => 'date',
'#title' => 'Date of Birth',
'#required' => TRUE,
'#default_value' => array(
'day' => format_date($node->dob, 'custom', 'j'),
'month' => format_date($node->dob, 'custom', 'n'),
'year' => format_date($node->dob, 'custom', 'Y'),
),
);
which displays the UNIX date in the database in three dropdown lists, oneeach for day, month and year - this is just what I wanted, so far so good.
Now I need to be able to access this elements values when the user clicks on the 'Save' button - but I now have two problems:
- The signature of the _form function is Person_form($node) so how do I access the $form['dob']['day'/'month'/'year'] values. $form is not passed as an argument to the function.
- Once I have the values, what is the best way of generating a UNIX date value?
I've Googled about for a while but the terms 'drupal' and 'date' just seem to point me to the Date module extension - surely I don't need an extension to handle simple dates like this :-o
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
hook_form()
被调用以显示表单用于编辑现有节点或创建新节点的字段。在第一种情况下,要显示的值是$node
中已存在的值。hook_load()
在调用之前加载与节点关联的值;返回值是将添加到节点对象的项目数组(例如,如果返回的数组是 array('extra_field' => 'value'),则节点将具有属性$node->extra_field
)。hook_update()
当节点被调用时被调用保存在数据库中(实现内容类型的模块应将其自己的值保存在其数据库表中),而hook_validate()
被调用来验证用户输入的值。由于您有日、月和年,您可以使用
mktime( )
获取 Unix 时间戳;您获得的时间戳将设置为当天的午夜。为了澄清我在评论中所说的关于不同按钮的不同提交处理程序的内容,这是
node_form()
:每个按钮都有自己的提交处理程序(用项目
#submit
定义)。如果您使用$form['#submit'] = 'submission_function'
设置$form
的提交处理程序,则该提交处理程序将用于每个不这样做的按钮t 定义自己的提交处理程序。您可以使用三个文本字段来表示日、月和年(可以内联显示它们,并且可以使用
'#field_suffix' => ' 设置放置在它们之间的
)。使用日期表单字段的优点是日、月、年字段根据站点设置的日期格式进行排序;如果日期格式在日和月之前显示年份,那么日期字段也会在其他两个字段之前显示年份。另一个优点是您不需要创建自定义代码来验证从用户获得的输入。/
/'日期字段的值是一个包含索引日、月和年的数组。
hook_form()
is invoked to show the form fields to edit an existing node, or creating a new one. In the first case, the values to show are the ones already present in$node
.hook_load()
is before invoked to load the values associate with the node; the return value is an array of items that will be added to the node object (in example, if the returned array isarray('extra_field' => 'value')
, then the node will have the property$node->extra_field
).hook_update()
is invoked when the node is saved in the database (a module implementing a content type should save its own values in its database table), whilehook_validate()
is invoked to validate the values entered from the users.As you have day, month, and year, you can use
mktime()
to get a Unix timestamp; the timestamp you get will be set to midnight of that day.To clear what I said in my comment about different submission handlers for different buttons, this is the code used by
node_form()
:Each button has its own submission handler (defined with the item
#submit
). If you would set the submission handler for$form
with$form['#submit'] = 'submission_function'
, that submission handler would be used for every button that doesn't define its own submission handler.You could use three textfields for day, month, and year (it is possible to show them inline, and the
/
to place between them would be set using'#field_suffix' => '/'
). The pro of using the date form field is that day, month, and year fields are ordered basing on the date format set for the site; if the date format shows the year before day and month, then also the date field would show the year before the other two fields. Another pro is that you don't need to create custom code to validate the input obtained from the user.The value of the date field is an array containing the indexes day, month, and year.
保存时调用的函数的签名应为
person_form_submit($form, &$form_state)
。由此,您可以使用$form_state
数组来检索您的值并按照您想要的方式处理它们。我不是关于您对person_form($node)
的使用,而是要构建您应该使用person_form($form_state)
作为表单构建器函数的表单。查看在 Drupal 中构建表单的文档:http://drupal.org/node/751826。要从日期获取时间戳,我将使用 php 函数 strtotime()< /a>.
The signature of the function that gets called upon save should be
person_form_submit($form, &$form_state)
. From this you can use the$form_state
array to retrieve your values and process them how you want. I'm not about your use ofperson_form($node)
, as to build the form you should be usingperson_form($form_state)
as the form builder function. Check out the documentation for building forms in Drupal: http://drupal.org/node/751826.To get a timestamp from a date, I would use the php function strtotime().