Tutorial 6: Control Post's Action Menu
Tutorial 6: Control Post's Action Menu
Action menu belongs to an item, Via API core app settings, we can add more action menu to each resource. Example, we can add edit and delete action menu to a post detail.
Step 1: Add action menu to the PostResource setting
In method PostResource::getMobileSettings() we add "action_menu" and define list of supported action. Each action has common configuration options
- "label": Label show on an action menu item
- "value": This is a string to help Mobile App know how to drive action when clicks on
- "acl": this value use to compare with permission response from the item the decide the action is shown or not
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'
],
]
]);
}
}
Step 2: Add permission schema to resource response
Update FindOne API code and set extra permission. Ideally, permission schema is permission allowed or not of the current logged in user with 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);
}
Step 3: Review the action menu render on Mobile App