Each day in individual Views Table Column

Created on 12 July 2023, over 1 year ago
Updated 10 February 2024, 9 months ago

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] }}.

Feature request
Status

Fixed

Version

1.12

Component

Integrating Views

Created by

🇵🇰Pakistan Ahmed.Raza

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

Comments & Activities

  • Issue created by @Ahmed.Raza
  • 🇳🇱Netherlands johnv

    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.

  • 🇵🇰Pakistan Ahmed.Raza

    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.

  • 🇳🇱Netherlands johnv

    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.

  • 🇵🇰Pakistan Ahmed.Raza

    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:

    Address module fields:

  • 🇳🇱Netherlands johnv

    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.

  • 🇨🇦Canada bisonbleu

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

  • 🇳🇱Netherlands johnv

    You may try the following:
    - create a view;
    - under Fields, add fields Title, ID, Office_hours
    - under Format/Format, set to 'Unformatted list' of 'Fields'
    - under Format/Show, set to 'Fields', with an 'Unformatted list' of 'Fields'
    - Under Format/Show, set all fields to 'Inline', adding ';' as a separator;
    - under Fields, set Office_hours formatter to 'default', which is a plain text formatter. Set the days formatter to ';', removing the default line break. Set other formatting options.

    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.

  • 🇳🇱Netherlands johnv

    Above instructions were updated.

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

  • 🇵🇰Pakistan Ahmed.Raza

    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
      
  • Status changed to Fixed 10 months ago
  • 🇳🇱Netherlands johnv

    @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
      
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024