Field Lookup

Search for and retrieve entries based on the fields being used by that entry (which are assigned by the section layout the entry is using), optionally defining which fields should be taken and populated for each entry found by the lookup using the take method. Perform a lookup by calling enact.field and defining a method chain of property/value pairs specifying the conditions under which to lookup entries.

Field properties which can be used in lookup comparisons:

  • id - The unique ID of the field.

  • name - The name of the field.

  • handle - The handle of the field.

  • type - The type of field.

  • instructions - The instructions of the field.

  • required - Whether the field is required (1 = required, 0 = not required)

  • fieldGroupId - The unique ID of the group the field is contained in.

  • groupName - The name of the field group the field is contained in.

  • groupHandle - The handle of the field group the field is contained in.

Call the field property you want to use as a condition, passing it a value. By default each value is compared to the property by being exactly equal (in SQL just property=value), but you can prefix your values with the below comparators to specify a different comparison (always include a space after the comparator):

  • > - Where property is greater than the value.

  • < - Where property is less than the value.

  • >= - Where property is greater than or equal to the value.

  • <= - Where property is less than or equal to the value.

  • ! - Where the property does not equal the value.

  • LIKE - Where the property contains the value.

(NOTICE: Field lookup queries are expensive and should be used sparingly)

Basic example

//get all the entries using the promotional video field

{% set entries = enact.field.handle('promoVideo').fetch %}

When you are done specifying your conditions, call the method fetch to return the array of matching entries, each represented as a Entry Wrapper. You may also use the first and last method to retrieve the first or last entry returned from the lookup. Both methods use the fetch method internally.

Loop the results

<p>{{ entries|length }} entries using the promo video</p>

<ul>

{% for e in entries %}

{{ e.getLinkHtml }}

<div class='date'>{{ e.createdOn }}</div>

{{ e.promoVideo.fullWidth.getVideoHtml }}

{% endfor %}

</ul>

Another example

//get all the entires that are using a color field from the field group Breaking News

{% set entries = enact.field.type('color').groupHandle('breakingNews').fetch %}
Take method

Use the take method to specify the fields (identified by their handles) that should be attached and populated on the entries returned by the lookup. By default all fields of each entry are attached and populated when doing lookups, use this method to override that (this is only warranted when you want to save system/database resources). The take method accepts as arguments an array of field handles, a comma delimited string of field handles, or multiple handles passed as individual arguments.

{% set blogs = enact.field.type('video').groupHandle('blogContent').field('body,promoVideo').fetch %}
Matrix method

Call the matrix method, passing a matrix field handle as the value, to find entries using a particular matrix field.

{% set entries = enact.field.groupHandle('artPresentation').matrix('artPieces').fetch %}
Search method

Use the search method to perform a LIKE comparison on the field name and handleproperties simultaneously.

{% set entries = enact.field.search('position').fetch %}
Order method

Use the order method to sort the result of the lookup, passing a property name and and optional sort type (ASC, DESC), by default ASC is used.

{% set entries = enact.field.search('position').order('name').fetch %}
Limit method

Limit the number of entries returned by the lookup

{% set entires = enact.field.type('video').limit(5).fetch %}
Page method

Use the page method in conjunction with the limit method to return a specific number of entries from a specific page of the results.

// will return entries 5-10

{% set entries = enact.field.type('dropdown').limit(5).page(2).fetch %}
Total method

Use the total method to force the lookup to return an integer value specifying the number of entries that matched the condition. The same can be accomplished using the twig length filter, however, this prevents the actual entries from being fetched and returned, saving database resources.

{% set totalEntries = enact.field.search('content').total.fetch %}