Accentuate Custom Fields
  • Welcome!
  • Introduction
    • Getting started with ACF
    • Field scopes
    • How to show fields in your storefront
  • Dashboard
    • Reference Manager
      • Creating a new reference within Reference Manager
    • Filter & Group
      • Filter editor
      • Filter usage
    • Activity log
    • App settings
  • Field Definitions
    • Fields & Sections
    • Deciding on a field type
    • ACF Field types
      • Text
      • Markdown text
      • HTML
      • Checkbox
      • Selection
      • Tags
      • Number
      • Date
      • Color
      • Media v1 (legacy)
      • Media v2
      • Custom Object (JSON)
      • Multi-language Text
      • Reference fields
      • References to Global fields
    • Shopify Field types
      • Shopify » Single line text
      • Shopify » Multi-line text
      • Shopify » Boolean
      • Shopify » Color
      • Shopify » Custom objects (JSON)
      • Shopify » URL
      • Shopify » Date
      • Shopify » Date and Time
      • Shopify » Integer
      • Shopify » Decimal
      • Shopify » Weight
      • Shopify » Volume
      • Shopify » Dimensions
      • Shopify » Reference fields
    • Automatic tagging
    • Large sets
    • Field contexts
    • Field context filters
    • Copy and paste fields
    • Change field name and type
    • Import existing fields
    • Linking multiple stores
  • The Editor
    • Using the editor
    • Promote fields to sidebar
    • Layouts
    • Hide from search
    • Repeatable fields
    • Version control
    • Update value on order creation
  • Bulk Import & Export
    • Export custom field values
    • Import custom field values
    • Metaobject export / import
  • METAOBJECTS
    • What are Metaobjects?
    • Metaobject Definitions
    • Metaobject instances / values
    • Displaying Metaobjects on your storefront
    • Metaobject export / import
  • THEME EXTENSIONS
    • SEO keywords
    • Sticky promo bar
    • Products promotion
  • Liquid Guides
    • Learning Liquid
    • Resize & crop images
    • Check for empty values
    • Access field definitions
    • Order notifications
    • Allow customers to change their field values
  • OTHER
    • Webhooks
    • API
      • Access to custom fields
      • Endpoints
  • Help
    • FAQ
      • Why are one of my products no longer showing in my reference field?
      • I have multiple products/variants with the same name
      • How do I make changes to my field values in bulk?
      • I added a new field definition but it is not showing in my storefront
      • How do I copy my field setup to another store?
      • My fields are still showing after I have deleted their field definition
      • Why are none of my fields showing in the editor?
      • My newly created object in Shopify is not available in ACF
      • Reference lists are empty?
      • Why am I seeing a "value cannot exceed 100,000 characters" error when saving?
      • Why can't I name my field "first", "last" or "size"?
    • Need help?
  • Product
    • Changelog
    • Feedback & Suggestions
Powered by GitBook
On this page
  • What is stored in the reference field?
  • Resolving field references
  • Resolving field references with indexes
  • Detecting the referenced field type
  • Resolving section references
  • Resolving section references with indexes (i.e. the non-sticky way)
  • Resolving section references with keys (i.e. the sticky way)
  1. Field Definitions
  2. ACF Field types

References to Global fields

PreviousReference fieldsNextShopify Field types

Last updated 10 months ago

Global fields references in ACF are meant as puzzle pieces for building your field definitions for a specific scope, making it easy to reference content that would otherwise be replicated across many products or collections, etc. For example. you have a set of features with an icon and a description common to your products defined as Global fields:

You can then use a global reference field to point to the section or any of the fields (and even specific occurrences within both), allowing for a single point of maintenance of the underlying data. So if a feature icon or description changes, you just update the global field and the change will be reflected on all your products instantly.

Tip: If a repeatable section contains a field of type Text, the content of the first in the sequence will be included after each occurrence's index number ("Organic" and "Vegan" in the example) for easier selection of the correct occurrence

What is stored in the reference field?

The underlying Metafield value when selecting global fields will always be a JSON object with a single .references property containing an iterable array of JSON objects with either a .field or a .section property (depending on what is being referenced) and optionally either an .index or .key property. The .key property is only used for repeatable section references and only when the global section being referenced has its "Use sticky references" setting enabled (see below). If just a single field or section is selected, the array will have just one entry. This allows for consistent use of Liquid's array filters such as first, last, where, etc. Example structure:

{ references: [
    { field: "field_name",
      section: "section_name",
      index: <number>,
      key: "internal_random_key"
    }]
}

