0

I have an override for the category/blog.php layout and i want to display a headerimage and a subtitle with the custom fields introduced in joomla 3.7. For this implementation I already use Joomla 3.9.3.

I have already used custom fields in articles and it works fine, but not in categories.

To get this to work I limit this to the subtitle. On the screenshot you can see I added a custom field "subtitle" to Category and finally the input field for the subtitle will be placed only in categories (and not in articles).

enter image description here

I tried to display the custom field like the ones in my articles in the other override in category/blog_item.php (-> https://docs.joomla.org/J3.x:Adding_custom_fields/Overrides, Section "Loading individual fields")

<?php
    // File location: /templates/templatename/html/com_content/category/blog.php

    // loop first to make the fields easy to reference via the field name as a key
    foreach($this->item->jcfields as $jcfield):
        $this->item->customFields[$jcfield->name] = $jcfield;
    endforeach;

    echo $this->item->customFields['subtitle']->label;
    echo $this->item->customFields['subtitle']->name;
    echo $this->item->customFields['subtitle']->value;
    echo $this->item->customFields['subtitle']->rawvalue;
?>

I also tried it with this code and only the name and the label of the custom field "subtitle" is displayed:

<?php 
    foreach(FieldsHelper::getFields('com_content.categories', $child) as $field) :
         $child->customFields[$field->name] = $field;
    endforeach;

    echo $child->customFields['subtitle']->label;
    echo $child->customFields['subtitle']->name;
    echo $child->customFields['subtitle']->value;
    echo $child->customFields['subtitle']->rawvalue; 
?>

What am I doing wrong?

  • As a matter of best coding practices, please get in the habit of writing a single space before and after concatenating dots/commas. One of these days, you might be banging your head against the wall wondering why your "clearly accessed object" isn't being accessed. stackoverflow.com/q/8288414/2943403 (php version dependent outcomes regarding code ambiguity) For that matter, when I am echoing data, I always use commas for concatenation because it clarifies the script's action as "displaying" rather than concatenating while declaring -- which a dot must be used for. – mickmackusa Feb 22 at 22:54
  • We need more context. Where is this code placed exactly? Is $this->item really a category object? Does $this->item->jcfields actually contains the fields? Is $child a category object? – Sharky Feb 26 at 15:56
0

I had pieces of my code from https://joomla.stackexchange.com/a/23675/13353, but this didn't help me.

And further today I found a solution on https://github.com/joomla/joomla-cms/issues/23227.

The problem was, that i don't have the current category id. Now with this, it's working. Thank you @Sharky for your tip, what object is $child.

<?php
$category   = JRequest::getInt('id');
echo 'cat id is: '. $category;
$currentCatFields = FieldsHelper::getFields('com_content.categories', $this->category, true);
echo '<pre>',print_r($currentCatFields,1),'</pre>';

foreach ($currentCatFields as $field) : 
echo $field->label . ':' . $field->value; 
endforeach 
?>
  • You don't need to get category ID and assign it to $category (it's not the same as $this->category). You just needed to pass category object which you already had ($this->category instead of $this->item) to FieldsHelper::getFields(). – Sharky Feb 27 at 8:13
  • Also avoid using JRequest. It's been long deprecated and will not be available in Joomla 4. – Sharky Feb 27 at 8:14
  • Thank you for your explanation. Do I also get this work in index.php? – Cbinger Feb 27 at 9:20
  • Category object is not available in template's index.php file. You'd have to retrieve it and its fields. See similar answer about articles joomla.stackexchange.com/a/23050/12652. – Sharky Feb 27 at 9:55

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.