jreviews:permission:listing.create
Filters whether a user has permission to create a new listing. Return false to deny permission.
You need to have a working knowledge of Hooks before you get started.
Fires when checking listing creation permissions, before the form is displayed or listing is saved
Parameters
| Name | Type | Description |
|---|---|---|
$canCreate |
bool |
Whether the user can create a listing based on user group settings |
$user |
\JReviews\App\Models\User |
The user attempting to create the listing |
$listing |
\JReviews\App\Models\Listing |
The listing being created (may be empty/new) |
$listingType |
\JReviews\App\Models\ListingType|null |
The listing type, or null if not specified |
Boilerplate Code
Use the boilerplate code to start using the filter, and add your own logic to modify the first argument and return it.
fwd_add_filter('jreviews:permission:listing.create', function($canCreate, $user, $listing, $listingType)
{
// Your code here
return $canCreate;
}, 20, 4);
The , 20, N after your callback are the hook priority and the number of arguments your callback accepts. By default, a hook passes your callback only its first argument; for a filter, that is the value being filtered, so a simple function($value) { ... } needs nothing extra. If your callback declares more parameters, such as function($value, $listing) { ... }, you must add N (the parameter count, 2 here). Because N is the fourth argument to fwd_add_filter() or fwd_add_action(), you must also pass the priority (20 is the default). Leaving these off when your callback expects extra parameters causes a Too few arguments to function ... fatal error.
Examples
Limit One Listing Per User
Limit listing submissions to one per user by checking existing published listings count.
fwd_add_filter('jreviews:permission:listing.create', function($canCreate, $user, $listing, $listingType) {
if ($user->id > 0) {
$count = \JReviews\App\Models\Listing::where('owner_id', $user->id)
->published()
->count();
if ($count > 0) {
return false;
}
}
return $canCreate;
}, 20, 4);
Source Files
app/Policies/ListingPolicy.php