To reproduce:

Create a node with a single instance date_popup field on it. Only add a from_date for simplicity. Mine date Year-Month-Day granularity. Save your node.

Create a node and input a value into this node.

Create a simple hook_form_alter() which changes the node_edit for you node type and add $form['your_date_field']['#access'] = FALSE; For easier testing maybe only do this for a particular user and not others. Then open two browsers to test.

Clear cache. Edit the node. Submit.

Desired behavior:
Values are kept in the node.

Actual behavior:
Values are cleared.

I've tracked this down to a couple issues:

#1 date/date_elements.inc

function date_combo_element_process($element, &$form_state, $form) {
   if (isset($element['#access']) && empty($element['#access'])) {
     return $element;
   }

This code is not setting #default_value for the element to get sent. Thus no value passed along to node_save and the value gets wiped out.

#2: date/date_popup/date_popup.module
In function date_popup_element_value_callback()

$form_state['input'] isn't populated with values from #access = FALSE fields.

I've modified the code like this:

-  $input = drupal_array_get_nested_value($form_state['input'], $element['#parents'], $input_exists);
+  $input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);

There are still more issues to be worked out, but I'm not sure if I'm going to be able to figure them out in a proper manner to form a patch.

Let me know if you need any additional assistance with this.

Similar issues:
#408770: #access of date_combo fields prevents validation
#1267434: Dates fields set to #access=FALSE are emptied on save

Documentation
For those looking for information related to #access = FALSE and $form_state['input'] vs. $form_state['values'] take a look at the documentation here: http://api.drupal.org/api/drupal/includes--form.inc/function/form_builder/7

Update
It doesn't need to be a date popup widget, all others including text and select, fail as well.

Comments

j0rd’s picture

Title: Date values not saved when #access = FALSE and date_popup are used. » Date values cleared, instead of saved when updating a node with a date_popup AND #access = FALSE

Updated title.

j0rd’s picture

Version: 7.x-2.0-alpha4 » 7.x-2.x-dev

Same issue under -dev.

j0rd’s picture

Issue summary: View changes

Added some documentation.

j0rd’s picture

Title: Date values cleared, instead of saved when updating a node with a date_popup AND #access = FALSE » Date values cleared, instead of saved when updating a node when $form['myfield']['#access'] = FALSE

Updating title.

omercioglu’s picture

Sub

das-peter’s picture

sub - since of not yet deployed "Follow" button :)
@j0rd thanks for tracking this down and connect the related issues!

doana’s picture

This issue is affecting one of my sites as well. Thanks to everyone who's working on this!

j0rd’s picture

@doana, not sure if anyone is working on this, so make your voice heard and hopefully we can get some eyes on this.

For me, I've had to remove the use of date for my module because of this problem. I've replaced it with an interger input field which is a unix datetime. I've also added some javascript to provide the calendar popup interface.

function mymodule_form_myform_alter(&$form, &$form_state) {
  if(isset($form['field_expiry_date'])) {                                               
    drupal_add_library('system', 'ui.datepicker');
    drupal_add_js('jQuery(document).ready(function ($) {                                
      var value = $("#edit-field-expiry-date-und-0-value").val();
      if(value) {                                                                   
        var date =  $.datepicker.parseDate("@", value * 1000);                        
        $("#edit-field-expiry-date-und-0-value").val($.datepicker.formatDate("yy-mm-dd", date));
      }
                                                                             
      $("#edit-field-expiry-date-und-0-value").datepicker({
        dateFormat: "yy-mm-dd",                                                     
      });

      $("#job-node-form").submit(function() {
        var value = $("#edit-field-expiry-date-und-0-value").val();
        if(value) {
          var date =  $.datepicker.parseDate("yy-mm-dd", value);
          $("#edit-field-expiry-date-und-0-value").val(parseInt($.datepicker.formatDate("@", date) / 1000));
        }
      });                                                                         
    });', 'inline');                                                                                      
  }
}

Then I'm using this snippet, so that views provides me with the unix datetime to use in my filters:

function mymodule_views_data_alter(&$data) {                                             
  // This appears to work, for getting current unix time on the field i need 
  if(isset($data['field_data_field_expiry_date'])) {
    $data['field_data_field_expiry_date']['field_expiry_date_value']['filter']['handler'] = 'views_handler_filter_date';
  }
}

I may release this little node as a light weight/simple alternative to the date module, but really, if this is fixed in date, it wouldn't be an issue. Plus I really enjoy date_api.module for working with my dates...

If anyone has access to date module maintainers, please let them know about this issue.

das-peter’s picture

Not sure if this is a D7 stable release blocker?
For me it sounds like an essential issue that could break a lot of stuff and thus like a D7 stable release blocker.
Opinions on that?

j0rd’s picture

@das-peter from me looking into the code, it looks like quite a few things needs to get re-factored to resolve this issue.

In my opinion currently, it appears that date module is doing certain things out of order, which is causing this problem. Perhaps Drupal itself has some issues in core as well.

Problem exists from hacking layers and layers of fixes for other issues, creating thing one.

At least from my research digging into this.

Needs someone with a good understanding of Drupal Form API & The Date module to get fixed. Anything I could do, would most likely break other things.

For me, it's a release blocker. I've had to stop using the module. I need the ability to restrict the end user from modifying the value in this date field, as it's tied to payments. If someone smart decides to manipulate the hidden value, then they can get all sorts of free goodies.

renat’s picture

For me, this is a release blocker, too. It leads to the user's data loss and makes impossible usage of some other modules. But, of course, would be nice to hear a word from Date's maintainers.

das-peter’s picture

I've created a summary with a patch here: #1178716-21: Translating node deletes date field entry
My problem with the patch is that I'm absolutely short on time. And since I haven't one of the mentioned scenarios, in a project I've to work on, I can't sideline project time to make wide test runs. Thus it would be great if you could apply the patch and test it in your scenarios.

KarenS’s picture

Status: Active » Closed (duplicate)

This is basically a duplicate of #1178716: Translating node deletes date field entry, which is where all the work to fix it is going on. I think most of the issues are fixed in the very latest -dev code, but I'm still working through it.

KarenS’s picture

Issue summary: View changes

Added additional information.