0

Im using Joomla 4. And for some reasons I need to publish my articles with joomla 4 custom html module that shows specific custom fields of current article. I have changed mod_custom's default.php file like this but coldn't get the value of "engine-1" custom field. I will be so happy if someone could help about it.

<?php
defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\WebAsset\WebAssetManager;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
$active = JFactory::getApplication()->getMenu()->getActive();

?>

<?php
// Get the custom field values

$fields = FieldsHelper::getFields('com_menus.item', $active, true);
$active->arbitrary_property_name = array();
foreach ($fields as $field) {
    $active->arbitrary_property_name[$field->name] = $field->value;
}
?>
<?php
// Output the value of the custom field if it exists
if (isset($active->arbitrary_property_name['engine-1'])) {
    echo "<h1>" . $active->arbitrary_property_name['engine-1'] . "</h1>";
} else {
    echo "<p>Custom field 'engine-1' is not defined or does not exist in the current menu item.</p>";
}

?>
1
  • Can you please edit your question to show us the output from var_export($active)? Commented Feb 20, 2023 at 21:17

1 Answer 1

0

I got the hint from here Custom Fields inside Articles News Module in Joomla 4 And made my own module from mod_article_news.

Just kept the codes I need and deleted rest. Than added custom filed part from the topic above like this.

default.php

<?php

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\Component\Content\Site\Model\ArticleModel;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

$jinput = Factory::getApplication()->input;
$id = $jinput->getInt('id');

$model = Factory::getApplication()->bootComponent('com_content')->getMVCFactory()->createModel('Article', 'Site');
$article = $model->getItem($id);

if (!$article) {
    return;
}
$fields = FieldsHelper::getFields('com_content.article', $article, true);
$article->arbitrary_property_name = array();
foreach ($fields as $field) {
    $article->arbitrary_property_name[$field->name] = $field->value;
}
?>

<?php echo $article->arbitrary_property_name['engine-1']; ?>

ArticlesNewsHelper.php

<?php

namespace Joomla\Module\ArticlesNews\Site\Helper;

use Joomla\CMS\Access\Access;
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Joomla\Component\Content\Site\Helper\RouteHelper;
use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Registry\Registry;

defined('_JEXEC') or die;

class ArticlesNewsHelper implements DatabaseAwareInterface
{
    use DatabaseAwareTrait;


    public function getArticles(Registry $params, SiteApplication $app)
    {
        return [];
    }

    public static function getList(&$params)
    {
        return [];
    }
}

I don't have php knowledge. Thats why I made it on my own way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.