Skip to content

Commit

Permalink
add navs fieldtype
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Aug 23, 2023
1 parent 102db9b commit d80954c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/Fieldtypes/Navs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Statamic\Fieldtypes;

use Statamic\CP\Column;
use Statamic\Facades\GraphQL;
use Statamic\Facades\Nav;
use Statamic\GraphQL\Types\NavType;

class Navs extends Relationship
{
protected $categories = ['relationship'];
protected $canEdit = false;
protected $canCreate = false;
protected $canSearch = false;
protected $statusIcons = false;

protected function toItemArray($id, $site = null)
{
if ($nav = Nav::findByHandle($id)) {
return [
'title' => $nav->title(),
'id' => $nav->handle(),
];
}

return $this->invalidItemArray($id);
}

public function getIndexItems($request)
{
return Nav::all()->sortBy('title')->map(function ($nav) {
return [
'id' => $nav->handle(),
'title' => $nav->title(),
];
})->values();
}

protected function getColumns()
{
return [
Column::make('title'),
];
}

protected function augmentValue($value)
{
return Nav::findByHandle($value);
}

public function toGqlType()
{
$type = GraphQL::type(NavType::NAME);

if ($this->config('max_items') !== 1) {
$type = GraphQL::listOf($type);
}

return $type;
}
}
1 change: 1 addition & 0 deletions src/Providers/ExtensionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ExtensionServiceProvider extends ServiceProvider
Fieldtypes\Lists::class,
Fieldtypes\Markdown::class,
Fieldtypes\Markdown\Buttons::class,
Fieldtypes\Navs::class,
Fieldtypes\NestedFields::class,
Fieldtypes\Radio::class,
Fieldtypes\Range::class,
Expand Down

0 comments on commit d80954c

Please sign in to comment.