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 😮
Thanks in advance
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$formwith$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.