aymanalhattami / filament-page-with-sidebar

Organize pages in the sidebar in order to make navigation between pages more comfortable.
https://github.com/aymanalhattami/filament-page-with-sidebar
MIT License
175 stars 22 forks source link

ViewRessource : Infolist & relation manager #16 #17

Closed Thiktak closed 1 year ago

Thiktak commented 1 year ago

A possible solution about the issue #16

Another idea (will test tonight), could be to call an @include of the corresponding view object (specified as constant or dynamically determined).

<x-filament-page-with-sidebar::page>
    @include($this->getChildComponentView())
</x-filament-page-with-sidebar::page>
And getChildComponentView could be a [ReflectionClass::getDefaultProperties](https://www.php.net/manual/en/reflectionclass.getdefaultproperties.php) to get the parent default value's $view of the component.

Implement like this, without creating any view:

// ...
use AymanAlhattami\FilamentPageWithSidebar\Traits\HasPageSidebar;

class ViewUser extends ViewRecord
{
    use HasPageSidebar;

    /**
     * $viewSidebar
     * override $view when HasPageSidebar is activated
     */
    // protected static string $viewSidebar = 'filament.[...].user-resource.pages.view-user';

    protected static string $resource = UserResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\EditAction::make(),
        ];
    }
}

Tested with Infolist & Edit.

Feel free to improve.

aymanalhattami commented 1 year ago

@Thiktak Thank you for your contribution! I appreciate your effort and the time you've dedicated to improving the package

I have some modification

namespace AymanAlhattami\FilamentPageWithSidebar\Traits;

trait HasPageSidebar
{
    /**
      * add sidebar to the page
      */ 
    public static bool $hasSidebar = true;

    /**
     *   public function mountHasPageSidebar
     *   Register automatically view if available
     */
    public function bootHasPageSidebar(): void
    {
        // Why boot ? https://livewire.laravel.com/docs/lifecycle-hooks#boot

        // Using ${'view'} instead of $view in order to avoid Intelephense warning
        if (static::$hasSidebar) {
            static::${'view'} = 'filament-page-with-sidebar::proxy';
        }
    }

    /**
     *   public function getIncludedSidebarView
     *   Return string include view
     */
    public function getIncludedSidebarView(): string
    {
        if (is_subclass_of($this, '\Filament\Pages\Page')) {
            $props = collect(
                (new \ReflectionClass($this))->getDefaultProperties()
            );

            if ($props->get('view')) {
                return $props->get('view');
            }
        }

        // Else:
        throw new \Exception('No view detected for the Sidebar. Implement Filament\Pages\Page object with valid static $view');
    }
}

Why these changes? one $view property for the page view blade is better than having two properties $view and $viewSidebar

sonarcloud[bot] commented 1 year ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Thiktak commented 1 year ago

I edited it.

This should be cleaner now.

aymanalhattami commented 1 year ago

Thanks @Thiktak