Hi,
when admin add a new user I get:

Notice: Undefined index: timezone in date_field_validate() (line 330 - C:\wamp\www\mysite\sites\all\modules\date\date.field.inc).

Whats wrong?
Thanks.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

euginewm’s picture

Status: Active » Needs review
FileSize
749 bytes

Please see my patch.

Status: Needs review » Needs work
jjmackow’s picture

This issue is also occurring when a new registration is created by an anonymous user. I'm using Profile2 as well, but I do not have any date fields in any of my profile types. Perhaps Profile2 is a red herring but I'll try to disable it and see if the issue continues.

I can see that $items is getting passed into the function date_field_validation() function and that it the failure is coming from a call to:

line 330: $date1 = new DateObject($item['value'], $item['timezone'], date_type_format($field['type']));

How can I trap which module is calling the date_field_validate() function?

jjmackow’s picture

An update: on line 330, $item['timezone'] is being passed a value of
[timezone] => America/New_York

but when this value is changed to "GMT", I am able to have an anonymous user create a registration. I still don't know where Timezone is coming from since I do not have any date fields in my profile

jjmackow’s picture

I've temporarily fixed my issue by changing the following on line 328 of date.field.inc:

from
if (is_array($item) && isset($item['value'])) {
to
if (is_array($item) && isset($item['value']) && isset($item['timezone'])) {

s_u_s_a_f_o_n’s picture

Thaks for an inspiration, i have fixed that issue by adding following code:

if (!isset($item['timezone'])) $item['timezone'] = date_default_timezone_get();

jkaine’s picture

S_u_s_a_f_o_n,

Where did you add this line of code?

Thanks,
JK

KarenS’s picture

Status: Needs work » Postponed (maintainer needs more info)

There is no patch so this is not 'needs work'. I can't do anything about this without information about how to reproduce the problem on a clean install.

rcaracaus’s picture

I was getting the same error and I fixed the module with the info from comment #5 I have created a patch.. tell me if it works...

If it is your first time using a patch go here:

http://drupal.org/project/date/git-instructions

kalabro’s picture

I has the same notice in these situations:

function date_test_form_article_node_form_alter(&$form, &$form_state) {
  $form['field_date']['#access'] = FALSE;
}

// Or for disabled:

function date_test_form_article_node_form_alter(&$form, &$form_state) {
  $form['field_date']['#disabled'] = TRUE;
}

Result: Notice: Undefined index: timezone in date_field_validate() (line 344 of /vhosts/test.home/sites/all/modules/date/date.field.inc).
Article created with date field setted to current time.

KarenS’s picture

No one yet has told me how to reproduce the problem. I don't see this error.

kalabro’s picture

How to reproduce:

  1. Add any date field to content type Article.
  2. Create a module with code in my comment #10 or use “Hide” checkbox from nodeformcols module.
  3. Try to create new article or edit existing one.

You will see notice and article will be created with date field setted to current time. I have strange problems with my custom AJAX, too. But everything works with alpha3.

KarenS’s picture

Status: Postponed (maintainer needs more info) » Fixed

This looks like the same underlying problem as #1359464: Date field in user profile show error if hidden. I just committed a fix for that and I don't see any error here.

kalabro’s picture

Thank you! It completely solved the problem.

ntigh52’s picture

Thank you Karen ! It completely solved the problem, on rc2 !!!!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

szantog’s picture

Version: 7.x-2.0-rc1 » 7.x-2.5
Status: Closed (fixed) » Active

It seems, there should be other cases, when $item['timezone'] is not built yet:

I have a form with date field, and some other fields, which use ajax. During the ajax processing the validation is called. I haven't idea what is the best way to fix, but insert
$item['timezone'] = isset($item['timezone']) ? $item['timezone'] : date_default_timezone_object();
before new DateObject is called solved this.

izus’s picture

Version: 7.x-2.5 » 7.x-2.6
Issue summary: View changes

experienced the same issue with 2.6 version

podarok’s picture

Status: Active » Needs review

status for bot

Status: Needs review » Needs work

The last submitted patch, 9: unidentified_index_error-1392472-5.patch, failed testing.

Taran2L’s picture

I can confirm #17. When form uses ajax, this PHP notice occur.

Taran2L’s picture

Version: 7.x-2.6 » 7.x-2.x-dev
Status: Needs work » Needs review
FileSize
1.48 KB
vijaycs85’s picture

Issue tags: +Needs tests

Lets add some test to cover this warning.

vijaycs85’s picture

Issue tags: +sprint
Antoine_k’s picture

Thanks #22 worked perfectly for me!

jvandooren’s picture

Patch #22 also fixed this issue for us. Can someone evaluate this patch and see if it can be committed?

podarok’s picture

Status: Needs review » Needs work

#22 looks good for me. RTBC for this part.
Agree with #23 - before committed this part of code should be covered by tests. Anyone?

jeff h’s picture

Status: Needs work » Reviewed & tested by the community

This patch worked for me. It fixed a PHP warning I was getting about the timezone not being set when using entity metadata wrappers to set a date field.

podarok’s picture

Status: Reviewed & tested by the community » Needs work

still needs test before merging in...

Taran2L’s picture

Willing to write a test, but now can't reproduce it ... Anyone to help?

FluxSauce’s picture

I can reproduce this when migrating content to a date field and I ignore the :timezone destination.

maen’s picture

fixed my warning too!

solotandem’s picture

I tested this with release 2.9 and the latest 2.x code which includes six more commits (latest on 2016-01-08). I find no warning with the 2.x code, but can 'trigger' it with the 2.9 code under specific conditions. In my case, I narrowed down the cause to the following flow:

  • element validate routines fire
  • one field on form generates an error
  • date_combo_validate() runs and does not execute code inside elseif (!form_get_errors()) block
  • $item array is not built out with keys like 'timezone'
  • custom code unsets the single form error
  • field validate routines fire
  • date_field_validate() runs and the same if (!form_get_errors()) condition is now satisfied
  • the code block is entered
  • the 'timezone' key is missing on $item
  • the PHP warning is generated

The above-mentioned condition in date_combo_validate() was replaced in this commit by an else condition so that the $item array is always built out.

Given this issue was created four years ago, a lot may have changed in date module. The various comments suggest to me the error was occurring under certain conditions (as in my use case outlined above) but not generally; and the conditions were not identified (to wit Karen could not reproduce the warning). Unless someone can produce a use case under which the error occurs using the latest release, it would seem this issue has been resolved by the commit referenced above.

nickonom’s picture

The patch in 22 gives hunk in PHP 7.0.8:

patch < *patch
patching file date.field.inc
Hunk #1 succeeded at 373 (offset 12 lines).

but still take cares of the error message. RTBC.

anrikun’s picture

+1 for patch #22 RTBC

vijaycs85’s picture

Status: Needs work » Postponed (maintainer needs more info)

I can't reproduce the warning. As per @solotandem on #33, the warning has been fixed already. If you can still get warning, please provide steps to reproduce please? @anrikun or @nickonom?

JvE’s picture

date_field_validate() (and perhaps other code) expects items to have both a "value" and a "timezone" key.
This "timezone" info is added in date_field_presave() which is not called until the entity is saved.

So any time you set a date field value with EntityMetadataWrapper (or another programmatic way) you will have an entity without a timezone until it is saved.

Solution 1: don't depend on "timezone" being present in date_field_validate() (which is what patch #22 does)
Solution 2: add the "timezone" in date_entity_metadata_struct_setter()

JvE’s picture

Status: Postponed (maintainer needs more info) » Active
ultrabob’s picture

Status: Active » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 22: date-undefined_index_timezone_1392472-22.patch, failed testing. View results

steinmb’s picture

Title: Undefined index: timezone » Undefined index: timezone in date.field.inc
Status: Needs work » Needs review
Issue tags: -sprint
FileSize
1.31 KB

Manual re-roll. Did not test it. Can anyone that see it tell me how to re-produce this notice?

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @steinmb, I was able to reproduce the error in my local environment and noticed it in my logs. This patch indeed fixes the issue.

joelpittet’s picture

@steinmb the steps I have is that I have a date field that is hidden through field_permissions on a page that is using node_authlink.
LMK if you need more context.

lazzyvn’s picture

I have the same probleme with date default empty
i think must be check !empty($item['value'])) NOT isset($item['value'])
here is my patch

DamienMcKenna’s picture

@lazzyn: That looks like a reasonable change. However, would you please open a new issue and cross reference it rather, than adding to an issue that's already RTBC? Thank you.

  • DamienMcKenna committed 68eab4b on 7.x-2.x
    Issue #1392472 by Taran2L, jvandooren, podarok, jeff h, FluxSauce, maen...
DamienMcKenna’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs tests
Parent issue: » #2867810: Plan for Date 7.x-2.11 release

Committed. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.