Skip to content

Commit

Permalink
Merge pull request #32 from cheesegrits/cheese-graph
Browse files Browse the repository at this point in the history
Adding AdjacencyListWidget
  • Loading branch information
saade authored Aug 26, 2024
2 parents e428b97 + f6e00e8 commit 2384f5d
Show file tree
Hide file tree
Showing 25 changed files with 2,682 additions and 784 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v1.6.0
uses: dependabot/fetch-metadata@v2.2.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3

- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ You can install the package via composer:
composer require saade/filament-adjacency-list
```

> [!IMPORTANT]
> In an effort to align with Filament's theming methodology you will need to use a custom theme to use this plugin.
>> If you have not set up a custom theme and are using a Panel follow the instructions in the Filament Docs first. The following applies to both the Panels Package and the standalone Forms package.
1. Add the plugin's views to your `tailwind.config.js` file.

```js
content: [
...
'<path-to-vendor>/saade/filament-adjacency-list/resources/**/*.blade.php',
]
```

2. Rebuild your custom theme.

```sh
npm run build
```

## Usage

```php
Expand Down Expand Up @@ -49,10 +68,13 @@ AdjacencyList::make('subjects')
->maxDepth(2) // defaults to -1 (unlimited depth)
```

### Customizing the `MaxDepth` of the tree.
### Triggering an action or opening a URL when clicking on an item.
```php
AdjacencyList::make('subjects')
->maxDepth(2) // defaults to -1 (unlimited depth)
->itemAction('edit') // or view, delete, moveUp, indent etc ...
->itemUrl(
fn (array $item) => YourResource::getUrl('view', ['record' => $item['id']]) // for example
)
```

### Creating items without a modal.
Expand Down Expand Up @@ -201,6 +223,36 @@ AdjacencyList::make('subdepartments')
->orderColumn('sort') // or any other column
```

## Widget

The `AdjacencyListWidget` can be used to render a tree for any model with a recursive child relationship (including many-to-many graph relationships, using Staudenmeir's HasGraphRelationships trait).

The simplest use case is ...

```php
class DepartmentTreeWidget extends AdjacencyListWidget
{
protected static string $relationshipName = 'descendantsAndSelf';

// If you're using a widget on an Edit or View page, the plugin will automatically set the $record for you.
// However, you're free to override this method to customize the record.
public function getModel(): Model | string | null
{
return Department::query()->where(['is_root' => true])->first();
}

// You can configure the widget using the same methods as the form component.
protected function adjacencyList(AdjacencyList $adjacencyList): AdjacencyList
{
return $adjacencyList
->label('Foo')
->editable()
->labelKey('nombre')
->childrenKey('hijos');
}
}

```
## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
Loading

0 comments on commit 2384f5d

Please sign in to comment.