0

excuse for my English. I try to retrieve my value from a custom field in com_categories. I added this field by a plugin. My value is correctly saved but i don't why, i can't retrieve my value from the field in blog_item.php file. (Of course, i ovverided this component) I used this tutorial to make the plugin : https://zunostudios.com/blog/development/203-how-to-add-custom-fields-to-articles-in-joomla

Here's my code :

zfield.php

<?php
// no direct access
defined ( '_JEXEC' ) or die ( 'Restricted access' );

class plgContentZfields extends JPlugin {

        /**
         * Load the language file on instantiation.
         * Note this is only available in Joomla 3.1 and higher.
         * If you want to support 3.0 series you must override the constructor
         *
         * @var boolean
         * @since 3.1
         */

        protected $autoloadLanguage = true;

        function onContentPrepareForm($form, $data) {

                $app = JFactory::getApplication();
                $option = $app->input->get('option');

                switch($option) {

                    case 'com_categories':
                        if ($app->isAdmin()) {

                                JForm::addFormPath(__DIR__ . '/forms');

                                //Show specific forms based on categories
                                $form->loadFile('content', false); 

                        }
                        return true;

                }

                return true;

        }

}
?>

content.xml

<form>
    <fields name="params" >
        <fieldset name="params" >

            <field name="backgroundcolor"
                type="color"
                label="Color of the category"
                /> 
        </fieldset>
    </fields>
</form>

autoblog_item.php (overrided file)

.
.
.
<?php // Todo Not that elegant would be nice to group the params ?>
<?php $useDefList = ($params->get('show_modify_date') || $params->get('show_publish_date') || $params->get('show_create_date')
    || $params->get('show_hits') || $params->get('show_category') || $params->get('show_parent_category') || $params->get('show_author') || $assocParam); ?>

<?php if ($useDefList && ($info == 0 || $info == 2)) : ?>
    <?php // Todo: for Joomla4 joomla.content.info_block.block can be changed to joomla.content.info_block 
$attributes = json_decode($this->item->params);
echo "background".$attributes->backgroundcolor;
    ?>
    <div>
    <?php echo JLayoutHelper::render('joomla.content.info_block.block', array('item' => $this->item, 'params' => $params, 'position' => 'above')); ?>
</div>
<?php endif; ?>
<?php if ($info == 0 && $params->get('show_tags', 1) && !empty($this->item->tags->itemTags)) : ?>
    <?php echo JLayoutHelper::render('joomla.content.tags', $this->item->tags->itemTags); ?>
<?php endif; ?>

.
.
.

My final goal is to put a background-color to each category. I want to use the value in my custom field to define the background-color. Any ideas please ?

2
  • Can you possibly narrow down the isolated point of failure? You have several if conditions in your snippets -- can you tell us which ones are processing as expected? What is the earliest point where your code breaks? I would like to urge you not to bounce in and out of php; it makes your code very hard to read and increases the likelihood of coding typos. Your code inside of onContentPrepareForm() can be much, much simpler. When you have a single case in your switch block, just use an if statement. When you have two if conditions that perform the same action, merge them... Nov 14, 2018 at 13:57
  • In your case, since your code is either executing in the frontend or the backend AND you are doing the same thing either way -- just remove the conditions altogether and execute the lines of code unconditionally. If you are going to return true no matter what happens in the function, just return true; once at the end of the function. Nov 14, 2018 at 13:59

1 Answer 1

1

The category object is $this->category, not $this->item and params are a Registry object so no need for json_decode().

echo $this->category->params->get('backgroundcolor');

UPDATE: To show category params for each article based on that article's category, you need retrieve the category for each article:

$categoryParams = JCategories::getInstance('Content')->get($this->item->catid)->getParams();
echo $categoryParams->get('backgroundcolor');
6
  • Thanks for the answer. But It doesn't work. I mean, i replace "$attributes = json_decode($this->item->params); echo "background".$attributes->backgroundcolor;" with your proposal but nothing changes.
    – Ronis
    Nov 14, 2018 at 15:09
  • What does var_dump($this->category->params); output?
    – Sharky
    Nov 15, 2018 at 6:40
  • To clarify, are you trying to add a different background to each article based on its category?
    – Sharky
    Nov 15, 2018 at 6:45
  • var_dump($this->category->params); output a lot of things, but not ["backgroundcolor"]...
    – Ronis
    Nov 15, 2018 at 9:05
  • In the blog view, where you see the category of the article, i want to add a block with a background-color per category. For exemple, a blue background for Category 1, a red background for Category 2, etc...
    – Ronis
    Nov 15, 2018 at 9:09

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.