Inside the CP, is it possible to pull the ID for the current field layout being rendered (both in the Field Layout Designer and in element editing forms)?
A bit of context:
I'm writing a plugin which adds functionality to the built-in Field Layout Designer. The extra functionality basically allow admins to add metadata when they are editing field layouts. This metadata is stored in a custom table, using the field layout's ID as FK. The idea is that whenever a field layout is rendered (either inside the FLD, or when an element is edited/created) the plugin should pull the metadata for the current field layout.
Modifying the FLD UI is easy – when the plugin loads, I basically check the DOM (with jQuery) for a FLD container. If a container is found, the plugin loads up a script.
Saving the metadata is also easy; the plugin hooks into fields.saveFieldLayout
like this:
craft()->on('fields.saveFieldLayout', function (Event $e) {
$myPluginModel = new MyPlugin_MyPluginModel();
$myPluginModel->fieldLayoutId = $e->params['fieldLayout']->id;
$myPluginModel->metadata = craft()->request->getPost('myPlugin.metadata');
craft()->myPlugin->saveStuff($myPluginModel);
});
The hard part is querying for the relevant metadata whenever a field layout is rendered, because I can't find a way to pull the currently rendered field layout's ID.
Here's ideas I've explored/solutions that doesn't work:
Neither the FLD nor the element editing forms expose their field layout's ID to the the DOM.
I've looked at the HttpRequestService to see if the field layout ID (or any other data I could use to query for the field layout ID) could be pulled from the request.
I've tried accessing the current controller class via
craft()->controller
– thinking if I could get to the current element type I might be able to pull the field layout from its model, butcraft()->controller
is alwaysNULL
.I've attempted to pull the global Twig context (via
craft()->templates->twig
) to look for thefieldLayout
variable.Ideally, I wouldn't want to pull the relevant metadata client side (i.e. with AJAX), but I did also look at the global Craft JS object to look for clues, but found nothing.
Parsing the request path and storing the ID of the entry type, asset source, category group etc. along with the field layout ID would work. I'd like to avoid this, because it makes it impossible to support custom element types without hard coding stuff.
If it isn't possible – by any means – to pull the current field layout ID, the alternative approach is to somehow figure out which element type is being edited, then pull that element type's model and finally get the field layout model/ID from there. Ideally, I'd like to do this without having to hard code attribute names (i.e. entryTypeId
, categoryGroupId
etc), because I want this to work for any custom element type, too. Any suggestions in this regard is very welcome, as well.