Customize Layout of Custom Fields

Overview

Create your very own custom field layouts in output views (this doesn't apply for form layouts), using theme customizations.

In most views, the $listing and $review arrays contain the corresponding custom fields.

JReviews renders these fields dynamically, in a pre-defined fashion, using the code snippets shown below which you can find in many of the template files, especially in templates within the listings/ and reviews/ theme folders.

Listings

<?php echo $CustomFields->displayAll($listing,'content');?>

<?php echo $CustomFields->displayAll($listing,'list');?>

Reviews

<?php echo $CustomFields->displayAll($review,'content');?>

<?php echo $CustomFields->displayAll($review,'list');?>

As you can see above, the displayAll method receives the object array $listing or $review and specififies the context content or list.

content refers to a detail page view, whether it's the listing detail page or the review detail page.

Internally displayAll will check the display settings for each field set in the Fields Manager and decide whether it gets included in the current view.

The $CustomFields helper class contains other methods that allow retrieving the information for individual custom fields so you can create your own layout.

Replace $listing with $review in the examples when you want to show review custom fields

Filter Field Groups

If you need to separate the display of a field group, or several field groups, you can pass a a 3rd options parameter to displayAll array to filter the groups. The options parameter accepts both includeGroups and excludeGroups arrays.

The syntax to include one or more groups is:

<?php 
echo $CustomFields->displayAll($listing,'content', [
  'includeGroups' => [
    'group-name1', 
    'group-name2',
    'group-name3',
  ]
]);
?>

The syntax to exclude or more groups is:

<?php 
echo $CustomFields->displayAll($listing,'content', [
  'excludeGroups' => [
    'group-name1', 
    'group-name2',
    'group-name3',
  ]
]);
?>

You can add one or as many group name aliases in the includeGroups and excludeGroups arrays. The group names can be found in the Field Groups manager.

If you are using the above code in a template that already shows custom fields, you should remove the existing code, or replace it, so the fields are not duplicated on the page.

Show Invidivual Fields

For the control freak in you, an even more targeted approach to display fields is possible, so you can place them invididually in the template.

Field Label

<?php echo $CustomFields->label('jr_fieldname',$listing); ?>

Field Text

This code will output the text of the field value where you place the code in the theme file:

<?php echo $CustomFields->field('jr_fieldname',$listing); ?>

Unformatted Field Text

You can also have a bit more control of the output by using additional parameters when you call the display method: $CustomFields->field($name, &$element, $click2search = true, $outputReformat = true)

The example below calls the brand field and turns off click2search and outputreformat advanced options:

<?php echo $CustomFields->field('jr_fieldname',$listing,false,false); ?>

The above also allows retrieving the text for field options that have associated images.

Field Value

If you have a custom field with field options, this code will output the value of the field as it is referenced in the database:

<?php echo $CustomFields->fieldValue('jr_fieldname',$listing); ?>

Field Value Multiple Options

For multiple select and checkbox fields that can accept multiple options, the fieldValue method returns an array instead of a string so you can't use the above syntax with echo. Instead you can join the values or parse them yourself using any PHP logic you want.

<?php echo implode(', ', $CustomFields->fieldValue('jr_fieldname',$listing)); ?>

Conditionals Based on Custom Fields

Output something only when a field is not empty

<?php if ($CustomFields->fieldValue('jr_fieldname',$listing)): ?>

  <!-- Conditional output -->

<?php endif;?>

Output something only when the field value matches a specific text

<?php if ($CustomFields->field('jr_fieldname',$listing,false,false) == 'string'):?>

  <!-- Conditional output -->

<?php endif;?>

Output something only when an option is selected in a multiple select or checkbox field

<?php if (in_array('option-value', $CustomFields->fieldValue('jr_fieldname',$listing))): ?>

  <!-- Conditional output -->

<?php endif; ?>