Problem/Motivation

I am trying to create a Views table and have a Office Hours field on one of our CT. I want to show each day from Office Hours field in an individual column. One way in my mind is to add the Office Hours field and exclude it from display and then add 7 custom text area fields and use 'rewrite the output of this field' option on each one of them and then use something like {{ field_office_hours[0, ..., 7] }} but that didn't work. Is there any easy way of doing this?

Proposed resolution

One way in my mind is to add the Office Hours field and exclude it from display and then add 7 custom text area fields and use 'rewrite the output of this field' option on each one of them and then use something like {{ field_office_hours[0, ..., 7] }}.

Comments

Ahmed.Raza created an issue. See original summary.

johnv’s picture

Please tell me which formatter you select.
There is a 'default' formatter, which just outputs text.
There is a 'table' formatter, that outputs a ... table.
That comes close to your request.
Or, perhaps, you want a Views record for each day, as if the field is a multi-value field.

johnv’s picture

Component: Code » Integrating Views
ahmed.raza’s picture

StatusFileSize
new175.79 KB

Hi @johnv, we need the ability to show days like this on a Views table. Basically each day of the week showing its hours in individual column.

johnv’s picture

Hi, I am not very good in theming.
The formatters that are provided show all data as a list, not as a row.

i think you can use either one of the following methods:
- provide a new formatter
- use the 'default' formatter, and set the 'separators' in the formatter settings, to create/mimic the columns.
- use a preprocess hook from office_hours.theme.inc . Here you have the formatted 'office_hours', and also the original 'itemList'to work with
- change a twig template, the are in the 'templates' directory

I am keen on hearing your result.

ahmed.raza’s picture

StatusFileSize
new132.48 KB
new125.08 KB
new98.36 KB

Hi Johnv, I ended up writing my own Views field plugin and rendering the hours information for each day of the week in its own column. But it'd be great to have separate fields in the Views and this is something doable, an example is the popular Address field module. This is a should have in case you want to export the data into a CSV for example Google Business that was the case for us we had to export the data into a specific format which required us to have these opening and closing hours information of each day of the week into its own column within the CSV. We were using views_data_export to generate the CSV but had no way to showing hours of each day into their separate columns.

My custom solution:
My Solution 1
Custom Field Configs

Address module fields:
Address Module

johnv’s picture

In the past I did updte Google Places with views_data_export.
For that, i used the Default Text formatter, notthe Table formatter.
Then, you can set the delimiter to ; or , and export the data.
I will check your code nonetheless.

bisonbleu’s picture

@Ahmed.Raza Your solution is also what I'm looking for, do you mind sharing your code ?

johnv’s picture

Version: 8.x-1.11 » 8.x-1.x-dev

You may try the following:
- create a view;
- under Fields, add fields Title, ID, office_hours
- under Format/Format/Format, set to 'Unformatted list' of 'Fields'
- under Format/Format/Settings, unset 'Add views row classes'
- under Format/Show, set to 'Fields',
- Under Format/Show/Settings, set 'Provide default field wrapper elements' to be able to set all fields to 'Inline', adding ';' as a separator;
-- Then, unset 'Provide default field wrapper elements'. See #2715015: inline fields and field wrapper elements: UI unexpected behavior
- under Fields, set Office_hours formatter to 'plain text'. Set the days separator to ';', removing the default line break. Set other formatting options.

Now, for all fields, set the Views options to make the View aw clean as possible:
- unset Link label to the referenced entity
- unset Style settings / Add default classes
- set Rewrite results / Strip HTML tags
- set Rewrite results / Remove whitespace (if needed)
- ...

This should give the required result for a csv export. Views_data_export can be used to generate a file in batch mode.

- For Office_hours, set the default formatter. This is/should be plain text.

johnv’s picture

Above instructions were updated.

The following issue fixes a problem with white spaces in the output: #3384240: Remove white space in plain text formatter

johnv’s picture

ahmed.raza’s picture

StatusFileSize
new442 bytes
new2.63 KB

Hi guys here are the files from custom solution in case someone is looking the same thing.

Place "OfficeHours.php" inside 'src/Plugin/views/field/' of your custom mdoule and "mymodule.views.inc" inside your custom modules' root folder. Make the appropriate changes as per your needs clear the caches and you should be good to go.

Thanks

  • johnv committed cd6c5c5b on 8.x-1.x
    Issue #3374111: Each day in individual Views Table Column
    
johnv’s picture

Version: 8.x-1.x-dev » 8.x-1.12
Status: Active » Fixed

@ahmed.azra, thanks a lot for your code.
I gave me a good starting point. The end code doesn't look like it at all, though.

Please check latest dev version, or upcoming vesion 8.x-1.13

Feedback is appreciated.

  • johnv committed c910e02e on 8.x-1.x
    Issue #3374111: Each day in individual Views Table Column
    

Status: Fixed » Closed (fixed)

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