Formatting office hours from a settings form

Created on 1 August 2024, 5 months ago
Updated 19 September 2024, 4 months ago

Problem/Motivation

This is a follow-up on Use the office hours field widget as Element (in a settings form) Fixed which allows us to include an office hour widget into a settings form. The captured values then get stored in a config entity like e.g.

'apbs_entities.settings:office_hours':
  times:
    1:
      0:
        day: '1'
        starthours:
          time: ''
        endhours:
          time: ''
        comment: ''
      1:
        day: '1'
        starthours:
          time: '07:00'
        endhours:
          time: '10:00'
        comment: ''
      day: '1'
      starthours:
        date: ''
        time: '07:00'
        object: null
      endhours:
        date: ''
        time: '10:00'
        object: null
      comment: ''
    2:
      0:
        day: '2'
        starthours:
          time: '07:00'
        endhours:
          time: '09:00'
        comment: ''
      1:
        day: '2'
        starthours:
          time: ''
        endhours:
          time: ''
        comment: ''
      day: '2'
      starthours:
        date: ''
        time: '07:00'
        object: null
      endhours:
        date: ''
        time: '09:00'
        object: null
      comment: ''

Now, my question is how to render such values in custom code. I have something in mind like this:

        $officeHours = \Drupal::config(Settings::CONFIG_KEY)->get(Settings::OFFICE_HOURS);
        $build = [
          '#theme' => 'office_hours',
          '#parent' => [],
          '#office_hours' => [],
          '#office_hours_field' => $officeHours,
          '#is_open' => TRUE,
          '#item_separator' => Xss::filter(
            '', ['br', 'hr', 'span', 'div']
          ),
          '#slot_separator' => '',
          '#attributes' => [
            'class' => ['office-hours'],
          ],
          '#attached' => [
            'library' => [
              'office_hours/office_hours_formatter',
            ],
          ],
        ];
        $rendered = \Drupal::service('renderer')->renderInIsolation($build);

This doesn't work yet. How should one go about this requirement?

💬 Support request
Status

Active

Version

1.0

Component

Code - formatter

Created by

🇩🇪Germany jurgenhaas Gottmadingen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jurgenhaas
  • 🇳🇱Netherlands johnv

    Hi Jurgen,
    I am not sure.
    Normally, the office hours are displayed using OfficeHoursFormatterTrait::getRows() or OfficeHoursItemList::getFieldRows().
    I guess you can call one of these, fabricating the inputvalues.
    You can also try/check the usage of 'getOfficeHoursPlugin', which returns a formatter.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Hi @johnv thanks for your reply. The problem is, that all these methods require a field or at least field definitions. But when using the office hours in a regular config form, there is no field involved.

Production build 0.71.5 2024