Advertisement

How to conditionally display custom fields in template overrides

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
mike_hermary
Joomla! Intern
Joomla! Intern
Posts: 82
Joined: Fri Aug 20, 2010 3:04 am
Location: Canada
Contact:

How to conditionally display custom fields in template overrides

Post by mike_hermary » Tue Nov 21, 2023 8:37 pm

I am using the following code to pull in some custom fields into an override of com_content > category > blog_item.php:

Code: Select all

$video = $this->item->jcfields[4]->value;
$media = $this->item->jcfields[8]->value;
Further down in the file, I have added the following blocks of code to perform some conditionals:

Code: Select all

<?php if(isset($media) && !empty($media)) : ?>
    <!-- Media custom field output -->
    <div class="media-block">
      <?php echo $media; ?>
    </div>
    <!-- Media custom field output -->
<?php endif; ?>
For some reason the if conditionals are not working as expected. The Media custom field output block is always being displayed, even if the custom field has no defined value in the database.

Is this an issue with the implementation of the custom fields in the override or something else?

In Joomla 3, I was using the following for adding custom fields to com_content template overrides:

Code: Select all

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$context = 'com_content.article';
$article = $this->item;
$fields = FieldsHelper::getFields($context, $article, true);

foreach($fields as $field) {
  $fields[$field->name] = $field;
}

<!-- Media custom field output -->
  <?php if(isset($fields['media']->value) && !empty($fields['media']->value)): ?>
    <?php $class = $fields['media']->params->get('render_class'); ?>
    <?php echo $fields['media']->value; ?>
  <?php endif; ?>
<!-- Media custom field output -->
I prefer the shortened version for Joomla 4, and would like to keep using it if possible.

Cheers,

Advertisement
User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17788
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: How to conditionally display custom fields in template overrides

Post by toivo » Sun Nov 26, 2023 9:25 am

The following code ignores the CSS class but works otherwise:

Code: Select all

use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

$customFields 	= FieldsHelper::getFields('com_content.article', $this->item, true);
$values 	= array_column($customFields, 'value', 'name');
$media 		= $values['media'];
if (isset($media) && !empty($media)) {
	echo '<p>media field = ' . $media . '</p>';
} else {
	echo '<p>media field not set or empty</p>';
}
Toivo Talikka, Global Moderator

alghoshal
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Fri Sep 27, 2024 1:37 pm

Re: How to conditionally display custom fields in template overrides

Post by alghoshal » Fri Sep 27, 2024 1:53 pm

Similar issue for custom text field on J4 article template override (com_content > article > default.php).
- The isset() conditional check alone doesn't seem to work.
- An additional check for "strlen() > 0" gets it working.

Code:

Code: Select all

<?php if (isset($this->item->jcfields[1]) && strlen($this->item->jcfields[1]->value) > 0) : ?>
 <p> Custom field value set to: <?php echo $this->item->jcfields[1]->value; ?></p>
<?php endif; ?>

Advertisement

Post Reply

Return to “Joomla! 4.x Coding”