1
  1. I am using a repeatable custom field for articles in Joomla! 3.9.16 with an alternative layout (override) of com_fields/layouts/field/render.php and an override of plugins/fields/repeatable/tmpl/repeatable.php to show the fields content in columns "After Display". This is working fine.

  2. The number of columns should depend on the value of a radio-field of the same field-group without "Automatic Display". I can not get this running.

The problem is that I do not get the value of this radio field into the field-layout. I think this is because I do not get the item-id of the current article via $this->item or $displayData['item']. It is no problem to get the 'number-of-columns' value in other overrides like the fields-layout com_fields/layouts/fields/render.php or the often used blog_item.php etc., but I want to do this just in the alternative layout if possible.

The following render.php is a simpler version of my code without stuff like links or images and without all the different versions I tried to get the field-value of 'number-of-columns' to $numberOfColumns. Just kept the variable where it should be echoed.

I was looking into almost every question about "custom fields" on stackexchange and other sites. Did try the helpers and different answers to fetch the item-id and field-values but without any luck.

render.php

<?php
defined('_JEXEC') or die;

if (!key_exists('field', $displayData))
{
    return;
}

$field = $displayData['field'];
$value = $field->value;


if ($value == '')
{
    return;
}

?>

<div class="cols-<?php echo $numberOfColumns; ?>">    
    <?php   
        $obj = json_decode($value, true);
        foreach ($obj as $value) : 

            $headline = $value['Headline'];
            $text= $value['Text'];
    ?>     
        <div class="item">
            <h3><?php echo $headline; ?></h3>
            <div class="content-wrapper">
                <p><?php echo $text; ?></p>
            </div>
        </div>
        <?php endforeach; ?>
</div>

repeatable.php

defined('_JEXEC') or die;

$fieldValue = $field->value;

if ($fieldValue === '')
{
    return;
}

// Get the values
$fieldValues = json_decode($fieldValue, true);

if (empty($fieldValues))
{
    return;
}

echo $fieldValue;
2
  • Are these custom fields on a component you developed yourself or on one of the Joomla core components? (It's so that we can know what files are easily editable). Mar 29, 2020 at 12:58
  • Just custom fields for articles.
    – Mano Meter
    Mar 29, 2020 at 14:11

2 Answers 2

2

Create an override of com_fields/layouts/fields/render.php. Find this line:

$content = FieldsHelper::render($context, 'field.' . $layout, array('field' => $field));

Change it to also pass the item to field layout:

$content = FieldsHelper::render($context, 'field.' . $layout, array('field' => $field, 'item' => $item));

The item with its fields is now accessible in com_fields/layouts/field/render.php.

2
  • Awesome thats exactly what i was looking for thank you so much!!!
    – Mano Meter
    Mar 30, 2020 at 7:34
  • Nice solution! Very neat :-). Mar 30, 2020 at 15:45
1

Here's a backstop solution, for using if no-one comes up with a better idea.

The basic approach is to use a plugin which is triggered by onContentPrepare. In this plugin it gets and processes the custom fields to find the number-of-columns field. It finds the value of this field and stores it in a PHP global variable, which then can be accessed in the render function.

I developed the plugin in a folder plg_content_setcolumns, and here are the files:

setcolumns.xml

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.8" type="plugin" group="content" method="upgrade">
    <name>Set number of columns</name>
    <description>Plugin to set number of columns from a custom field</description>
    <files>
        <filename plugin="setcolumns">setcolumns.php</filename>
    </files>
</extension>

setcolumns.php

<?php
defined( '_JEXEC' ) or die;

class plgContentSetColumns extends JPlugin
{
     public function onContentPrepare($context, $item, $params, $offset)
     {
         if ($context == "com_content.article") 
        {
            JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
            $fields = FieldsHelper::getFields('com_content.article', $item);
            global $ncols;
            $ncols = false;
            foreach ($fields as $field)
            {
                if ($field->name == 'number-of-columns')
                {
                    $ncols = $field->value;
                    break;
                }
            }
        }
        return true;
    }
}

You can then access $GLOBALS['ncols'] in your render code.

You may have to change some variables to suit your own field names.

0

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.