Creating Article Custom Fields - Part 2

Views: 162

Once you become comfortable adding article custom fields anywhere that you need them, you will discover a whole new world of possibilities and be able to provide more value to each of your projects.

Article Custom Fields

Including custom fields anywhere in your template that uses the 'Article Object' programmatically can be accomplished using the following steps:

    1. Add the following line to the top of the php file you want to the custom field to display on:
      use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
    2. Depending on where the article object is being called (if in module or category page it would be inside a 'foreach' array, if in an article page, anywhere in the top php code would work):
      $jcFields = FieldsHelper::getFields('com_content.article', $article, true);
      foreach($jcFields as $jcfield)
      {
      $jcFields[$jcfield->name] = $jcfield;
      }
    3. Add the custom fields you wish to include after the code in Step 2:
      $mycustomfield = $jcFields['mycustomfield-alias']->rawvalue;

Breaking down the following line from Step 2:
$jcFields = FieldsHelper::getFields('com_content.article', $article, true);

  1. 'com_content.article': This tells the code to look for custom fields related to the 'Article Object'
  2. '$article': is the 'Article Object' that the custom fields are going to be pulled from
  3. 'true':indicates whether to display all the field data or (false) just the raw data - changing to false will shave a little time off the page load (especially if you are using a lot of custom fields)

Breaking down the following line from Step 3:
$mycustomfield = $jcFields['mycustomfield-alias']->rawvalue;

  1. '$mycustomfield': This can be whatever unique name you prefer to use to name your custom field variable (as long as it starts with '$')
  2. 'mycustomfield-alias': Alias name of the article custom field.
  3. 'rawvalue':indicates we are using the "raw" value of the custom field as opposed to the $mycustomfield = $jcFields['mycustomfield-alias']->value; That displays the default layout display of the custom field.
Finding the 'Object'

An 'Object' can be best described as a collection of data for a specific item. In the code snippets about, we are trying to find the 'Article Object'. This can vary depending on the page/module type.

Article Object Cheat Sheet

  • Article Category Page - $article (inside the 'foreach' statement)
    $jcFields = FieldsHelper::getFields('com_content.article', $article, true);
  • Single Article Page - $this->item
    $jcFields = FieldsHelper::getFields('com_content.article', $this->item, true);
  • Article Category Module - $item (inside the 'foreach' statement)
    $jcFields = FieldsHelper::getFields('com_content.article', $item, true);

Article Custom Field Example

The following example will add add article custom fields to an 'Articles - Category' module using a layout override:

  1. Create a 'Text Type' custom field titled 'Tester 1' with the alias 'tester-1' and add some text to the field for the selected article that will display in the module.
  2. Create a new layout override for the module:
    1. 'System > Site Templates > [Your Template] > Create Overrides'
    2. Under 'Modules' click 'mod_articles_category'
    3. Select the 'Editor' tab on the same screen then click the html folder
    4. Click the folder 'mod_articles_category'
    5. Click the 'default.php' file then click 'Rename File' at the top of the page and rename to 'tester.php'
    6. Repeat for theĀ 'default_items.php' file renaming to 'tester_items.php'
  3. Open theĀ 'tester_items.php' file around line 16 add:
    use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
  4. Around line 18 replace:
    <?php foreach ($items as $item) : ?>
    With:
    <?php foreach ($items as $item) :
    $jcFields = FieldsHelper::getFields('com_content.article', $item, false);
    foreach($jcFields as $jcfield)
    {
    $jcFields[$jcfield->name] = $jcfield;
    }
    $tester1 = $jcFields['tester-1']->rawvalue; ?>
  5. Look for <?php if ($item->displayDate) : ?> (originally around line 47) and on an empty line above it add:
    <?php echo $tester1;?>
Using echo

At the most basic level displaying items in PHP is as simple as a basic 'echo' statement such as <?php echo $tester1;?> where the $tester1 variable represents $jcFields['tester-1']->rawvalue;

We could have also accomplished the same by calling the full variable like:<?php echo $jcFields['tester-1']->rawvalue;?>