Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically set BackendMenu context from controller path #1311

Conversation

sephyld
Copy link
Contributor

@sephyld sephyld commented Feb 16, 2025

Closes #1269

I've added the three protected attributes $author, $plugin and $sideMenuItemCode in order to set the backend menu context in the base controller class. As per linked issue's description, I'm setting the values for these attributes during class generation.

The only reason left for the constructor to be overridden by the created controller was the chance of using the --sidebar option when using the create:controller artisan command. Passing this option resulted in setting the boolean sidebar property for it to be used in the template scaffold file. If this property is true the controller assigns the value compact-container to the $bodyClass attribute.

In order to get rid of it I thought it would be better to set the same value for the $bodyClass outside the constructor

@LukeTowers
Copy link
Member

@sephyld we could probably set those values from the namespace of get_class($this) inside of the run method after the action has been run but before the view is rendered if BackendMenu::getContext() doesn't return anything (or just set it by default in the run method before the action is run so that it can be overridden). That way devs don't have to provide any values at all if they have the default approach of 'author.plugin.controller.action` for menu context but can still override it if for some reason they need a different backend context (usually because they're providing a controller that is going to be injected into another context's navigation menu, i.e. a Comments plugin that extends the Blog plugin and adds the Comments controller to the blog main menu item).

@LukeTowers LukeTowers added this to the 1.2.8 milestone Feb 17, 2025
@LukeTowers LukeTowers added the enhancement PRs that implement a new feature or substantial change label Feb 17, 2025
@sephyld
Copy link
Contributor Author

sephyld commented Feb 23, 2025

@LukeTowers hi

I see. I was thinking about something like this, please let me know if I'm on the right track

        // run method
        // [...]
        /*
         * Set the admin preference locale
         */
        BackendPreference::setAppLocale();
        BackendPreference::setAppFallbackLocale();

        /*
         * Set the navigation context
         */
        $this->setNavigationContext();

        /*
         * Execute AJAX event
         */
        if ($ajaxResponse = $this->execAjaxHandlers()) {
            $result = $ajaxResponse;
        }
        // [...]
    protected function setNavigationContext(): void
    {
        $context = \BackendMenu::getContext();
        $fullClassNameArray = explode('\\', get_class($this));

        $author = $fullClassNameArray[0];
        $plugin = $fullClassNameArray[1];
        $className = $fullClassNameArray[count($fullClassNameArray) - 1];

        $owner = $context->owner ?? "$author.$plugin";
        $mainMenuCode = $context->mainMenuCode ?? strtolower($plugin);
        $sideMenuCode = $context->sideMenuCode ?? strtolower($className);

        \BackendMenu::setContext($owner, $mainMenuCode, $sideMenuCode);
    }

This way we cuold get rid of the $author, $plugin and $sideMenuItemCode protected attributes. Devs could simply override the setNavigationContext method doing something like this in the controller classes

    protected function setNavigationContext(): void
    {
         \BackendMenu::setContext("AnotherAuthor.AnotherPlugin", "anotherplugin", "items");
    }

or if \BackendMenu::setContext is called in the constructor the getContext call will return the previously given values.

Does it check out?

LukeTowers added a commit that referenced this pull request Feb 26, 2025
@LukeTowers
Copy link
Member

That's perfect @sephyld, thanks very much! I've implemented it in 5d0b9ca as a part of #1312

@LukeTowers LukeTowers closed this Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement PRs that implement a new feature or substantial change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically set BackendMenu context from controller path
2 participants