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?