Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1003607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:00:21+00:00 2026-05-16T08:00:21+00:00

I’m writing a Drupal module and have run into what should be a trivial

  • 0

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:

  1. 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.
  2. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:00:22+00:00Added an answer on May 16, 2026 at 8:00 am

    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 is array('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), while hook_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():

    $form['buttons'] = array();
    $form['buttons']['submit'] = array(
      '#type' => 'submit',
      '#access' => !variable_get('node_preview', 0) || (!form_get_errors() && isset($form_state['node_preview'])),
      '#value' => t('Save'),
      '#weight' => 5,
      '#submit' => array('node_form_submit'),
    );
    $form['buttons']['preview'] = array(
      '#type' => 'submit',
      '#value' => t('Preview'),
      '#weight' => 10,
      '#submit' => array('node_form_build_preview'),
    );
    if (!empty($node->nid) && node_access('delete', $node)) {
      $form['buttons']['delete'] = array(
        '#type' => 'submit',
        '#value' => t('Delete'),
        '#weight' => 15,
        '#submit' => array('node_form_delete_submit'),
      );
    }
    

    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.

    I need to provide a method for the user to specify the value in a more friendly format, i.e. dd/mm/yyyy.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.