Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Fields/OrganicField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions tests/Fields/FieldMcpVisibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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();
Expand Down