Pulling the currently rendered field layout inside the CP 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()-&gt;on('fields.saveFieldLayout', function (Event $e) {
    $myPluginModel = new MyPlugin_MyPluginModel();
    $myPluginModel-&gt;fieldLayoutId = $e-&gt;params['fieldLayout']-&gt;id;
    $myPluginModel-&gt;metadata = craft()-&gt;request-&gt;getPost('myPlugin.metadata');
    craft()-&gt;myPlugin-&gt;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()-&gt;controller – thinking if I could get to the current element type I might be able to pull the field layout from its model, but craft()-&gt;controller is always NULL.
I've attempted to pull the global Twig context (via craft()-&gt;templates-&gt;twig) to look for the fieldLayout 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.
It looks like EntryTypeModels have a fieldLayoutId attribute. By inspecting the route params, we can get the entry type ID, and then create a model from that. Other than this, I haven't been able to find anything passed to the FLD.

    $params = craft()-&gt;urlManager-&gt;getRouteParams();
    $variables = $params['variables'];
    $matches = $variables['matches'];
    $entryTypeId = $matches['entryTypeId'];
    $entryType = EntryTypeRecord::model()-&gt;findById($entryTypeId);
    $fieldLayoutId = $entryType-&gt;fieldLayoutId;


Hopefully this helps!

Edit:

I also found that a fieldLayoutModel is passed to the fieldlayoutdesigner.html template when it is included. See the include in templates/settings/section/_entrytypes/edit.html as an example. Content from the FieldLayoutModel is then loaded. 

I know you said you would rather not load with ajax, but it is definitely better than nothing. I don't see any hook or event that gives you access to this information.

You could do something like 

var fieldLayoutId = '{{ fieldLayout.id|e('js') }}';


from within your included js file and then go pull your metadata via ajax.