How to display custom fields from an Article in a module?

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
nzrunner
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Sat Dec 22, 2007 1:21 am

How to display custom fields from an Article in a module?

Post by nzrunner » Sun Sep 03, 2017 2:50 pm

I am using custom fields on Users and Articles. I am enjoying the new found flexibility!

As part of the process, I am developing a module for a client that will display some custom fields.

I have used the following code in my module file to grab the value of a custom field from a user record: -

Code: Select all

JLoader::register(
    'FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'
);
$fields = FieldsHelper::getFields('com_users.user',  JFactory::getUser());

foreach ($fields as $f) {
    if ($f->name == "profile-image") {
        $image = $f->value;
    }
}
However, that doesn't work for an article. When I debug the fields array, everything is there except the value property.

I have had a read of the articles contained here https://docs.joomla.org/J3.x:Adding_cus ... /Overrides and it seems to suggest that the reason is because I have not called the onContentPrepare event.

My question is, how to I call, from within my module, the onContentPrepare event so that my custom fields array is populated with the value stored within the article?

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 25586
Joined: Mon Oct 27, 2008 9:27 pm
Location: Akershus, Norway

Re: How to display custom fields from an Article in a module?

Post by Per Yngve Berg » Sun Sep 03, 2017 3:18 pm


nzrunner
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Sat Dec 22, 2007 1:21 am

Re: How to display custom fields from an Article in a module?

Post by nzrunner » Sun Sep 03, 2017 11:40 pm

Thank you for your reply.

I am hoping someone has a real world code example of how I can apply that in my situation.

nzrunner
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Sat Dec 22, 2007 1:21 am

Re: How to display custom fields from an Article in a module?

Post by nzrunner » Mon Sep 04, 2017 11:12 am

I figured it out. I need to pass the item object whereas I was initially only passing the item id.

The code now looks like this: -

Code: Select all

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');

$id = JFactory::getApplication()->input->get('id');

$model =& JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request'=>true));
$appParams = JFactory::getApplication()->getParams();
$model->setState('params', $appParams);
$item =& $model->getItem($id);
$jcFields = FieldsHelper::getFields('com_content.article',  $item, True);

foreach($jcFields as $jcField)
{
	$jcFields[$jcField->name] = $jcField;
}
Then, following the section here titled "Loading individual fields", I can call each individual field and key and the module is working beautifully :-)

eestepan
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Fri May 10, 2019 10:35 am

Re: How to display custom fields from an Article in a module?

Post by eestepan » Fri May 10, 2019 10:39 am

Hi, I'm happy to see you figured out how to do this I'm still struggling and will appreciate some help.

I have some custom fields values and even labels I'll like to render in a part of module, for every corresponding article. I know that maybe with your code is sufficient enough but I'm not a programmer.

<?php if( $show_intro ): ?>

<div class="bt-introtext">


<?php echo $row->description; ?>. <--- want to replace this with custom fields

</div>


<?php endif; ?>


Post Reply

Return to “Joomla! 3.x Coding”