jreviews:listing:badges.{:name}
Filters the status badges for a specific display context (detail, list, or card view).
Start here
Copy the boilerplate below, then add the logic you need. Return the modified first argument. New to hooks? Read the hooks primer first.
Replace {:name} with the display context you want to target. See available contexts below.
Use jreviews:listing:badges when the same change should apply everywhere. It runs before the context-specific hooks.
Fires after the generic badges hook, allows context-specific customization
Badge options
Each block renders the JReviews badge component. Keep the size and style values from $filterResult unless you have a deliberate reason to override the surrounding page.
blocks- status, featured, recent, or hot by default The default badge blocks. A block can be a callback-and-position array (recommended), a callable, or an HTML string; the latter two render at position 999.
include- null by default, or an array of badge keys null shows all available badges. An array shows only the named badges.
exclude- [] by default, or an array of badge keys Hides the named badges when include is null.
color- gray by default; primary, red, orange, yellow, green, lime, blue, purple, pink, gray, or light Applies when you render a custom badge block. The built-in blocks choose their own colors.
size- md by default; xs, sm, base, md, or lg Controls padding and text size. base and md use the same default size.
solid- false by default; true or false true creates a filled badge; false creates the softer default style. Badges do not have a variant option.
Parameters
| Name | Type | Description |
|---|---|---|
$filterResult |
array |
Same structure as jreviews:listing:badges |
$listing |
\JReviews\App\Models\Listing |
The listing being displayed |
Variations
Use one of these hook names when you only want to change a specific display context.
jreviews:listing:badges.detail- Fires in detail view
jreviews:listing:badges.list- Fires in list view
jreviews:listing:badges.card- Fires in card view
For example, to target jreviews:listing:badges.detail only:
fwd_add_filter('jreviews:listing:badges.jreviews:listing:badges.detail', function($filterResult, $listing)
{
// Your code here
return $filterResult;
}, 20, 2);
Boilerplate
Paste this into your hook file, then add your logic and return the modified first argument.
fwd_add_filter('jreviews:listing:badges.{:name}', function($filterResult, $listing)
{
// Your code here
return $filterResult;
}, 20, 2);
, 20, N sets the priority and how many arguments your callback accepts. If you use both $filterResult and $listing, keep , 20, 2 so both are passed to your callback.
Examples
- Add a custom-field badge
- Add badges for one category
- Add the listing type as a badge
- Add badges for paid listing plans
- Add a badge with a callable
- Add a badge as HTML
Add a custom-field badge
Show the selected text from a custom field as a badge in the listing detail, list, and card views.
fwd_add_filter('jreviews:listing:badges', function (array $filterResult, $listing): array {
// Replace jr_safetylevel with your custom field name.
$label = $listing->getFieldText('jr_safetylevel');
$value = $listing->getFieldValue('jr_safetylevel');
if (! $label || ! $value) {
return $filterResult;
}
$filterResult['blocks'] = fwd_collect($filterResult['blocks'] ?? [])
->put('safety-level', [
'callback' => fn () => fwd_view('jreviews::components.badge', [
'color' => 'green',
'size' => $filterResult['size'],
'solid' => $filterResult['solid'],
'attributes' => [
'data-jr-badge' => 'safety-level',
'data-jr-value' => $value,
],
'slot' => $label,
])->render(),
'position' => 5,
]);
return $filterResult;
}, 20, 2);
Add badges for one category
Limit two custom-field badges to a specific category, as in the V5 listing_status_labels example.
fwd_add_filter('jreviews:listing:badges', function (array $filterResult, $listing): array {
// Replace 50 with the category ID where these badges should appear.
if ((int) $listing->cat_id !== 50) {
return $filterResult;
}
$badges = [
'safety-level' => [
'field' => 'jr_safetylevel',
'color' => 'teal',
'position' => 5,
],
'safety-status' => [
'field' => 'jr_safetystatus',
'color' => 'orange',
'position' => 6,
],
];
$blocks = fwd_collect($filterResult['blocks'] ?? []);
foreach ($badges as $key => $badge) {
$label = $listing->getFieldText($badge['field']);
if (! $label) {
continue;
}
$blocks->put($key, [
'callback' => fn () => fwd_view('jreviews::components.badge', [
'color' => $badge['color'],
'size' => $filterResult['size'],
'solid' => $filterResult['solid'],
'attributes' => ['data-jr-badge' => $key],
'slot' => $label,
])->render(),
'position' => $badge['position'],
]);
}
$filterResult['blocks'] = $blocks;
return $filterResult;
}, 20, 2);
Add the listing type as a badge
Add a badge using the current listing type's title.
fwd_add_filter('jreviews:listing:badges', function (array $filterResult, $listing): array {
$listingType = $listing->listing_type;
if (! $listingType?->title) {
return $filterResult;
}
$filterResult['blocks'] = fwd_collect($filterResult['blocks'] ?? [])
->put('listing-type', [
'callback' => fn () => fwd_view('jreviews::components.badge', [
'color' => 'blue',
'size' => $filterResult['size'],
'solid' => $filterResult['solid'],
'attributes' => ['data-jr-badge' => 'listing-type'],
'slot' => $listingType->title,
])->render(),
'position' => 5,
]);
return $filterResult;
}, 20, 2);
Add badges for paid listing plans
Show a different badge for each paid plan assigned to a listing.
fwd_add_filter('jreviews:listing:badges', function (array $filterResult, $listing): array {
$planIds = array_map('intval', $listing->paid['plan_ids'] ?? []);
$plans = [
1 => ['key' => 'basic-plan', 'label' => 'Basic Plan', 'color' => 'purple'],
2 => ['key' => 'premium-plan', 'label' => 'Premium Plan', 'color' => 'green'],
];
$blocks = fwd_collect($filterResult['blocks'] ?? []);
foreach ($plans as $planId => $plan) {
if (! in_array($planId, $planIds, true)) {
continue;
}
$blocks->put($plan['key'], [
'callback' => fn () => fwd_view('jreviews::components.badge', [
'color' => $plan['color'],
'size' => $filterResult['size'],
'solid' => $filterResult['solid'],
'attributes' => ['data-jr-badge' => $plan['key']],
'slot' => $plan['label'],
])->render(),
'position' => 5 + $planId,
]);
}
$filterResult['blocks'] = $blocks;
return $filterResult;
}, 20, 2);
Add a badge with a callable
Use a callable when the badge only needs to render after the other badge blocks. Callables use the default position 999.
fwd_add_filter('jreviews:listing:badges', function (array $filterResult, $listing): array {
$filterResult['blocks'] = fwd_collect($filterResult['blocks'] ?? [])
->put('member-only', function () {
return fwd_view('jreviews::components.badge', [
'color' => 'purple',
'slot' => 'Members only',
])->render();
});
return $filterResult;
}, 20, 2);
Add a badge as HTML
An HTML string is accepted and renders at position 999. Use fwd_e() for every dynamic value placed in the HTML, including text and attributes.
fwd_add_filter('jreviews:listing:badges', function (array $filterResult, $listing): array {
$label = $listing->getFieldText('jr_partner_level');
if (! $label) {
return $filterResult;
}
$filterResult['blocks'] = fwd_collect($filterResult['blocks'] ?? [])
->put('partner', '<span class="jrPartnerBadge" data-jr-badge="'.fwd_e($label).'">'.fwd_e($label).'</span>');
return $filterResult;
}, 20, 2);
Source Files
resources/views/site/components/listing/badges.blade.php