Metaobject Collection Field Output

I have created a Metaobject called brands. It has a title, description, image and a collection reference. There is a screenshot of the Metaobject below.

I have included the current code I’m using below. Essentially I’m trying to output the collection link for the selected collection in the Metaobject entry to use for the a href. However all I seem to be able to output is the collection ID using the code I have or just gid://shopify/Collection/628469498189 for example using {{ brand.collection }}.

I need to be able to output the collection handle for the selected collection, not the id. Is this possible? Nobody in Shopify support seems to be able to help.

<section class="brands-list">
  <div class="brands-grid">
    {% for brand in shop.metaobjects.brands.values %}
      <div class="brand-item">
        {% if brand.collection %}
          {% assign collection_handle = brand.collection | split: '/' | last %}
          <a href="/collections/{{ collection_handle }}" class="brand-link">
        {% endif %}
          <img 
            src="{{ brand.logo | image_url: width: 400 }}" 
            alt="{{ brand.title }}" 
            class="brand-logo">
        {% if brand.collection %}
          </a>
        {% endif %}
        <h3 class="brand-title">{{ brand.title }}</h3>
      </div>
    {% endfor %}
  </div>
</section>

Many Thanks,

Roo

To get what you need you should use .value – this way you will get collection object from the collection field.

{{ brand.name }} 

{{ brand.collection.value.url }} 

produces:

My brand
/collections/all

Technically, you should also use brand.name.value, but for text line system stores just the text. However for collection it stores the id.

Using .value allows you to fetch corresponding collection object and then use it’s properties.

Legend Tim, thanks for clearing that up for me. Many Thanks!