2

I'm trying to write a general use plugin for a calculation fieldtype that evaluates a twig expression (defined in the field settings) to display additional information about the current record in the CP (i.e. price * qty = amount). So far, everything is working perfectly — the generic expression 1 + 1 is evaluated and the result is properly displayed in the field.

The problem is, I don't actually know how to reference the current entry in the CP from within the CP via twig, to get access to the entry field values. Looking for something like {{ entry.price }} * {{ entry.qty }}, but don't know what to use instead of entry. Does anyone know a twig expression that I can use?

2

As far as I know. The variable {{ entry }} is not available to custom field plugins through inheritance and must be explicitly set and returned as a variable in the getInputHtml method within the class definition file.

See this question for details on how to set this variable and things to be aware of.

1

A bit hard to tell what's going on without any code examples, but if you're just looking for a way to grab an entry from your plugin, and you already have the ID of the entry, then EntriesService has a getEntryById() method available via craft()->entries->getEntryById() that your plugin can call.

  • Thank you, but not exactly what I'm looking for. – Douglas McDonald Sep 15 '14 at 18:10
  • The plugin is designed to just evaluate a twig expression, and output the result into the field. Within the plugin settings you can define the twig expression to be evaluated. Something like {{ entry.unitPrice * entry.quantity }}. The problem is {{entry}} does not seem to work in this context. I'm looking for a way to reference the current entry within the admin panel. I don't know if there is already a variable defined for this, or if I should create a custom 'entry' or 'id' variable in the plugin, or if I should parse the uri within the twig expression to retrieve the id. – Douglas McDonald Sep 15 '14 at 18:27

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.