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 ?
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 ofonContentPrepareForm()
can be much, much simpler. When you have a single case in your switch block, just use anif
statement. When you have twoif
conditions that perform the same action, merge them...return true
no matter what happens in the function, justreturn true;
once at the end of the function.