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 at 22:00
  • I am not getting any error... i need support.. Sep 4 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 at 4:33
0

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 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 at 6:39
0

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;    
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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