Avoid Code Duplication Caused by ExpressionEngine (v2.6.1) Not Letting You Reuse Names in Channel Field Sets

I developed a content template in a CMS called ExpressionEngine. Everything seemed simple enough until I was asked to incorporate a few extra fields that were commonly used in other content templates. Since the website was using an outdated version of ExpressionEngine that didn’t let you use the same field name in multiple content templates, I ended up duplicating a lot of code that's used to display these common fields. Well, it turns out that there is a better way.

Background

It should be mentioned that the information in this article is based on a really old version (2.6.1) of ExpressionEngine. Shortly after mentioning this post on Twitter, the support team from ExpressionEngine reported that the issue discussed in this post has been fixed in newer versions of their Content Management System (CMS). Unfortunately, I didn't have the level of access required to upgrade the CMS.

The primary goal for the project was to create a new content template. A content template is basically an online form for the website manager to use to build a specific type of web page. The new type of page I wanted to help the website manager create was a faculty listing page.

The website already had a content template for building the individual faculty profiles. So all of the information needed for the faculty listing page, such as the names and photos for the faculty members, was already available. By default, ExpressionEngine already includes a field for the page title. So the only thing I needed to add to the content template was a field for the website manager to choose which faculty members to display on the listing page.

The content template was created using the Channels and the Channel Fields modules built into ExpressionEngine. For a more detailed walkthrough of these features, there's a video series available through YouTube by DaBrook; you can start with "Channels and Custom Fields in ExpressionEngine."

The Problem

The completed content template was working well, until I was asked to incorporate a field that's common to our other content templates. Adding fields to a content template is fairly simple. However, I wasn’t aware that you can’t use the same field name for two different content templates. So in one template the field is named "pages_asides". My new content template has the field named as "fac_list_asides".

Since the field needs its own unique name in the older version of ExpressionEngine (v2.6.1), the code used to display the field needed to be duplicated to handle both content templates. For example, the following block of code would run whenever a faculty listing page was displayed:

{!-- DISPLAY ASIDES BASED ON PAGE CHANNEL --}
{if "{embed:channel}" == "Faculty Listing"}
     {exp:channel:entries channel="faculty_listing" entry_id="{embed:entry_id}"}
          <!-- NO EDIT BUTTON -->
          {fac_list_asides}
               {if fac_list_asides:count == 1}
                    <section class="asides">
               {/if}
               <article>
               {if fac_list_asides:aside_code == ""}
                    {fac_list_asides:aside_body}
               {if:else}
                    {fac_list_asides:aside_code}
               {/if}
               </article>
               {if fac_list_asides:count == fac_list_asides:total_results}
                    </section>
               {/if}
          {/fac_list_asides}
     {/exp:channel:entries}

Then the following code block would handle cases where the field is named "pages_asides":

{if:else}
     {exp:channel:entries channel="pages" entry_id="{embed:entry_id}"}
          <!-- NO EDIT BUTTON -->
          {pages_asides}
               {if pages_asides:count == 1}
                    <section class="asides">
               {/if}
               <article>
               {if pages_asides:aside_code == ""}
                    {pages_asides:aside_body}
               {if:else}
                    {pages_asides:aside_code}
               {/if}
               </article>
               {if pages_asides:count == pages_asides:total_results}
                    </section>
               {/if}
          {/pages_asides}
     {/exp:channel:entries}
{/if}

To make matters worse, my next project was going to involve added the asides field to a third content template. That means I would need a third block of code to handle that field.

Alternative Solution

As mentioned earlier, content templates are created in ExpressionEngine using Channels. Each Channel is linked to a set of Channel Fields. Most pages on the website I'm working on use a Channel named "pages" that is linked to a set of Channel Fields called "pages_fields". Since many of the fields defined in "pages_fields" would be useful for my new faculty listing pages, I decided to link my "faculty_listing" Channel to the Channel Fields named "pages_fields". Then I re-created the field for the website manager to select the faculty members they want to display on their faculty listing page.

Since both the "pages" and the "faculty_listing" Channels use the same set of fields, I needed to modify which fields appear for the website manager when they are creating a normal page versus a faculty listing page. To hide the new faculty selection field for entries using the "pages" Channel, for example, I went into edit mode for a page using that Channel. Then clicked the "show toolbar" option which opens a side menu for hiding/showing fields for the Channel being viewed (see Figure 1).

ExpressionEngine screenshot displaying the "show toolbar" option
Figure 1. Click Show Toolbar

From there I clicked the eye next to the new "Faculty Pages" field so that it's closed (see Figure 2). This prevents the field from appearing for the "pages" Channel. Then under the "Publish Layout" section, I chose all the user groups since everyone using the "pages" Channel should get the same field list (see Figure 3). Then I repeated the steps for a page using the "faculty_listing" Channel. Of course, I want the "Faculty Pages" field to be displayed in that Channel.

ExpressionEngine screenshot showing the icon to show/hide a Channel Field
Figure 2. Show/Hide Fields
ExpressionEngine screenshot showing the Publish Layout options
Figure 3. Indicate Which Users See the Changes

Now that the "pages" and "faculty_listing" Channels use the same field set, the code used to display the common fields can be simplified. Here is the streamlined code which works for both channels:

{exp:channel:entries channel="pages|faculty_listing" entry_id="{embed:entry_id}"}
     <!-- NO EDIT BUTTON -->
     {pages_asides}
          {if pages_asides:count == 1}
               <section class="asides">
          {/if}
          <article>
          {if pages_asides:aside_code == ""}
               {pages_asides:aside_body}
          {if:else}
               {pages_asides:aside_code}
          {/if}
          </article>
          {if pages_asides:count == pages_asides:total_results}
               </section>
          {/if}
     {/pages_asides}
{/exp:channel:entries}

0 Comments

There are currently no comments.

Leave a Comment