We need to convert a couple of fields from date & time to just date.

There is a lot of content and a lot of functionality based of these fields (views, etc), so a new field is out of the question.

Can you see any reason why these are locked after the field has data? There are not any schema changes, so afaick there should be not negative side effects. We have already run one field update script without issues, but now we need to repeat this with another field.

   $description = t('Select the date attributes to collect and store.');
+   if ($has_data) {
+     $description .= ' ' . t('Changes to date attributes only effects new or updated content.');
+   }
   $form['granularity'] = array(
     '#type' => 'checkboxes',
     '#title' => t('Date attributes to collect'),
     '#default_value' => $granularity,
     '#options' => $options,
     '#attributes' => array('class' => array('container-inline')),
     '#description' => $description,
-    '#disabled' => $has_data,
     'year' => $checkbox_year,
   );
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Alan D.’s picture

Title: Unlock date field granularity » Unlock "Date attributes" (granularity) field setting
justindodge’s picture

I'd like to see this unlocked as well - I wanted to mention that I've had similar use cases, and have also changed these field settings programmatically without an adverse affect that I could observe.

In case it helps anyone in the interim, I've used this code to handle these types of changes in the past - this is an example of adding hour and minute granularity to a field called 'field_date'.

  //Load the field info
  $field = field_info_field('field_date');
  
  //Add hour and minute to granularity
  $field['settings']['granularity']['hour'] = 'hour';
  $field['settings']['granularity']['minute'] = 'minute';
  
  //Update the field!
  try {
    field_update_field($field);
    $msg = t('field_date granularity settings were updated successfully.');
  }
  catch (Exception $e) {
    $msg = t('Attempt to update field_date failed: %message.', array('%message' => $e->getMessage()));
  }
  
  return $msg;
justindodge’s picture

Status: Active » Reviewed & tested by the community

I've also used the method illustrated in the original post, and I think it's a safe change to make.

Alan D.’s picture

Status: Reviewed & tested by the community » Needs work
FileSize
830 bytes

Guessing that this is "needs review" until till a patch is rolled and tested

Alan D.’s picture

Status: Needs work » Needs review
5t4rdu5t’s picture

I've tested this patch and works great, just what I was looking for. Thank you!

Alan D.’s picture

@Karen/Tim
And words of wisdom on this? Even if that is: "Should be ok, but I'm worried that I haven't considered something that will blow up somewhere..."

Ken Hawkins’s picture

We were in the same position (needed to stop collection the hour and minute stamp) and this worked fine for us.

Date repeat functionality is supposed to be a more involved change -- has anyone tried that?

haydeniv’s picture

Status: Needs review » Needs work
FileSize
28.47 KB
24.05 KB

When I used this patch and added hour and minute granularity it opened up a disabled "Timezone Handling" field. I am wondering if that also should be enabled? In my situation where I am adding granularity, I would need to configure it because it is now relevant where it wasn't when I first created the field.

Before adding hour and minute:
No hour and minute

After adding hour and minute:
Hour and minute

cr0ss’s picture

Status: Needs work » Needs review
FileSize
1.16 KB

Alan D, thank you for great patch #4, it helps to solve the problem with extending granularity.

Another thing mentioned in #9 is the situation I've faced to when applied patch and tried to intend the granularity. Due to disable option of a timezone field the validation wasn't passing the field settings to store.

Attached patch is slightly changed version of #4 which helped me to resolve the issue. Please review.

wOOge’s picture

Patch from #10 tested, and works.

lgomezma’s picture

Indeed I had the same problem as cr0ss and his patch at #10 solved it.

Any idea how to 'unlock' the required for 'Collect an end date'? I don't want this to be compulsory anymore but it's locked.

Tilo’s picture

One more confimation for patch #10. It works excellent. Many thanks

walidvb’s picture

In case someone is trying to change the granularity, here is the code:

 //Load the field info
  $field = field_info_field('field_date');
  //Add hour and minute to granularity
  $field['settings']['granularity']['hour'] = 'hour';
  $field['settings']['granularity']['minute'] = 'minute';
  //Update the field!
  try {
    field_update_field($field);
    $msg = t('field_date granularity settings were updated successfully.');
  }
  catch (Exception $e) {
    $msg = t('Attempt to update field_date failed: %message.', array('%message' => $e->getMessage()));
  }
  return $msg;

(Credit to someone else, can't find the source anymore. sorry)

EDIT-------------------

Oh, it was in this thread.... my bad..

texas-bronius’s picture

Note: To optionally *remove* hours:minutes granularity, change the 'hour' and 'minute' in the code snippet to NULL. This does not actually update any values already in the database, but their display will have the reduced formatting on View, as will the form fields on Edit-- but it's not clear to me on inspection whether merely hitting Save on an existing field will update its dates with that reduced granularity. I think it does.. just don't know why they're not 00:00 instead of 05:00 :)

podarok’s picture

Assigned: Unassigned » vijaycs85
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

#10 looks good
let`s RTBC

haydeniv’s picture

vijaycs85’s picture

Status: Reviewed & tested by the community » Fixed

Thanks! Comitted 66162b8 and pushed to 7.x-2.x

vijaycs85’s picture

Assigned: vijaycs85 » Unassigned

Status: Fixed » Closed (fixed)

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

Status: Closed (fixed) » Needs review

Status: Needs review » Needs work

The last submitted patch, 10: date-unlock-field-granularity-1840008-1.patch, failed testing.

Alan D.’s picture

Status: Needs work » Closed (fixed)

@cabplan

The code is already in dev, so there is not need to retest this :)