Plugin 1

I have a plugin that is a custom fieldtype. It works fine, and has a getInputHtml() method that behaves as expected.

Plugin 2

Now, I have a second plugin, where I want to output the HTML from the Plugin 1 getInputHtml() method.

I have the FieldModel:

$field = $field->getField();

I have the Plugin 1 fieldtype object

$fieldtype = craft()->fields->populateFieldType($field, $element);

And I call the getInputHtml() method:

$fieldtype->getInputHtml($field->handle, $value);

If I place the necessary files at the base of Plugin 2 plugin2/templates/plugin1/_field/input everything works fine. But I can't seem to access the input HTML template in the context of Plugin 1: plugin1/templates/_field/input

I've tried to update the Template Path in various ways before calling $fieldtype->getInputHtml():

craft()->path->setTemplatesPath(craft()->path->getPluginsPath() . 'plugin1/');

Everything I have tried has resulted in a template error:

Unable to find the template "plugin1/_field/input".

Any ideas on what I'm doing wrong?

  • is the call to Plugin 2 happening in a CP or action request? – Ben Croker Jul 7 '14 at 11:17
  • The call from Plugin 2 to Plugin 1 is happening on the front-end (a variable passes of the call to the service layer). At this point, this is a largely academic pursuit about paths. This is really not a good idea because getInputHtml() is designed to work on the CP side only and comes (potentially) with a lot of extra baggage for anything involving the front-end. – Ben Parizek Jul 8 '14 at 7:02

The problem as I see it is that the call is happening on the front-end. Because of this, the template renderer is not looking for the template at plugin1/templates/_field/input as it normally would, but at plugin1/_field/input, which explains the error message.

If Plugin 1 is your own plugin then you should be able to include the templates part of the path in the render call, for example:

return craft()->templates->render('templates/_field/input');

I haven't tested this but I think it should work.

Alternatively, and I would recommend this, since this is a front-end call, you could create your own function getFrontEndInputHtml() to return the html appropriately.

| improve this answer | |
  • My initial goal was to be able to target any plugin this way but I think your answer sums up what is possible here. I can modify Plugin 1's render path or add an additional function to get the results I want for that plugin but that doesn't help me address any plugin I don't have control over. For Field Types in other plugins, it appears I'll need to explore alternative routes to communicate with them or use the template data they use. – Ben Parizek Jul 13 '14 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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