Display dynamic content in a banner field
- Thread started
- Last updated
What you’ll achieve
Display generated banner content on listing pages without adding an input to the listing form.
My site
Show the available stock on each listing, such as 3 available. Contact us to reserve. Hide the message when that listing has no stock.
Create a banner field for generated content
Create a custom field named jr_bannerfield with type Banner. Choose a listing field group in the field editor. Open the listing type where the banner should appear and include that group in its Field Groups selection. Enable the banner in the layouts where it should appear. Replace that name consistently if yours differs. A banner supplies output without adding an editable input to the listing form.
Create a listing field of type Integer, labeled Stock, with the saved name jr_stock. Enter stock in Name; JReviews adds the prefix. Put it in the same associated field group as the banner. Keep it active, optional, writable by listing editors, and readable by visitors who can see the message. Enter 3 on one listing and 0 on another. These are numeric inputs, not predefined options. Leave the banner description empty and hide its label. Both versions below use these same saved stock values.
JReviews 6 Early Access
Add the Blade output
Use this formatting on the modern list or detail pages selected in your Early Access settings.
Use Custom Fields → Edit Field → Advanced Settings → Blade template code on the banner field. Paste this complete example, which reads the Stock value prepared above:
@if ((int) $listing->getFieldValue('jr_stock', 0) > 0)
<p>{{ (int) $listing->getFieldValue('jr_stock', 0) }} available. Contact us to reserve.</p>
@endif
A listing with stock 3 displays 3 available. Contact us to reserve. A listing with zero or no stock displays no banner. Replace the calculation with your generated information when ready.
For longer or shared formatting, select a Field output Blade template from overrides. See Custom Field Output Formatting for the variables, examples, and template paths. Use Blade to format the field output.
JReviews 5 and v6 Legacy
Prepare the banner output
Create an empty banner field with no description. Put this expression in its PHP output format so the field stays hidden until it has content.
return $text ?: false;
Use templates/jreviews_overrides/filters/filter_functions.php.
Use jreviews_overrides/filters/filter_functions.php.
Start a new PHP file with <?php followed by defined('MVC_FRAMEWORK') or die;. Keep each line once when combining hooks.
Fill the banner on list pages
Clickfwd\Hook\Filter::add('post_get_listings_listpage_query', function ($listings, $params) {
foreach ($listings as &$listing) {
if (!isset($listing['Field']['pairs']['jr_bannerfield'])) {
continue;
}
$stock = max(0, (int) ($listing['Field']['pairs']['jr_stock']['value'][0] ?? 0));
$listing['Field']['pairs']['jr_bannerfield']['description'] = $stock > 0
? '<p>'.$stock.' available. Contact us to reserve.</p>'
: '';
}
unset($listing);
return $listings;
});
Fill the banner on detail pages
Clickfwd\Hook\Filter::add('post_get_listing_detailpage_query', function ($listing, $params) {
if (!isset($listing['Field']['pairs']['jr_bannerfield'])) {
return $listing;
}
$stock = max(0, (int) ($listing['Field']['pairs']['jr_stock']['value'][0] ?? 0));
$listing['Field']['pairs']['jr_bannerfield']['description'] = $stock > 0
? '<p>'.$stock.' available. Contact us to reserve.</p>'
: '';
return $listing;
});
Keep both callbacks if you show the message on list and detail pages. Each reads that listing's Stock value, so listings can display different quantities. The listing with Stock 3 must show 3 available. Contact us to reserve. The listing with 0 or no Stock value must show no banner. Replace both field names consistently if yours differ. Keep each callback registered once.
Check populated and empty output
Check a listing whose generated content is populated and another whose banner should be empty. Display generated values as text so entered characters cannot become HTML formatting. Include HTML only when it is trusted content that you control. Verify every list or detail layout where you display the field.
The PHP expression hides the banner when its description is empty. The hooks fill that description before the field appears.
The Blade template generates the content when it displays the field.
If you need live inventory or prices, connect and cache those external data separately. These examples do not include an API client.