1

I tried below code

<?php

$article_id = 2;

$model = JModelLegacy::getInstance('Article', 'ContentModel');
$article = $model->getItem($article_id);

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$fields = FieldsHelper::getFields('com_content.article', $article, true);

foreach ($fields as $key => $field)
{
   echo $field->title . ' : ' . $field->rawvalue. ' <br> ';
}

?>

I want to fetch/get the fields and their values individually. also tried

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

but giving me error. please guys help me out.

3
  • but giving me error Please edit your question to include the exact error message that you are receiving.
    – mickmackusa
    Sep 2, 2021 at 22:00
  • I am not getting any error... i need support.. Sep 4, 2021 at 7:40
  • Can you please provide some debugging details such as dumping $article and $fields? It is important that you narrow your support question to the earliest point of error. Telling us what is working as desired helps volunteers to better understand your problem and confidently suggest a resolution.
    – mickmackusa
    Sep 27, 2021 at 4:33

3 Answers 3

2

Since you are talking about a module, and your code and comments indicate that you are trying to get a specific article by its ID, your module helper should look like:

<?php

namespace Joomla\Module\YourModuleName\Site\Helper

\defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

abstract class YourModuleNameHelper
{
    // I saw in your comment before, you trying get the
    // ID of the article using the url param
    // so I will assume you want to get the article ID
    // when you are in a article view
    public static function getArticle(&$params)
    {
        $app = Factory::getApplication();
        if (
            $app->input->get('option') === 'com_content'
            && $app->input->get('view') === 'article'
        ) {
               // Save to here all the datas you need return
               $items = new \stdClass;

               // Get the article ID
               $article_id = $app->input->getInt('id');

               // Set application parameters in model
               $appParams = $app->getParams();
               
               // The article model
               $model = $app->bootComponent('com_content')->getMVCFactory()->createModel('Article', 'Site', ['ignore_request' => true]);
               
               // Please, use any other filter as you need
               $model->setState('params', $appParams);
               $model->setState('filter.published', 1);
               $model->setState('article.id', (int) $article_id);

               $article = $model->getItem();

               // Get the custom fields
               $fields = [];
               $jcfields = FieldsHelper::getFields('com_content.article', $article);
               if (!empty($jcfields)) {
                   foreach ($jcfields as $field) {
                       $fields[$field->name] = $field;
                   }
               } 
               
               $items->fields = $fields;
       
               // rest of the code... like get the title, introtext etc

               return $items;    
        }
    }
}
1

I'm not sure if you discovered an answer yourself but this is what I do in J4 to get custom fields:

use \Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

FieldsHelper::getFields('com_content.article', $article);

Then you can cycle through the returned fields in your foreach. Hope this helps.

2
  • Hello Glenn, No i didn't get the answer yet as joomla documentation is so outdated(i mean not updated).. Sep 27, 2021 at 6:36
  • I tried your code in my module but it's not working... <?php foreach ($this->items as $i => $item) : ?> <?php echo $item->customtitle; ?> <?php endforeach; ?> error: Using $this when not in object context Sep 27, 2021 at 6:39
1

I solved a similar problem, based on great help from the Joomla forum: https://forum.joomla.org/viewtopic.php?f=831&p=3677225

Setting the namespace with a use statement was not working for me.

Here is the final standalone example:

namespace some\custom\namespace;

define('JOOMLA_MINIMUM_PHP', '7.2.5');
define('_JEXEC', 1);

// Just to show your example can live in any sub-folder of "its own"
define('JPATH_BASE', __DIR__ . "/../../../../");
include_once JPATH_BASE . "/includes/defines.php";
include_once JPATH_BASE . "/includes/framework.php";

$container = \Joomla\CMS\Factory::getContainer();

$container->alias('session.web', 'session.web.site')
    ->alias('session', 'session.web.site')
    ->alias('JSession', 'session.web.site')
    ->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
    ->alias(\Joomla\Session\Session::class, 'session.web.site')
    ->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');

$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);

\Joomla\CMS\Factory::$application = $app;

\JLoader::register('JNamespacePsr4Map', JPATH_LIBRARIES . '/namespacemap.php');
(new \JNamespacePsr4Map)->load();

$app->createExtensionNamespaceMap();

use \Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

// Get data for the logged-in user
$user = \Joomla\CMS\Factory::getApplication()->getSession()->get('user');

if ($user->id != NULL) {
  // User logged in, get the custom fields data
  $customFields = FieldsHelper::getFields('com_users.user', $user, 0);
  echo "<pre>USER:" . PHP_EOL;
  var_dump($user);
  echo PHP_EOL . "<hr />CUSTOMFIELDS:" . PHP_EOL;
  var_dump($customFields);
  echo "</pre>";
}
else {
  echo "User not logged in.";
}

Your Answer

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

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