Looking for a bit of guidance here. I'm calling drupal_get_form on the node_form in a custom module (implementing it as a step in the ctools form wizard). I've modeled the call off of the code in node_add() in node.page.inc.

The form loads properly if no date field is present. However, if the node type being loaded utilizes a date fields, the following error is generated (many times):

Warning: in_array() expects parameter 2 to be array, null given in DateObject->limitGranularity() (line 483 of /localhost/druce/docroot/sites/all/modules/date/date_api/date_api.module).

It doesn't seem to be like there should be any difference between the core's call to node_form and my own, but I must be missing something. Here's my snippet:

<?php
function ysl_registration_recommendation_form($form, &$form_state)  {
  $type = 'service_provider';

  if (node_access('create', $type)) {
    global $user;
    $types = node_type_get_types();
    $node = (object) array(
      'uid' => $user->uid, 
      'name' => (isset($user->name) ? $user->name : ''), 
      'type' => $type, 
      'language' => LANGUAGE_NONE,
    );

    drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
    $form = drupal_get_form($type . '_node_form', $node);

  return $form;
  }
}
?>

Any thoughts would be appreciated. Thanks!

Comments

jim kirkpatrick’s picture

I'm hitting exact same issue... Trying to get a node form and pre-populate the date field. Same error occurs.

@madmatter23 -- I'll post more about my debugging soon, but did you resolve the issue?

grasmash’s picture

I did not resolved the issue Jim. I've moved onto other more pressing tasks, but I will be circling back around to this eventually. Please let me know if you find the source of the problem during your debugging.

jim kirkpatrick’s picture

Oddly the problem went away as I finished the code I was working on... Looking back at the code I see I now use this:

  // build an empty node and get its form
  global $user;
  module_load_include('inc', 'node', 'node.pages');
  $form = array();
  $node = new stdClass();
  // get node type from menu hook argument object, or plain string if coming from node/add
  $node->type = $node_type;
  $node->uid = $user->uid;
  // set node language from user, or use default
  $node->language = (!empty($user->language) ? $user->language : LANGUAGE_NONE);
  $node->name = (isset($user->name)?$user->name : '');
  $form = node_form($form, $form_state, $node);

The key being the node_form() call, rather than drupal_get_form() as I had before... seems to do more setup stuff, or call more hooks or whatever -- give it a go.

jim kirkpatrick’s picture

Issue summary: View changes

fixing code snippet

murz’s picture

Sometimes got the same problem after upgrading to 2.2.

murz’s picture

This issue sometime repeats on entity edit form submit. But helps clearing site cache and reloading entity edit form. Seems that the problem is near caching date field, maybe $granularity value sometimes missed in cached version of entity?

murz’s picture

Seems sometime form submitted data have no info about date field, but in html form this field is exist.
I try with same object, sometimes all works good, but sometimes - not. When I got the problem, clearing cache and after clearing cache - refresh node edit form helps, all saves normally.
But after some operations I got this error again.

The problem is starting in file date_elements.inc, function date_combo_element_process($element, &$form_state, $form):
after $field = field_widget_field($element, $form_state);
$field = NULL.
So $form_state['field']['field_id'] element is not exist.
On normal operation, in $field I see object instead of NULL.
Why this element is absent, I can't find at now, will try to debug more.

sven.lauer’s picture

I ran into the same issue implementing a "clone" functionality for commerce's products.

Jim's comment in #3 made me realize what I was doing wrong: The callback I was writing was called (via hook_menu()) by drupal_get_form()---but I was doing a drupal_get_form() inside the callback itself, so the form was actually processed twice. In my case, the better solution was to make sure hook_menu() calls my callback directly, and continuing to use drupal_get_form() inside the callback. This will often be the better solution, as it means you have access to the alter()ed form.

So this does not seem to be a bug in the Date module (though I am wondering what it is doing that causes this ...).

flamingalah’s picture

I am having a similar problem with date fields in entities created with the Entity Construction Kit. My date fields act as expected any where else on the site, but when attached to eck entities, the date format/popup doesn't work and I get this error:

Warning: in_array() expects parameter 2 to be array, null given in DateObject->limitGranularity() (line 503 of /srv/http/rotamanager/sites/all/modules/date/date_api/date_api.module).

I'm not sure how to start debugging this.

(I have posted an issue in the eck queue, but no reply after a week so far)

lrhenals’s picture

I have the same problem when I try to create a new user. Im using Profile2 Registration Path module

mdlopresti’s picture

I'm having the same issue as flamingalah in #8

I'm also adding date fields to eck bundles.

Berni86’s picture

I have the same problem that #9

Berni86’s picture

The solution to this problem "#9" is updating Profile2 Registration Path to the newest dev version "7.x-2.x-dev".
Works well.

leschekfm’s picture

For reference I'm linking the issue from the eck queue #1780382: Date popup not working within field collection for eck entities

leschekfm’s picture

I did post a patch for the eck bug. If you are also affected by this please review it in the eck queue.

kurtzhong’s picture

Rather weird, change $form_state to &$form_state solves my problem.

leschekfm’s picture

@kurtzhong: please also post your experience in the linked eck issue, so that the patch can be committed sooner.
Thanks

leschekfm’s picture

Issue summary: View changes

added wrapper tags to example code

smichel’s picture

FWIW, I just hit this same issue, and spent a lot of time searching around and found no solutions that worked for me. I think I read every page that could be found regarding this problem.

In my case, since I'm creating a ctools wizard, I just unset the two fields on the node form before showing the form, and will include date fields separately in the wizard. I wish I could have found a better fix for it....

Gik000’s picture

The problem it's still alive ...

is it possible that after 2 years the solution is still far?

I spent so much time try understanding why it happens ...
there's a problem with date_api and drupal_get_form ... it's the only thing I'm sure about by now.

daveferrara1’s picture

The following issue has a patch that you should look at. My specific problem was adding an ECK form inside VBO _custom_action_form($settings, &$form_state)

https://www.drupal.org/node/2332829

trungonly’s picture

After 9 years I faced exact problem. D7 latest Date version.

Following #6 with debug from Devel Krumo. The variable $form_state was missing.

My try and success is passing $form_state variable from custom module menu call to node_form.

function ysl_registration_recommendation_form($form, &$form_state)  {
    // ...
    $form = node_form($form, $form_state, $node);  // passing correct $form_state here
    // ...
}

Problem solved.