2

From my plugin, I know I can use ...

craft()->templates->renderObjectTemplate($string, $entry)

... to parse, for example, { belongsTo.first.fullName } to the full name of the first user in the belongsTo field.

Now, I need an easy way to parse { belongsTo.find } into an array with all users related to this belongsTo field. Is there an easy way to do this, or should I let my plugin do the work?

Background information

My custom notification plugin has a settings field where I can set the 'receivers' of a notification. If an entry has a user field called 'belongsTo', and the setting is { belongsTo.find }, I need all the users related to this field, so I can further process the notification.

2

You should be able to use the full Twig tag for what you want to do. Perhaps something like the following (which assumes you have an email field you are trying to output for each user and want to separate your emails by a comma):

{% for person in object.belongsTo %}{{ person.email }}{% if not loop.last %},{% endif %}{% endfor %}

renderObjectTemplate should parse both the abbreviated syntax with a single curly brace or complete Twig tags. To take a close look, the abbreviated syntax:

{ belongsTo.first.fullName }

Is being interpreted by Craft as the following:

{{ object.belongsTo.first.fullName }}
  • Ben, this would be useful if I needed fullName, or title. However, what I need is an array with UserModels, to use further down the road ... – Paul Sep 9 '14 at 18:02
  • Not sure I understand the question then @PaulVerheul. I was assuming the belongsTo field was returning userModels and you were preparing your data to output in your template. Can you explain why you are using renderObjectTemplate and still trying to access the variables it's parsing down the road? renderObjectTemplate renders Twig code, so it can output a Twig tag which is already an array, but it's not going to give you an array back to use later. Is the array you want already available in your $entry variable? – Ben Parizek Sep 9 '14 at 22:10

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.