#2750 Rendering Fields

Parent Category:
Documentation
Category:
DPFields
Last Updated:
Allon Moritz (laoneo), Thursday, 03 December 2015 16:47 (Thursday, 03 December 2015)
Created:
Thursday, 13 August 2015 11:08 (Thursday, 13 August 2015)
Hits:
7378

Introduction

DPfields does show the fields values on the front end by hooking into Joomla plugin events. The Fields itself are rendered through layouts which makes it easy for custom components or templates to adapt them to the current style.

Rendering

The code which renders the fields is extracted in layouts and does integrate with the default look and feel of articles.

frontend display article with field

Change the output

To adapt the output of the custom fields to your template you can do a layout override as described in the Joomla docs. DPFields uses the JLayout classes to render the output of the custom fields. The code which renders the fields is stored in the file components/com_dpfields/layouts/fields/render.php. This file looks rather complex and contains a lot of PHP code which is not easy to understand for none experienced PHP developers. Probably you don't need to alter that file at all. The single field is rendered in the file components/com_dpfields/layouts/field/render.php and looks actually like:

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

$field = $displayData['field'];
$label = $field->label;
$value = $field->value;
if (! $value)
{
	return;
}

$class = $field->render_class;
?>

<dd class="dpfield-entry <?php echo $class;?>">
	<span class="dpfield-label"><?php echo htmlentities($label);?>: </span>
	<span class="dpfield-value"><?php echo $value;?></span>
</dd>

 

If you want to change the output of a single field copy the file components/com_dpfields/layouts/field/render.php to templates/{{your template}}/html/layouts/com_dpfields/field/render.php.

Override field value

To change the value of a field itself you can do layout overrides the same way as described before of the files in the folder components/com_dpfields/layouts/field. There are the layouts for the different types of fields which do render the value which will be passed to the field/render.php file. For example when you have an image list and you want to add a special class name to the image create a file in the folder templates/{{your template}}/html/layouts/com_dpfields/field/prepare/imagelist.php with the following content:

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

$field = $displayData['field'];
$value = $field->value;
if (! $value)
{
	return;
}

$value = (array) $value;

$buffer = '';
foreach ($value as $path)
{
	if (! $path)
	{
		continue;
	}
	$buffer .= '<img class="my-gallery" src="' . $field->fieldparams->get('directory', 'images') . '/' . $path . '"/>';
}
echo $buffer;

 

developer layout override

Render the fields inside the article description

It is also possible to show the fields inside the article description with the well known mustache syntax. To render all the fields you need to add {{#dpfields}} which is the beginning of an iteration over the list of fields where the {{/dpfields}} tag is the end of it. Mustache ensures the variables are HTML escaped. If you want to render it as it should be (probably the value parameter is such a candidate) add another mustache around it. For example if you want to render the fields as a list like in the image add the following code wherever you want in the article description:

<p>This article does contain the fields inline in the documentation:</p>
<ol>
{{#dpfields}}
	<li>Field Label is <b>{{label}}</b> and the value is <br /><b>{{{value}}}</b></li>
{{/dpfields}}
</ol>

 To prevent DPFields from rendering the fields twice. You can create a special Yes No field, the alias has to be disable-default-rendering. If this is special field is set to yes, then the fields are not rendered through the default Joomla event onContentBeforeDisplay.

frontend display article with fields inline

Editor Button

You can also activate the DPFields editor button to insert single fields into the description area. If the Insert Field button is clicked a modal popup opens which shows the list of fields to insert.

backend edit article form field button

Accessing the fields in the layout

DPFiels adds the fields to the item during the onContentPrepare event. This means if you have a template override of the default article view, then you can access the easily like $this->item->dpfields. It is an array containing all the fields as a key value pair. Like that you can implement your own custom logic with the custom fields.

For example when you want to print the fields as an unnumbered list you have to add the following code:

<ol>
<?php
foreach ($this->item->dpfields as $field) {
	echo '<li><b>'.$field->label.'</b>'.$field->value.'</li>';
}
?>
</ol>
 

Getting the fields of the category

DPFields supports custom fields for the category too. As Joomla doesn't provide an event to attach the fields to a category without a core hack, the fields do need to be fetched through the DPFieldsHelper class like:

DPFieldsHelper::getFields('com_content.category', array('id' => $this->item->catid), true);
 

Comments (0)

Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
More information Ok Decline