Joomla 3.7 - Custom fields in module

2,357 views
Skip to first unread message

Kai Røen

unread,
Mar 9, 2017, 9:33:33 AM3/9/17
to Joomla! General Development
I was wondering if it is possible to fetch fields from "custom fields" in a module? I would like to extend the module mod_latest_news to display link, title and introtext with one of the custom fields for the article.

I can of course make a query to get it straight from the database, but if there are some code to access the fields, it would be great.

Bakual

unread,
Apr 12, 2017, 11:25:36 AM4/12/17
to Joomla! General Development
I haven't tested, but I think it should be possible with an override.

Mohammad Hossein Shakouri

unread,
Jun 16, 2017, 9:20:17 AM6/16/17
to Joomla! General Development
On Thursday, March 9, 2017 at 6:03:33 PM UTC+3:30, Kai Røen wrote:
> I was wondering if it is possible to fetch fields from "custom fields" in a module? I would like to extend the module mod_latest_news to display link, title and introtext with one of the custom fields for the article.
>
> I can of course make a query to get it straight from the database, but if there are some code to access the fields, it would be great.

The first answer I posted has a mistake, it's inside mod_articles_category module.

For mod_article_news I have deleted the code since I did not needed it.
But it was a bit easier, no libraries needed to be loadad

Mohammad Hossein Shakouri

unread,
Jun 16, 2017, 9:20:19 AM6/16/17
to Joomla! General Development
On Thursday, March 9, 2017 at 6:03:33 PM UTC+3:30, Kai Røen wrote:
> I was wondering if it is possible to fetch fields from "custom fields" in a module? I would like to extend the module mod_latest_news to display link, title and introtext with one of the custom fields for the article.
>
> I can of course make a query to get it straight from the database, but if there are some code to access the fields, it would be great.


I've done it this way within joomla module - content - news ->

You must load the library first


JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

top of the file somewhere

and then you can access cfields for each item in list like this

// How to get joomla custom fields within a joomla module override
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$jcFields = FieldsHelper::getFields('com_content.article', $item, true);
$array = json_decode(json_encode($jcFields), True);
$fields = [];
foreach ($array as $field) {
array_push($fields,$field['name'],$field['value'],$field['rawvalue']);
}





my code for a slide show is



defined('_JEXEC') or die;
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

?>
<div class="full-width swiper-container slider-<?php echo $moduleclass_sfx; ?>" dir="rtl">
<div class="swiper-wrapper">
<?php foreach ($list as $item) : ?>
<?php
/*
$jcategories = JCategories::getInstance('Content');
$category = $jcategories->get($item->catid);
$currentCatFields = FieldsHelper::getFields('com_content.categories', $category, true);
print_r($currentCatFields);
*/
// How to get joomla custom fields within a joomla module override
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$jcFields = FieldsHelper::getFields('com_content.article', $item, true);
$array = json_decode(json_encode($jcFields), True);
$fields = [];
foreach ($array as $field) {
array_push($fields,$field['name'],$field['value'],$field['rawvalue']);
}
$text = $fields[1];
$image = $fields[4];
$imgURL = $fields[5];
echo '<div class="swiper-slide">
<div class="slide-text">'.$text.'</div>'.
$image
.'<div class="slide-overlay"></div></div>';
/*foreach ($object as $value)
$array[] = $value->post_id;
*/
#require JModuleHelper::getLayoutPath('mod_articles_news', '_item');
?>
<?php endforeach; ?>
</div>
<!-- Add Pagination -->
<div class="swiper-pagination"></div>
<!-- Add Navigation -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
Reply all
Reply to author
Forward
0 new messages