Page tree
Skip to end of metadata
Go to start of metadata

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.

Step 1: Add action menu to the PostResource setting

In method PostResource::getMobileSettings(), we add "action_menu" and define list of supported actions. Each action has common configuration options

  • "label": Label shows on an action menu item
  • "value": This is a string to help Mobile App knows how to drive action when menu item is tapped on
  • "acl": this value is used for comparing with permission returned in the response to decide if 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

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);
} 

Step 3: Review the action menu on the Mobile App



  • No labels