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>