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

Admin menu customization #75

Closed
damianoporta opened this issue Jun 14, 2015 · 3 comments
Closed

Admin menu customization #75

damianoporta opened this issue Jun 14, 2015 · 3 comments

Comments

@damianoporta
Copy link
Contributor

Hello,
I thought we should give more control on admin menus. At the moment wp-mvc adds a top level menu for each controller. Maybe (I think often) it is not what users want. One solution is to set 'in_menu' => false for each page but it sounds too tedious.

Let suppose we have a configuration like:

MvcConfiguration::set([
    'AdminPages' => [
        [
            'controller' => 'venues',
            'action' => 'index',
            'label'  => 'Venues',                        
            'pages' => [                
                [
                    'controller' => 'venues',
                    'action' => 'new',
                    'label' => 'New venue',                                        
                ],  
                [
                    'controller' => 'other_controller',
                    'action' => 'other_action',
                    'label' => 'Other Label',                                        
                ],                 
            ]
        ],
        [
            'controller' => 'articles',
            'action' => 'show',
            'label'  => 'Articles',                        
            'pages' => [                
                [
                    'controller' => 'comments',
                    'action' => 'show',
                    'label' => 'List of comments',                   
                ],  
            ]
        ],
    ]
]);

As you can see with this configuration we can mix controllers and set what links we really need to show inside the pages array. It is simple.

To work with this kind of configuration I also rewrite add_menu_pages method inside /core/loaders/mvc_admin_loader.php,

public function add_menu_pages() {

        $menuPosition = 12;

        $menuPosition = apply_filters('mvc_menu_position', $menuPosition);

        $adminPages = MvcConfiguration::get('AdminPages');

        foreach ($adminPages as $page) {

            $menuIcon = 'dashicons-admin-generic'; 

            /* check if there is a corresponding model with a menu_icon post type argument */
            try {
                $modelName = MvcInflector::singularize(MvcInflector::camelize($page['controller']));
                $model     = mvc_model($modelName);

                if(isset($model->wp_post['post_type']['args']['menu_icon'])) {
                    $menuIcon = $model->wp_post['post_type']['args']['menu_icon'];    
                }
            } catch (Exception $e) {
                ; //not every controller must have a corresponding model, continue silently
            }

            $controllerTitleized = MvcInflector::titleize($page['controller']);                    
            $topLevelHandle      = 'mvc_' . $page['controller'] . '_' . $page['action'];
            $capability = $this->admin_controller_capabilities[$page['controller']];

            $method = function() use ($page){
              MvcDispatcher::dispatch(['controller' => 'admin_'. $page['controller'], 'action' => $page['action']]);
            };

            add_menu_page(
                $controllerTitleized,
                $controllerTitleized,
                $capability,
                $topLevelHandle,
                $method,
                $menuIcon,
                $menuPosition
            );

            foreach($page['pages'] as $subPage) {

                $method = function() use ($subPage){
                  MvcDispatcher::dispatch(['controller' => 'admin_'. $subPage['controller'], 'action' => $subPage['action']]);
                };  

                $subPageHandle = 'mvc_' . $subPage['controller'] . '_' . $subPage['action'];

                add_submenu_page(
                    $topLevelHandle,
                    $subPage['label'] . ' ‹ ' . $controllerTitleized,
                    $subPage['label'],
                    $capability,
                    $subPageHandle,
                    $method
                );                                
            }  

            $menuPosition++;
        }
    }

Then, I also have removed process_admin_pages because it was no longer called.
What do you think? If you like the idea i will optimize the code and send a PR.

@cyberscribe
Copy link
Collaborator

It is an interesting idea. Because I use this plugin extensively in production environments to support critical features of high-traffic websites, one of my big concerns is that any release maintains 100% backward-compatibility. Pushing something into the Plugin Repository that breaks previous code is a sure way to get a one-star review and angry users. So, if this can be done in a way that is 100% backwards-compatible, I would be interested to see it. Also note 1.3.1 includes support for custom Dashicons, so that would need to be factored in as well. Thanks!

@cyberscribe
Copy link
Collaborator

I'll close this for now, but please do reference this issue if you submit a pull request.

@cyberscribe
Copy link
Collaborator

Note that you can also do:

MvcConfiguration::set(array( 'AdminPages' => array( 'name_of_models' => array( 'icon' => 'dashicons-whatever' ) ) ));

This should be updated in the documentation as the 'icon' value is not currently specified anywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants