diff --git a/src/Fields/OrganicField.php b/src/Fields/OrganicField.php index 74701fab..fbc51869 100644 --- a/src/Fields/OrganicField.php +++ b/src/Fields/OrganicField.php @@ -3,7 +3,9 @@ namespace Binaryk\LaravelRestify\Fields; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; +use Binaryk\LaravelRestify\MCP\Requests\McpIndexRequest; use Binaryk\LaravelRestify\MCP\Requests\McpRequestable; +use Binaryk\LaravelRestify\MCP\Requests\McpShowRequest; use Binaryk\LaravelRestify\Traits\ProxiesCanSeeToGate; use Closure; use Illuminate\Http\Request; @@ -82,14 +84,18 @@ public function hideFromIndex($callback = true) public function showOnMcp($callback = true) { - $this->showOnMcp = $callback; + $this->showOnMcp = $callback instanceof Closure + ? $callback + : fn () => (bool) $callback; return $this; } public function hideFromMcp($callback = true) { - $this->hideFromMcpCallback = $callback; + $this->hideFromMcpCallback = $callback instanceof Closure + ? $callback + : fn () => (bool) $callback; return $this; } @@ -154,6 +160,14 @@ public function isShownOnMcp(RestifyRequest $request, $repository): bool return call_user_func($this->showOnMcp, $request, $repository); } + if ($request instanceof McpShowRequest) { + return $this->isHiddenOnShow($request, $repository) === false; + } + + if ($request instanceof McpIndexRequest) { + return $this->isHiddenOnIndex($request, $repository) === false; + } + return $this->showOnMcp; } diff --git a/tests/Fields/FieldMcpVisibilityTest.php b/tests/Fields/FieldMcpVisibilityTest.php index afd2b9a1..38bf5e76 100644 --- a/tests/Fields/FieldMcpVisibilityTest.php +++ b/tests/Fields/FieldMcpVisibilityTest.php @@ -5,6 +5,7 @@ use Binaryk\LaravelRestify\Fields\Field; use Binaryk\LaravelRestify\Fields\FieldCollection; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; +use Binaryk\LaravelRestify\MCP\Requests\McpIndexRequest; use Binaryk\LaravelRestify\MCP\Requests\McpRequest; use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository; use Binaryk\LaravelRestify\Tests\IntegrationTestCase; @@ -30,7 +31,7 @@ public function test_field_collection_filters_mcp_hidden_fields_for_index(): voi }), // Conditional MCP visibility ]); - // Regular request + // // Regular request $regularRequest = new RestifyRequest; $regularIndexFields = $fields->forIndex($regularRequest, $this->repository); @@ -41,7 +42,7 @@ public function test_field_collection_filters_mcp_hidden_fields_for_index(): voi $this->assertContains('admin_notes', $regularFieldNames); // MCP request without admin - $mcpRequest = new McpRequest; + $mcpRequest = new McpIndexRequest; $mcpIndexFields = $fields->forIndex($mcpRequest, $this->repository); $mcpFieldNames = $mcpIndexFields->map(fn ($field) => $field->getAttribute())->toArray();