Action menu of a Post item is based on API Core app settings, we can add more action menus to each resource. Example, we can add Edit and Delete action menus to a Post Detail.
In method PostResource::getMobileSettings(), we add "action_menu" and define list of supported actions. Each action has common configuration options
See the sample code:
PostResource.php
<?php
class PostResource extends ResourceBase
{
public function getMobileSettings($params = [])
{
$searchFilter = (new PostSearchForm());
$searchFilter->setLocal($this->getLocalization());
return self::createSettingForResource([
'resource_name' => $this->resource_name,
'search_input' => [
'placeholder' => 'Search Posts'
],
'sort_menu' => $searchFilter->getSortOptions(),
'filter_menu' => $searchFilter->getWhenOptions(),
'action_menu' => [
[
'label' => 'Edit',
'value' => Screen::ACTION_EDIT_ITEM,
'acl' => 'can_edit'
],
[
'label' => 'Delete',
'value' => Screen::ACTION_DELETE_ITEM,
'style' => 'danger',
'acl' => 'can_delete'
],
]
]);
}
} |
We will update the findOne() API code to set extra permission. Permission schema is to allow or not the current logged in user doing certain actions on the Resource item
PostApi.php
<?php
/**
Find detail one document
*
@param $params
@return mixed
@throws \Exception
*/
public function findOne($params)
{
$id = $this->resolver->resolveId($params);
$post = new PostResource([
'post_id' => $id,
'title' => "Post's title example " . $id,
'description' => "Post's description " . $id,
'text' => "Post's Full Text " .$id
]);
// Set extra permission of current user with the post
$post->setExtra([
'can_edit' => \Phpfox::isAdmin(),
'can_delete' => \Phpfox::isAdmin(),
]);
return $this->success($post);
}
|
