4

I started working a project on the upcoming 3.7 version, using the com_fields.

I have created an article and a category level group of fields. The idea is that I want to use the category level fields to 'globalize' per category those extra values which should be used/displayed as default on each of that category's articles, unless they are overwritten on the article level.

Now on the article view - I have access to the custom fields of the article and I am trying to access also the article's category custom fields, which I did it like so:

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

The short documentation for the FieldsHelper::getFields mentions:

  • Returns the fields for the given context.
  • If the item is an object the returned fields do have an additional field "value" which represents the value for the given item.

But the problem is that I am getting the category's custom fields array containing the fields objects and their properties but without their actual values.

How I could go about this?

6

The key here (which I misunderstood/overlooked) is that for the FieldsHelper::getFields method, to also return the value & rawvalue of the custom fields, the $item has to be a content item object.

I was only passing the id of the category, instead of an object of that category, that's why I didn't get the field's value.

Just for clarity, I post here the declaration of the method and its parameters, as defined in the FieldsHelper class.

public static function getFields($context, $item = null, $prepareValue = false, array $valuesToOverride = null)

So for my case (when in article view), in order to get the custom fields and their values of the article's category, I have to pass the category ($item) as a category object, like so:

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$jcategories = JCategories::getInstance('Content');
$category   = $jcategories->get($article->catid);
$currentCatFields = FieldsHelper::getFields('com_content.categories', $category, true);

As an alternative route, if we know the custom fields ids for which we want to get their their values, we can use the com_fields model like so:

$model = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true));

//$categoryFieldsIds = array();
$currentCatFields = $model->getFieldValues($categoryFieldsIds, $currentCatID);
3

For getting custom fields and values for Joomla articles, you can use the following code:

<?php       
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); //load fields helper
$customFieldnames = FieldsHelper::getFields('com_content.article', $artile_id, true); // get custom field names by article id
$customFieldIds = array_map(create_function('$o', 'return $o->id;'), $customFieldnames); //get custom field Ids by custom field names
$model = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true)); //load fields model
$customFieldTitles = array_map(create_function('$o', 'return $o->name;'), $customFieldnames); //Fetch names for custom fields
$customFieldValues = $model->getFieldValues($customFieldIds , $item->content_item_id); //Fetch values for custom field Ids

?>

Hope it helps!

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