Closed (fixed)
Project:
Office Hours
Version:
8.x-1.17
Component:
Code - widget
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
9 Apr 2024 at 15:40 UTC
Updated:
13 Jun 2024 at 13:04 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
johnvI am not sure.
I think you can program a settings form, and add the field. Not sure where the settings are then stored.
Would it be something like this request? #3371688: Exceptions that apply to entire bundles
I am also maintainer fo Workflow module,
and there I have learned how to add a widget to a custom form. Perhaps you can take advantage of it:
Comment #3
johnvIn the past, I encountered a way to create an Article or Basic page, having PHP in the body text. That might be an alternative.
Comment #4
johnvAlso, with the P a r a g r a p h s module, you can add fields on a per page basis.
Comment #5
jurgenhaasThat approach from the workflow module doesn't work, since I don't have any entity type with an office_hours field. I've carefully reviewed the site's entity types and none could be used as a "dummy" to hold a reference to a field.
In a settings form, something like this would be nice:
The, I should receive an array of values when the form gets submitted in the submit handler and should be able to store them in the config entity.
When I try the above code, just nothing appears in the form.
Comment #6
johnvHmm,
in orde to help you, I need your complete, code. But on this level, office_hours acts as any other field.
Ofcourse, you can always add the opening hours as plain text on some page.
Comment #7
jurgenhaasWell, just a regular settings form, as almost all modules bring with them, e.g.
Comment #8
johnvProblem is that your approach uses 'FormElements', not 'FieldWidgets'.
The FormElements are the single time slots, united in the Weekly FieldWidget.
Perhaps it is possible to wrap the time slots in a complex Element, like 'checkboxes' is a wrapper around x 'checkbox' elements.
You may borrow code from OfficeHoursWeekWidget::formElement(), which does
$elements[] = ['#type' => 'office_hours_slot', ...]or from OfficeHoursCOmplextWeekWidget::formElement(), which calles simpler widgets.
Comment #9
jurgenhaasAh, that might work. Thanks a lot @johnv for pointing that out, I'll give that a try. FYI, a couple of years ago I had a similar requirement with the address field module, where a solution become available at some point as well. So, it should be possible here too, let's see. I'll report back.
Comment #10
johnvI have refactored the widget into an element. Now cleaning up code. Please bear with me.
Comment #12
johnvAs a preliminary version, please drop the attached file in the src/Element directory.
Then :
and:
Comment #13
jurgenhaasThis looks very promising. But I had to make a couple of corrections:
OfficeHoursTableTestoffice_hours_table_testEverything else seems to be correct. When I do that, and try to open the form, I'm getting this error:
Note: I'm using the 8.x-1.x dev release for this test.
Comment #14
jurgenhaasIt looks like
OfficeHoursTableTestprovides an array as$element['#value']instead of anOfficeHoursItemComment #19
johnvPlease try the newest dev-version.
Comment #20
johnvComment #21
jurgenhaasThis is amazing!!! Thanks @johnv I've tested the latest dev release and that is working exactly as expected. This is outstanding, thank you so much for providing this feature on request.
Comment #22
johnvYou're welcome :-)
In order to make sure that this functionality won't be lost in the future, a test case is needed.
However, I am not sure how to do that.
Do you know how to create such a test case: create form, save data, check data?
Comment #23
jurgenhaasI'm not an expert in that area, but for our ECA module, a colleague of mine wrote some form submission tests and you can find an example at https://git.drupalcode.org/project/eca/-/blob/2.0.x/modules/form/tests/s...
Maybe that's helpful?
Comment #25
johnvThanks, I will take a look.
Following patch to correct the widget code, which is broken after isolating the Element :-(
Comment #26
johnvComment #28
johnvAdded tag for adding a SettingsForm test like https://git.drupalcode.org/project/eca/-/blob/2.0.x/modules/form/tests/s...
Comment #30
johnvComment #32
johnvI created #3450541: Add test for WeekTableElement in a form (not a widget), so this issue can be closed.
Comment #33
jurgenhaasThank you so much for taking care of this. Really much appreciated.
Comment #34
johnvyou are welcome :-)