You can query the .references array for either the .field or .section property being present to detect what is being referenced as well as the .index property to detect if a specific occurrence is being referenced.

Resolving field references

When one or more fields are being referenced, each field reference (an element in the .references array) will contain the field's name in the .field property and you can resolve the reference in Liquid like this:

{% assign reference_to = product.metafields.accentuate.features.references | first %}
{% assign referenced_global_field = shop.metafields.globals[reference_to.field] %}

<p>{{ referenced_global_field }}</p>

Or, if you have selected multiple fields references:

{% assign references_to = product.metafields.accentuate.features.references %}
{% for reference_to in references_to %}
  {% assign referenced_global_field = shop.metafields.globals[reference_to.field] %}
  <p>{{ referenced_global_field }}</p>
{% endfor %}

If you are referencing repeatable fields, please see the related article "Repeatable fields" for instructions about how to loop over these

Resolving field references with indexes

A global reference may point to specific occurrences of repeatable fields. When this is the case, the referenced index is stored in the .index property. This example covers a single field reference to a specific occurrence:

{% assign reference_to = product.metafields.accentuate.features.references | first %}
{% assign referenced_global_field = shop.metafields.globals[reference_to.field][reference_to.index] %}
<p>{{ referenced_global_field }}</p>

Detecting the referenced field type

If you need to check which type of field is being referenced, you can cross-reference the .field selection against the field definitions for the global scope, like this:

{% assign reference_to_type = shop.metafields.acf_settings.global.fields | where: "name", reference_to.field | map: "type" | first %}

<p>The type of field being referenced is '{{ reference_to_type }}'</p>

Resolving section references

If the global reference field is referencing a .section, you can go via the field definitions to find the section's fields (see related article below), like this:

{% assign reference_to = product.metafields.accentuate.features.references | first %}
{% assign referenced_fields = shop.metafields.acf_settings.global.fields | where: "section_name", reference_to.section %}

{% for referenced_field in referenced_fields %}
  {% assign referenced_global_field = shop.metafields.globals[referenced_field.name] %}
  <p>{{ referenced_field.label }}: {{ referenced_global_field }}</p> 
{% endfor %}

If you are referencing a repeatable section, please see the related article "Repeatable fields" for instructions about how to loop over these

Resolving section references with indexes (i.e. the non-sticky way)

Reference fields may point to specific occurrences of repeatable sections. When this is the case, the referenced index is stored in the .index property and is valid for accessing the relevant part of the section's fields. This example covers a single reference to a section's specific occurrence (like "Features #1" in the above selection) which then is resolved to the contained fields dynamically:

{% assign reference_to = product.metafields.accentuate.features.references | first %}
{% assign referenced_fields = shop.metafields.acf_settings.global.fields | where: "section_name", reference_to.section %}

{% for referenced_field in referenced_fields %}
  {% assign referenced_global_field = shop.metafields.globals[referenced_field.name][reference_to.index] %}
  <p>{{ referenced_global_field }}</p> 
{% endfor %}

Usually, though, you'll be selecting specific sections and just want to use the index of the section being referenced (like "Features #1" and "Features #2" in the above selection), so you can loop the reference field selections and code the fields directly in Liquid using the index property:

{% assign references_to = product.metafields.accentuate.features.references %}
{% for reference_to in references_to %}

  <p>{{ shop.metafields.globals.feature_title[reference_to.index] }}</p> 
  <p>{{ shop.metafields.globals.feature_description[reference_to.index] }}</p> 

{% endfor %}

Resolving section references with keys (i.e. the sticky way)

Same as above, reference fields may point to specific occurrences of repeatable sections but can optionally store a "sticky" reference to the selected occurrences' index. This happens automatically when the global section being referenced has its "Use sticky references" setting enabled. When this is the case, the referenced "index" is stored indirectly in the .key property and the correct index can then be determined dynamically in Liquid.

This is the recommended way to use section references because it ensures that the references are always up to date even if the occurrences within the global section are rearranged after selections have been stored in the reference field.

This example covers a single reference to a section's specific occurrence (like "Features #1" in the above selection) via a key. The actual occurrence index is then resolved in Liquid via the 'assign index =' statement:

{% assign reference_to = product.metafields.accentuate.features.references | first %}
{% assign index = shop.metafields.globals[reference_to.section] | where: "key", reference_to.key | map: 'index' | first %} 
  
<p>{{ shop.metafields.globals.feature_title[index] }}  
</p> 
<p>{{ shop.metafields.globals.feature_description[index] }}</p> 

If you get lost in field references and resolving to global fields, try outputting the involved fields using Liquid's json filter. This will often reveal any issues