2

I have a front end form that saves an entry with a relation field based on an ID.

<input type="hidden" name="items[{{ index }}][finish]" value="{{ finish.id }}">

When the entry is saved this ID saves the field with the correct category relation.

I then have a plugin which triggers on entry save that sends an email and attaches the $entry.

craft()->on( 'entries.saveEntry', function( Event $event ){
    $entry = $event->params['entry'];
    if( !$event->params['isNewEntry'] || $entry->section->handle != 'sampleOrders' )
    {
        return;
    }
    //die(Craft::dump($entry->samplesOrder[0]->finish->find()));
    $email = new EmailModel();
    $email->toEmail = 'luke@ten4design.co.uk';
    $email->subject = 'Order Received';
    $email->body = '';
    $email->htmlBody = craft()->templates->render( 'pentagonemailservice/_emails/order', array(
        'entry' => $entry
    ) );
    try{
        craft()->email->sendEmail( $email );
    }
    catch( Exception $e ){
        Craft::log( 'Could not send email.', LogLevel::Error );
    }
} );

For some reason in my email template I cannot access the finish field with the following code. The other fields print correctly. The finish field is a category relation.

{% for item in entry.samplesOrder %}
    {% set appr = craft.entries.section( 'appearances' ).apprId( item.appearance ).first() %}

    <h3 class="appearance__name h beta"><a class="a" href="{{ appr.url }}">{{ appr.apprId }} - {{ appr.displayName }} - {{ appr.pentagonRange[0].title }}</a></h3>

    <h3 class="h h6 caps--spaced m-b--05">Material</h3>
    <p class="appearance__material h delta basket-bits">{{ appr.material }}</p>

    <h3 class="h h6 caps--spaced m-b--05">Finish</h3>
    {#<p class="basket-bits">{{ finish.title }}</p>#}

    <h3 class="h h6 caps--spaced m-b--05">Quantity</h3>
    <p class="basket-bits">{{ item.quantity }}</p>
{% endfor %}

How do I access the finish field in my email template?

2

Related elements, such as categories, are stored by Craft as arrays, even if there is only a single element to be stored.

In your entry form, you don't appear to be storing an array, but only the id of the selected category:

<input type="hidden" name="items[{{ index }}][finish]" value="{{ finish.id }}">

This seems to be borne out by what you see when you dump the item variable in your email template. item[finish] should be an array, even if only a single category is assigned to it.

Try using the following in your entry form to see if that helps, which should store the category id as an array:

<input type="hidden" name="items[{{ index }}][finish][]" value="{{ finish.id }}">

You should then be able to access the correct value for the category title using the code in @carlcs answer.

1

I don't think it has something to do with your event listener, and you just access the related elements not quite right. Try to get them like so:

{% set finish = item.finish.first() %}
{% if finish %}
    <h3 class="h h6 caps--spaced m-b--05">Finish</h3>
    <p class="basket-bits">{{ finish.title }}</p>
{% endif %}

or use a relatedTo query to get the finishes form the field.

4
  • Using {{ entry.finish.first().title }} gave me: Impossible to access an attribute ("title") on a null variable Tried using {{ item.finish }} and it gives me the following: Object of class Craft\ElementCriteriaModel could not be converted to string But using {{ item.finish[0] }} gives me Key "0" in object with ArrayAccess of class "Craft\ElementCriteriaModel" does not exist – Luke Pearce Dec 10 '15 at 13:44
  • Yep, sorry. It's the item variable, not entry. Updated the answer. – carlcs Dec 10 '15 at 15:00
  • item.finish.first() returns NULL. – Luke Pearce Dec 10 '15 at 15:13
  • If I dump item I can see the following : ["samplesOrder"]=> array(1) { ["new1"]=> array(3) { ["type"]=> string(5) "order" ["enabled"]=> string(1) "1" ["fields"]=> array(3) { ["appearance"]=> string(7) "SAN0004" ["finish"]=> string(3) "514" ["quantity"]=> string(1) "1" } }}} – Luke Pearce Dec 10 '15 at 15:18

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.