Skip to content

Mapping custom fields to a common nomenclature

Mark Croxton edited this page Aug 20, 2013 · 2 revisions

When you have multiple channels with different field groups, but several duplicate fields. For example:

Channels:

  • products_bikes
  • products_skateboards
  • products_trikes

Fields:

  • bikes_summary
  • skateboards_summary
  • trikes_summary

Map the duplicate fields so that they can be referenced with a common name:

{!-- make a list of your channel prefixes --}
{exp:stash:set_list name="channels"}
    {stash:channel}products_bikes{/stash:channel}
    {stash:channel}products_skateboards{/stash:channel}
   {stash:channel}products_trikes{/stash:channel}
{/exp:stash:set_list}

{!-- make a list of your common field suffixes --}
{exp:stash:set_list name="fields"}
    {stash:field}summary{/stash:field}
    {stash:field}another_field{/stash:field}
{/exp:stash:set_list}

{!-- map channels and fields to generate tag pairs --}
{exp:stash:set name="mapped_fields" parse_tags="yes" parse_depth="2" type="snippet"}
   {exp:stash:get_list name="fields"}
      {stash:{field}}
         {exp:stash:get_list:nested name="channels"}
            {{channel}_{field}}
         {/exp:stash:get_list:nested}
      {/stash:{field}}
   {/exp:stash:get_list}
{/exp:stash:set}

{!-- capture the data --}
{exp:stash:set parse_tags="yes" type="snippet"}
   {exp:channel:entries
      channel='{exp:stash:get_list name="channels" backspace="1"}{channel}|{/exp:stash:get_list}'
  parse="inward"
   }

   {mapped_fields}

  {/exp:channel:entries}
{/exp:stash:set}

{!-- example of using the data in your view template (Stash embed or EE embed) --}
{if summary}{summary}{/if}

{!-- example of using the data in the *same* template you capture the data --}
{if summary}{exp:stash:get name="summary" type="snippet"}{/if}
Clone this wiki locally