For the moment there's only a default value for sunday but there could be a default value for all the week days. So for a company with several shops, there could be default opening hours on when one create a shop content type, no need to start again selecting values. Then if there are some exceptions, the user can edit the field to make some adjustements. What do you think?

Default value
The default value for this field, used when creating new content.
Sunday from ☐☐ ☐ ☐ until ☐☐ ☐ ☐ 

Comments

Jerome F’s picture

An other solution for improvement in the process of filling in the office hours field could be http://drupal.org/node/1168172

johnv’s picture

Some clarification: The default value which now appears on the field settings is provided by core and only supplies 1 value.
ATM Office Hours does supply default values, and has a strange thing here: we do not need 1 default value, but 7...

jonhattan’s picture

Traced it down to field_ui.admin.inc:

2002   // @todo Allow multiple values (requires more work on 'add more' JS handler).
2003   $element += field_default_form($instance['entity_type'], NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state, 0);

I'm looking for a related issue in core. For a field like office_hours that manage multiple values as single, default values with multiple values just works. It's easy to test by removing last argument in the above function call.

johnv’s picture

Perhaps we can override it with a hook_form_FORM_ID_alter() ?

jonhattan’s picture

No, since this is a direct call to _form a function, instead of via drupal_get_form().

The way to fix this is to provide a week widget instead of the hacky way the module provides the widget at present.

jonhattan’s picture

Category: feature » task
Status: Active » Needs review
StatusFileSize
new8.87 KB

Find attached a patch to convert the widget to a multivalued one.

jonhattan’s picture

StatusFileSize
new8.69 KB

Minor fix to previous patch.

jonhattan’s picture

StatusFileSize
new9.67 KB

$field['cardinality'] is not set when first creating the field, and the widget is unable to build itself correctly. Hardcoded to 14.

jonhattan’s picture

Status: Needs review » Needs work

It doesn't play with monday as first day of week.

jonhattan’s picture

Status: Needs work » Needs review
StatusFileSize
new16.86 KB

Another round. Now the widget is almost agnostic about the underlying implementation (0,1=sunday, 2,3=monday, etc).

In summary these are the changes proposed to convert the widget to a multivalued one:

1. Change the widget definition from 'multiple values' => FIELD_BEHAVIOR_DEFAULT to 'multiple values' => FIELD_BEHAVIOR_CUSTOM so we are telling to Drupal that we handle the field when it's multivalued (always). So that Drupal will call our hook_field_widget_form() implementation only once and not 14 times.

2. due to previous point, our widget is no longer based on field_multiple_value_form(). To not reinvent the wheel, lets base our implementation on the relevant code from this function. I just copied and adapted some relevant lines into office_hours_field_widget_form().

3. Working with $items indexed by 14 keys for 7 days and 2 slots per day is a PITA, moreover, days needs a shift based in first day of week. There were a lot of spaghetti in the code relaying on #delta or #weight.
I got rid of all of this and introduced day and daydelta. Day is a 0..6 value and daydelta 0..1, as present limitation to 14 values. Backend doesn't change for now. It could change in concordance to support a standard day number and more than 2 slots per day.

Since backend is unaltered, we need to convert values after loading and before saving. For loading I introduced a new function: office_hours_arrange_field_items() and a new data structure: a two level array based in day and day delta. See code for details. In a follow up issue I'll try to addapt the formatter to this new internal representation.

OTOH, implemented hook_field_presave() to addapt values to backend before saving.

4. Last, theming. Instead of a hacky override of theme_field_multiple_value_form() declared our own theme_office_hours_week(). This is a very stripped down version of theme_field_multiple_value_form() that transforms elements to table rows with no need for reordering.

johnv’s picture

Status: Needs review » Needs work

Finally got time to test this promising patch, after reviewing your other patches.
However, too much has changed. Pleas provide a patch against latest version.

johnv’s picture

Status: Needs work » Fixed

Thanks jonhattan. This is a very valuable patch.
It is committed in commit 9218429.

Status: Fixed » Closed (fixed)

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

ubun2junky’s picture

Title: Default value for every day of the week » Entering Exception dates like holidays

Just wondering if there is any way to implement into this module a way you can also enter specific dates that would be an exception to regular hours. For example holidays. So I could have monday to friday open 9am to 5pm, but on july 4th that lands of a wednesday it would display closed instead of open from 9am to 5pm.

johnv’s picture

Title: Entering Exception dates like holidays » Default value for every day of the week

@ubun2Junky, please do not hi-jack this issue.
Please search for open issues first, and if you cannot find one, open another.
Options:
#1743366: [Season Meta] Add "Seasonal Hours / Duration" feature
#1119406: Add a "description" feature
You can also add a textfield in your entity with the exceptions, and add it below the Office Hours.