WordPress.org

Ready to get started?Download WordPress

Forums

WP MVC
Custom admin menu / using one top level menu per plugin (3 posts)

  1. dodmax
    Member
    Posted 1 year ago #

    Hi there,

    I'm trying to generate the menu by myself since I want to use one top level menu per plugin instead of one per controller.

    So I went through the add_menu_pages() function in wp-mvc\core\loaders\mvc_admin_loader.php and for what I understand I need to add an handling method to the dispatcher doing something like that:

    $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "'.$admin_controller_name.'", "action" => "index"));');

    So my problem is how can I get the current instance of the loader class to access the dispatcher attribute?

    Here is the function I've drafted so far:

    function create_mvc_admin_menu($controllers)
    {
        if (!is_array($controllers) || count($controllers) == 0)
          return;
    
        $menu_position = 20;
        $menu_position = apply_filters('mvc_menu_position', $menu_position);
    
        $main_controller = array_shift($controllers);
        $controller_titleized = MvcInflector::titleize($main_controller);
        $admin_controller_name = 'admin_'.$main_controller;
        $top_level_handle = 'mvc_'.$main_controller;
    
        $method = $admin_controller_name.'_index';
    
        // /!\ This will fail since there is no $this
        $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "'.$admin_controller_name.'", "action" => "index"));');
    
        add_menu_page(
                $controller_titleized,
                $controller_titleized,
                'administrator',
                $top_level_handle,
                array($this->dispatcher, $method),
                null,
                $menu_position
        );
        foreach ($controllers as $controller)
        {
            $method = $controller.'_index';
            $page_handle = $top_level_handle.'-'.$controller;
    
            if (!method_exists($this->dispatcher, $method)) {
                    $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "'.$controller.'", "action" => "list"));');
            }
    
            add_submenu_page(
                    $top_level_handle,
                    $controller_titleized.' ‹ '.MvcInflector::titleize($controller),
                    MvcInflector::titleize($controller),
                    'administrator',
                    $page_handle,
                    array($this->dispatcher, $method)
            );
        }
    }

    Any suggestions? Thanks!

    http://wordpress.org/extend/plugins/wp-mvc/

  2. Janar Jürisson
    Member
    Posted 1 year ago #

    Instead of $this->dispatcher use something like this:
    $dispatcher = new MvcDispatcher();

    and later use it like this

    if (!method_exists($dispatcher, $method)) {
    $dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "'.$controller.'", "action" => "list"));');
    }

    It seems to work that way.

  3. Janar Jürisson
    Member
    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags