3

We are trying to find the best solution for handling repeated content within existing Fields in the CMS.

That is, there are bits of information which could be used nearly anywhere within existing fields in the CMS (e.g. inside a Plain Text field / Rich Text field etc) (things like the event booking link, booking deadline, event start date etc etc). The ideal solution would be that these bits of information are housed in one place in the CMS (probably will be a global), and can cleanly be pulled through to other areas of the CMS with a super user-friendly bit of syntax.

-

We've explored a few options, namely the following...

Reference Tags

As these are limited to Plain Text field types, these are not an option.

Globals

As per this SE question.

This would involve having to add include(template_from_string()) almost everywhere where a content is output from the CMS, which would be a pain and also the syntax might be a little confusing for the end user.

Create our own plugin

Whilst this might mean the syntax could be a bit more user friendly (e.g. something like [main-event-dates], we would still have to pass every field output to the plugin (i.e. craft.myPlugin.parseSyntax(entry.body)

Events

I'm just starting exploring Hooks and Events, and this looks like the best solution so far to me. From my understanding, the approach which would be most suitable for us would be something like this:

myPluginPlugin.php

function init() {
    craft()->on('elements.onPopulateElement', function(Event $event) {
        craft()->myPlugin_main->parseSyntax($event);
    });
}   

myPlugin_MainService.php

function parseSyntax($event) {
    // Parse the content of the fields, using something like the following
    $event->params['element']->getContent();
}

Considerations with using the 'Events' method

  • We would need to build in a mechanism of programatically clearing all template caches when one of those bits of central information is changed (as Craft will not be aware of which templates need their cache clearing)
  • Is there a risk that this will be super php intensive to essentially loop though each field for each bit entry model / matrix block model etc etc?

-

Does anyone know of a better way to achieve what we are trying to achieve? Or feel strongly any way about any of the above?

6
  • 3
    My suspicion is that you're grossly overthinking it. Craft excels at reusing content, it seem like you're trying to reinvent the wheel here. Let me ask... is this a website for a single, big event, or for a series of events?
    – Lindsey D
    Commented Jun 23, 2016 at 17:49
  • It seems like you are confused by "Hooks and Events" which is for internal CMS programing events... nothing to do with "Events CONTENT(Lectures, Sporting Events, Concerts, etc) I'd be looking at related entries craftcms.com/docs/relations. Where you relate an entry for an concert event to a another blog type entry -or- you pull entries for upcoming event by date to the front page template, etc
    – Moto_Nomad
    Commented Jun 27, 2016 at 22:07
  • @LindseyD : Hm, that was a suspicion but I'm not sure that we are to be honest. The website, which covers a whole organisation, exists to handle a great deal of information, both about the organisation, about the festival it runs (the main event), supplementary events, a blog, resources etc etc and the content is still growing (there will be an update over the next few weeks including a music section linked to the festival for example). For that reason, the client may well want to use information (e.g. about the festival) in multiple places within the CMS. Hope that gives a bit of context?
    – Mike Vaux
    Commented Jun 28, 2016 at 8:59
  • @Moto_Nomad : No, I'm not confusing them, I understand that they are not in anyway linked to an "event" like a sporting event / concert etc. I think I made this clear in my question (forgive me if I didn't), but the issue is not about relating entries, but smaller bits of information, for which there is not a prescribed location.
    – Mike Vaux
    Commented Jun 28, 2016 at 9:03
  • OK, So you want have site-wide type globals INSIDE rich text fields? Sorry, the questions's title completely threw me off.
    – Moto_Nomad
    Commented Jun 29, 2016 at 16:50

2 Answers 2

3

Forgive me but after re-reading everyone's input, I lean towards Moto_Nomad relationships suggestion, mostly because it will make life as easy as possible for the content-administrator.

In your latest comment you write:

...but the issue is not about relating entries, but smaller bits of information, for which there is not a prescribed location.

Q: Could you give us an example of a 'smaller bit of information', please?

That is, there are bits of information which could be used nearly anywhere within the CMS (things like the event booking link, booking deadline, event start date etc etc)

I'd suggest that the 'smaller bits of information' as well as the info you mention above, which are relevant to a Festival/Event should be stored within those (Event) Entries.

Especially if these 'bits of info' are updated over time i.e. from from one event to the next.

(I'll avoid going into an explanation of the Entries field type)

;)

The ideal solution would be that these bits of information are housed in one place in the CMS (probably will be a global), and can cleanly be pulled through to other areas of the CMS with a super user-friendly bit of syntax.

Check out this thread: Is it possible to use Twig in a Rich Text Field?

I realise the above is about the Front-End but thought it might be useful.

Let us know how you get on, Mike :)

2
  • Thanks for the suggestions Matt! Relationships. Ideally we're looking for a solution for which the information can be pulled into any field, so a dedicated 'relationship' field unfortunately wouldn't fulfil that. Smaller bits of information. Things you've highlighted in bold in your second quote and also things like booking deadlines, current number of delegates, the minimum birth date to for serving etc. Storing info in the main entries. This may well be where we end up if this more complex route is not realistic! Twig thread. That's referenced in my 'Globals' section.
    – Mike Vaux
    Commented Jun 28, 2016 at 14:20
  • Note to self, read AND click all links in a question :/
    – Matt P
    Commented Jun 28, 2016 at 14:37
3
Reference Tags

As these are limited to Plain Text field types, these are not an option.

Despite what the docs say, in my tests, Rich Text fields actually are parsed for reference tags. Not sure if/when this actually changed, but it seems to work now (Craft 2.6)

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.