Creating a conference website with in Drupal 8 (Also: use views to summarize values of fields)

I’m creating a Drupal website for an upcoming conference where we have sessions with a list of contributions (talks, workshops, …). A contribution will be shown in several sessions (actually streamed as the conference will be online due to COVID-19).

Structure

I designed my content-types like this (I removed all non-relevant fields):

Contribution

  • Duration (field_duration), Integer (time in minutes)

Session

  • Start Date (field_start_date), Date with time
  • Contributions (field_contributions), Entity reference to content type Contribution, multiple values

Tasks

In the display of a Contribution I want to show in which sessions it will be shown and at what exact time (which is the start time of the session plus the sum of all preceding contributions).

Also, in the display of a Session I want to list all contributions with the exact start time (and end time).

Views

I found the module Views Cumulative Field, but found it not sufficent, so I looked for a different solution.

I solved this with views using Views Data Export:

View „Contribution Duration“
  • Create a view „Session Contributions Duration“, type „Data Export“, accepted format: „CSV“
  • List content of type „Contribution“
  • Add contextual filter „Content: ID“ (if not available, show all), allow multiple values (under „More“)
  • Add field „Content: ID“ (nid)
  • Add field „Duration“ (field_duration)

Example output:

nid,field_duration
4,15
6,30
11,30
View „Session Contributions Duration“
  • Create a view „Session Contributions Duration“, type „Data Export“, accepted: „CSV“
  • List content of type „Session“
  • Add Contextual Filter „Content: ID“ (if not available, show all)
  • Add field „Start Date“ (field_start_date), formatter „Plain“
  • Add field „Contributions“ (field_contribution_durations), formatter „View“ (Contribution Duration), view arguments „Field value“, multiple, implode with „,“, override with custom text:
    {{ field_contribution_durations|split("\n")|slice(1)|map(c => c|split(",")|join(":"))|join("|") }}

Example output:

field_start_date,field_contribution_durations
2020-11-25T10:00:00,4:15|6:30
2020-11-25T12:00:00,11:30
2020-11-26T10:00:00,4:15|6:30

(First field is the start_date of the session, second is the list of contributions separated by „|“, where the „:“ separates the ID of the contribution and the duration.

View „Sessions of Contribution“
  • Create a view „Sessions of Contribution“, type HTML List of fields (or other, this is for display).
  • List content of type „Session“
  • Add Contextual Filter „Content: Contributions“ ((if not available, hide)
  • Add field „Contributions“ (field_contributions), formatter „View“, View „Session Contributions Duration“, multiple false, rewrite text:
    {% set start_date = field_contributions|split("\n")[1]|split(",")[0] %}
    {% set durations = field_contributions|split("\n")[1]|split(",")[1]|split("|")|map(d => d|split(":")) %}
    {% set not_found = true %}
    {% set offset = 0 %}
    {% for c in durations if not_found %}
    {% if c[0] == arguments.field_contributions_target_id %}
    {% set not_found = false %}
    {% else %}
    {% set offset = offset + c[1] %}
    {% endif %}
    {% endfor %}
    {{ date(start_date)|date_modify("+" ~ offset ~ "minutes")|date('Y-m-d H:i') }}
Content Type „Contribution“, Manage Display:
  • Using Display Field Copy, I created a copy of the Node ID called Sessions. Use the View „Sessions of Contribution“ to display this value, and pass the field value (the Node ID) as only argument.
View „Contributions in Session“
  • Create a view „Sessions of Contribution“, type HTML List of fields (or other, this is for real display).
  • List content of type „Contribution“
  • Add relationship „Content using field_contributions“
  • Add contextual filter „Content: ID“ (if not available, hide), Relationship none, allow multiple values
  • Add contextual filter „Content: ID“ (if not available, show all), Relationship field_contributions
  • Add field „Content: ID“ (nid), Relationship none, Exclude from display
  • Add field „Content: ID“ (nid_1), Relationship field_contributions, Formatter View, View „Session Contributions Duration“, View Arguments Field Value, rewrite text:
    {% set offset = 0 %}
    {% set not_found = true %}
    {% set durations = nid_1|split("\n")[1]|split(",")[1]|split("|")|map(d => d|split(":")) %}
    {% set start_date = nid_1|split("\n")[1]|split(",")[0] %}
    {% for c in durations if not_found %}
    {% if c[0] == nid %}
    {% set not_found = false %}
    {% else %}
    {% set offset = offset + c[1] %}
    {% endif %}
    {% endfor %}
    {{ date(session_start)|date_modify("+" ~ offset ~ "minutes")|date('Y-m-d H:i') }}

When previewing this view, use something like „1,2/3“ where 1 and 2 are the IDs of contributions of a session and 3 the session ID.

Content Type „Session“, Manage Display:
  • Field „Contributions“: Format „View“, View „Contributions in Session“, View Arguments: Field Value and Entity ID, multiple implode by „,“.
View „Sessions“ (optional)
  • Create a view „Sessions“, type HTML List of fields (or other, this is for real display).
  • List content of type „Session“
  • Add field „Contributions“, Formatter View, View „Contributions in Session“, View Arguments: Field Value and Entity ID, multiple implode by „,“.

Conclusion

Of course, most of these views (except of the Data Exports) have additional fields, because sessions have names, contributions have titles and presenters. And the contribution lists also add the end date. I hope, you can figure out how to add this yourself.

Leave a Reply

You must be logged in to post a comment.