0

I have a paragraph which is essentially a date range, however, the individual dates can have prefixes, suffixes, can be in differing formats, I have a dropdown to select the format and dates need to be displayed differently depending on whether they span within a decade, over decades or over century boundaries. The date values have been split out into a daterange field so we can search / filter by date, however, within the parent paragraph, I'm wanting to return just the processed date instead of the list of fields.

I can do the IF this RETURN xyz to get the date fields into the required formatted string, however, I'm struggling to find how I can build this into a plugin and attach to my desired paragraph(s) / nodes.

It's fairly simple to do this within the theme templates, however, I'm needing to do this at a field / node level, not a twig template as the data will be pulled both as a traditional web page, but will also need to be pulled out via XML export, probably within various views and so on, so don't want to be doing the presentation transformation in multiple places. I need it to be independent of the site theme files.

This is in Drupal 8.8

Hope this makes sense.

1 Answer 1

2

If you wish to have absolute control on the display of your date range, I'd recommend you to create a module with a new FieldFormatter to expand one of the DateRange fieldformatters (the original files are in code/modules/datetime_range/src/Plugin/Field/FieldFormatter).

By overriding the viewElements() function, you'll be able to set the desired display(s) for your date range from one place and independent from the site theme files.

Once your new fieldformatter file is created, go to your paragraph "manage display" page and modify the display of your date range field to your new fieldformatter.

Hope this helps.

4
  • Given your use-case (views, exports,...), I'd expand this answer even further, and create a custom field type too, instead of the Paragraph entity. That field type should be extended from core's DateRange field, but add additional columns corresponding to the current paragraph fields.
    – Hudri
    Jan 9, 2020 at 13:56
  • I was wondering about the custom field, seems probably a little more flexible in my use case and encapsulates all my customization in a single package. How will that work with searching? In an ideal world I'd like to be able to take a search date and then determine whether this date is between the field's start & end dates, can this be built into the custom module as well? Jan 9, 2020 at 14:47
  • Well, if your custom field extends the DateRangeItem field, then you will have access to all properties of the latter, including 'start_date' and 'end_date'. For instance, you would be able to use these properties as filters in a view (I guess it is what you are looking for when talking about determining if a search date is between start and end dates.
    – misterdidi
    Jan 10, 2020 at 8:21
  • Thanks, this seems to be playing OK, however, I'm wanting to take it a little further. Currently the custom fields sit in a paragraph and I can drag and drop the order of the individual fields within the paragraph and also drag and drop the order of the paragraphs (the individual paragraphs act as a wrapper for groups of values), is it possible to pop the grouped fields directly into a content type and bypass the paragraphs? I can put the field into the content type, but can't see how I can group these without paragraphs or field_collection (which says to be depreciated in favor of paragraphs) Jan 21, 2020 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.