Tutorial 6: Control Post's Action Menu
Tutorial 6: Control Post's Action Menu
Action menu belongs to an item, Via API core Action menu of a Post item is based on API Core app settings, we can add more action menu menus to each resource. Example, we can add edit Edit and delete action menu Delete action menus to a post detailPost Detail.
Step 1: Add action menu to the PostResource setting
In method PostResource::getMobileSettings(), we add "action_menu" and define list of supported actionactions. Each action has common configuration options
- "label": Label show shows on an action menu item
- "value": This is a string to help Mobile App know knows how to drive action when clicks menu item is tapped on
- "acl": this value use to compare is used for comparing with permission response from the item the decide returned in the response to decide if the action is shown or not
See the sample code:
PostResource.php
Code Block |
---|
<?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' |
...
], |
...
] ]); |
...
} } |
...
Step 2: Add permission schema to resource response
Update FindOne We will update the findOne() API code and to set extra permission. Ideally, permission Permission schema is permission allowed to allow or not of the current logged in user with the resource doing certain actions on the Resource item
PostApi.php
Code Block |
---|
<?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); |
...
} |
...
Step 3: Review the action menu
...
on
...
the Mobile App
...