4

I'm trying to code up a Matrix field for a homepage slider, and it contains a single 'Slide' block type. It has fields for Image (Assets), Heading (Plain Text), Sub-Heading (Plain Text), and Slide Link (Entries). I've got everything working, except for the Slide Link field (where I want to output the entry URI for the entry that was selected in that field).

Whenever I add {{ block.slideLink }} to my template, I get the following error:

Object of class Craft\ElementCriteriaModel could not be converted to string

So I'm obviously not meant to output a link to an entry (I only have one link being selected in the field) in this way, so can someone help me out with the syntax that I need please? There weren't any Entries examples in the Matrix Field docs that I could copy from, and I couldn't figure out how to adapt the Entries Field examples to fit in with a Matrix field.

Thanks in advance for any help, and here's my code. The incorrect {{ block.slideLink }} code is on line 11:

{% extends "_layout" %}

{% block content %}

  <div class="grid-100 flexslider">
    <ul class="slides">

      {% for block in entry.homepageSlideshow %}

        <li>
          <a href="{{ block.slideLink }}">

            {% set largeImage = {
              mode: 'crop',
              width: 1180,
              height: 450,
              quality: 80
            } %}

            {% set image = block.image.first() %}
            {% if image %}
              <img src="{{ image.getUrl(largeImage) }}" alt="{{ image.title }}">
            {% endif %}

            <div class="flex-caption grid-40">
              <h2>{{ block.heading }}</h2>
              <p style="font-weight: bold;">{{ block.subHeading }}</p>
            </div><!-- flex-caption -->

          </a>
        </li>
      {% endfor %}
    </ul>
  </div><!-- grid-100 -->

{% endblock %}
13

You get this error because {{ block.slideLink }} returns an array of EntryModels (even if you set your Entries field up to only allow 1 entry). So you need to add the first() method to get the first (and only) EntryModel that you are interested in:

{% set entry = block.slideLink.first() %}

Access the url property to return the URL of that entry:

{% if entry %}
    {{ entry.url }}
{% endif %}
  • I had the same error when trying to access and asset for an entry. After adding the first() method everything worked as expected. Thanks @carlcs. – Justin K May 1 '16 at 20:47
7

All related fields (assets, entries, categories, tags, etc) are designed to store more than one item, and thus always return an "array-like" object (although not technically an array), even if you have the limit set to 1. Thus you first need to either loop through the items or grab the first item using first or [0]. In addition, each item is an object model that has various object properties, that you will need to retrieve using dot syntax. So to get your url you would would want something like:

{% for block in entry.homepageSlideshow %}
    <a href="{{ block.slideLink.first.url }}">
    ...
{% endfor %}

or

{% for block in entry.homepageSlideshow %}
    <a href="{{ block.slideLink[0].url }}">
    ...
{% endfor %}

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.