Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim’s picture

It's unlikely I'm going to need a D7 version of this for any of my own projects for a fair while...

Consider this to be me throwing this open for anyone interested to work on it :)

DamienMcKenna’s picture

Subscribe.

calefilm’s picture

Subscribe

muschpusch’s picture

pfff.. i have a hard time porting this to D7. Most of the validation and altering stuff can be done with the date API. But hiding / unsetting the second date field is a pain because than date validation fails... Any idea's on this? I got it working with display none on the second date field but that shouldn't be the way to do it...

joachim’s picture

If date validation is expecting to find a value in a particular $form_state['values'] key, then you could make sure that exists by having a form element that's of #type 'value', with the right form array key.

joachim’s picture

@muschpusch do you want to post a patch of what you have so far? Perhaps someone else could lend a hand :)

incaic’s picture

The following worked for me:

Simply add the name of this function to your date element's [#after_build] from within a hook_form_FORM_ID_alter() function. This will make the field type 'hidden'.

/**
 * Single day
 */
function <modulename>_single_day_after_build($element) {
  foreach($element as $key => &$field) {
    if (is_numeric($key) && is_array($field) && isset($field['value']) && isset($field['value2'])) {
      $field['value']['time']['#title'] = 'Start Time';
      $field['value2']['#title'] = '';
      $field['value2']['time']['#title'] = 'End Time';
      // change textfield to hidden
      $field['value2']['date']['#type'] = 'hidden'; // unset() will cause warnings
      $field['value2']['date']['#theme'] = 'hidden';
      $field['value2']['date']['#title'] = '';
    }
  }
  return $element;
}

This validation alter is needed as date_combo_validate() checks for empty. This will make the 'hidden' to_date the same as the from_date.

/**
 * Implements hook_date_popup_pre_validate_alter().
 */
function <modulename>_date_popup_pre_validate_alter(&$element, &$form_state, &$input) {
  $parents = $element['#parents'];
  $parent = array_pop($parents);
  $field_name = $element['#field']['field_name']; 
  if ($parent == 'value2' && $field_name == '<fieldname>') {
    // Traverse through form_state making sure path exists
    $valid = TRUE;
    // no need for 'values' as only 'input' is used in date_combo_validate()
    $curr =& $form_state['input'];
    for ($i=0, $l = count($parents); $i < $l; ++$i) {
      if (!isset($curr[$parents[$i]])) {
        $valid = FALSE;
        break; // path doesn't exist
      } else if (($i < $l - 1) && !is_array($curr[$parents[$i]])) {
        $valid = FALSE;
        break; // path doesn't exist
      }
      $curr =& $curr[$parents[$i]];
    }
    // If path exists then copy from_date to to_date
    if ($valid) {
      $from_date = $curr['value']['date'];
      $element['date']['#default_value'] = $from_date;
      $element['date']['#value'] = $from_date;
      $input['date'] = $from_date;
      $curr['value2']['date'] = $from_date;
    }
  }
}

FYI: It would have been better to use _date_combo_pre_validate_alter() as it would be easier/cleaner to copy from_date to to_date since both values are available. BUT, in date_combo_validate() the $form_state values, for some reason, are stored in 2 variables $item and $posted before calling drupal_alter() and are used for validation afterwards. So any changes to $form_state from a custom alter function are ignored. Posted the date module issue here: http://drupal.org/node/1981548

joachim’s picture

Thanks for posting your findings.

I you could post your changes as a patch, I'll commit them to a new D7 branch :)

markdavidzahn’s picture

I desperately need this for Drupal 7 and am hoping a D7 branch is available soon. I don't quite have the know-how to implement #7 above.

joachim’s picture

@markzahn: Modules here are maintained by volunteers, and this particular one is not maintained much at all, because the site I developed it for is long gone and I don't have the free time to work on it. What you could do is hire yourself a developer to complete the porting work, and have them post their work in the form of a patch here. I can then commit it and make a D7 release.

manoloka’s picture

incaic can you provide a patch for 7#?

incaic’s picture

Sure ... but can't promise a time frame.

NWOM’s picture

Awesome. I'm glad to hear. I could really use this as well. Thanks in advance!

jgullstr’s picture

Issue summary: View changes
Status: Active » Needs work
FileSize
10.97 KB

Here's a patch for an initial Drupal 7 version. A setting for single day is provided to date_select, date_text and date_popup widgets. It is, however, currently only ever tested on date_popup.

NWOM’s picture

Thank you so much jgullstr for working on the port. I went ahead and tried testing it, and noticed that it errors out on an unlimited field when clicking "Add Another" date.

The following error message is displayed:
PHP Fatal error: Cannot use string offset as an array in date_single_day.module on line 121

I know that this is still in progress, but I figured this might help. Thanks again.

Edit: It also shows the same error message when saving the node with only one date added. I'm assuming it has a problem with multiple dates in general.

jgullstr’s picture

Assigned: Unassigned » jgullstr

Thanks for the feedback NWOM, I will check it out in the next couple of days.

jgullstr’s picture

Some updates. Should work better with multi-dates now.

jgullstr’s picture

Assigned: jgullstr » Unassigned
Status: Needs work » Needs review
NWOM’s picture

Works perfectly! Thanks so much. I was able to create new nodes, edit nodes, etc without any problems.

However I noticed prior that the patch does not work against the main release, and also the dev branch via git. I instead used your entire patch and removed all "-" lines by hand etc.

jgullstr’s picture

Previous patch was against master branch, here's one for 6.x-1.x. Also added an isset condition for widget settings in date_single_day_date_popup_process() to avoid notices.

joachim’s picture

Just a couple of minor points:

  1. +++ b/date_single_day.info
    @@ -1,6 +1,7 @@
    -dependencies[] = date_popup
    ...
    +dependencies[] = date_popup
    

    Not sure why this dependency line gets moved around.

  2. +++ b/date_single_day.module
    @@ -1,176 +1,158 @@
    +  if (module_exists('date_popup')) {
    +    $info['date_popup']['settings'] += array(
    +      'date_single_day' => 0,
    +    );
       }
    

    This is fine as it is, but it's quicker to just check the $info array for the presence of the 'date_popup' widget.

jgullstr’s picture

Thanks for your comments, joachim. I replaced the date dependency with date_popup (Date is inherited from date_popup). Removed module_exists() since date_popup is already a dependency. Also removed date_select and date_text from hook_widget_info_alter, as they did not work anyway. I think this version might be ok for a 7.x-dev release.

7nomad’s picture

Content-type having the date field is referenced using entity reference with widget type Inline entity form. Patch #22 worked for me when I created nodes normally. When I tried to create node using Inline entity form. I got an error

A valid date is required for End date value #1

drubage’s picture

Does anyone have a D7 version put together for download instead of these patches?

joachim’s picture

Patch in #22 doesn't apply I'm afraid.

BTW, if anyone would like co-maintainership (or indeed, ownership) of this project, please do say!

jgullstr’s picture

I'd be happy to help out developing and maintaining a stable D7 branch of this module.

joachim’s picture

Great! I've filed an issue for that, so it's a bit more formal: #2168057: Request for co-maintainers.

jgullstr’s picture

Status: Needs review » Closed (fixed)

I have just commited a 7.x-2.x branch of this module. An alpha1 release should be available for download in a few hours. Please test it out and report possible errors as new issues. Setting issue to "Closed (fixed)".

sachbearbeiter’s picture

thanks a lot
sb