Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add custom fields to core modules in Joomla! 3
defined('_JEXEC') or die;
<div class="latestnews">
<ul class="list-group list-group-flush">
<?php foreach ($list as $item) : $image = json_decode($item->images); ?>
// most important part
// context:
// for articles: com_content.article
// for users: com_users.user
// etc...
$jcfields = FieldsHelper::getFields('com_content.article', $item, true);
// bonus: custom fields by name
// usage: $fields_by_name['name-of-field']->value
$fields_by_name = \Joomla\Utilities\ArrayHelper::pivot($jcfields, 'name');
<li itemscope itemtype="" class="list-group-item px-0" style="background-color:<?php echo $fields_by_name['article-favourite-color']->value; ?>">
<?php if($image->image_intro != "") : ?>
<a class="article-media" href="<?php echo $item->link; ?>" itemprop="url">
<img class="card-img-top pb-2" src="<?php echo JURI::root().$image->image_intro; ?>" alt="<?php echo htmlspecialchars($image->image_fulltext_alt); ?>">
<?php endif; ?>
<a class="article-title" href="<?php echo $item->link; ?>" itemprop="url">
<span itemprop="name">
<?php echo $item->title; ?>
<?php endforeach; ?>
Copy link

woluweb commented Nov 26, 2019

	// custom fields by ID
	// usage: $fields_by_id[X]->value, X being the ID of the custom field
	$fields_by_id = \Joomla\Utilities\ArrayHelper::pivot($jcfields, 'id');

Example of real use for a custom field having ID 28 and Name 'institution'

My custom field with $fields_by_id: value; ?>

My custom field with $fields_by_name: value; ?>

Copy link

Thanks but getting error after putting code inside module ...

Warning : Undefined variable $list and Warning: foreach() argument must be of type array|object, null given

Kindly Help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment