Actions

Add new menu and view by a plugin: Difference between revisions

From LimeSurvey Manual

Line 1: Line 1:
How to write a plugin that adds a menu to the admin menubar & when clicked renders some content.
How to write a plugin that adds a menu to the admin menubar & when clicked renders some content.


= 2.5x =
= 2.55.0 and above =


1. Create a new plugin, in our case called '''PageDemo'''. In the plugins/ folder, there should be a folder name called PageDemo and within it a class called PageDemo inheriting \ls\pluginmanager\PluginBase. Also create a folder called views/ in your plugin folder and add index.php to it.
1. Create a new plugin, in our case called '''PageDemo'''. In the plugins/ folder, there should be a folder name called PageDemo and within it a class called PageDemo inheriting \ls\pluginmanager\PluginBase. Also create a folder called views/ in your plugin folder and add index.php to it.
Line 32: Line 32:
</syntaxhighlight>
</syntaxhighlight>


2. Subscribe to event '''beforeAdminMenuRender''' to append menus.
2. Subscribe to event '''beforeAdminMenuRender''' to append menus:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Line 42: Line 42:
     {
     {
         $this->subscribe('beforeAdminMenuRender');
         $this->subscribe('beforeAdminMenuRender');
    }
</syntaxhighlight>
3. Add a function called '''beforeAdminMenurender''' to and append a menu object to the event:
<syntaxhighlight lang="php">
    /**
    * Append menus to top admin menu bar
    * @return void
    */
    public function beforeAdminMenuRender()
    {
        // Create the URL to the plugin action
        $url = $this->api->createUrl(
            'admin/pluginhelper',
            array(
                'sa'    => 'fullpagewrapper',
                'plugin' => $this->getName(),
                'method' => 'actionIndex'  // Method name in our plugin
            )
        );
        // Append menu
        $event = $this->getEvent();
        $event->append('extraMenus', array(
            new Menu(array(
                'label' => 'Menu label',
                'href'  => $url
            ))
        ));
     }
     }
</syntaxhighlight>
</syntaxhighlight>

Revision as of 14:39, 22 November 2016

How to write a plugin that adds a menu to the admin menubar & when clicked renders some content.

2.55.0 and above

1. Create a new plugin, in our case called PageDemo. In the plugins/ folder, there should be a folder name called PageDemo and within it a class called PageDemo inheriting \ls\pluginmanager\PluginBase. Also create a folder called views/ in your plugin folder and add index.php to it.

plugins/
  PageDemo/
    PageDemo.php
    views/
      index.php

This is the PageDemo.php file so far, with basic settings:

<?php

use \ls\menu\MenuItem;
use \ls\menu\Menu;

/**
 * Demo for adding new admin page in LimeSurvey 2.56.1
 * @since 2016-11-22
 * @author Olle Härstedt
 */
class PageDemo extends \ls\pluginmanager\PluginBase
{
    static protected $description = 'Short demo on how to show a new admin page';
    static protected $name = 'PageDemo';
    protected $storage = 'DbStorage';
}

2. Subscribe to event beforeAdminMenuRender to append menus:

    /**
     * Init plugin and subscribe to event
     * @return void
     */
    public function init()
    {
        $this->subscribe('beforeAdminMenuRender');
    }

3. Add a function called beforeAdminMenurender to and append a menu object to the event:

    /**
     * Append menus to top admin menu bar
     * @return void
     */
    public function beforeAdminMenuRender()
    {
        // Create the URL to the plugin action
        $url = $this->api->createUrl(
            'admin/pluginhelper',
            array(
                'sa'     => 'fullpagewrapper',
                'plugin' => $this->getName(),
                'method' => 'actionIndex'  // Method name in our plugin
            )
        );

        // Append menu
        $event = $this->getEvent();
        $event->append('extraMenus', array(
            new Menu(array(
                'label' => 'Menu label',
                'href'  => $url
            ))
        ));
    }

2.06 lts/2.6.x lts

1. Create a new plugin. let us call it Rewards

2. register for events afterAdminMenuLoad and newDirectRequest in the constructor

3. implement the above methods methods in the new plugin class

4. In the method afterAdminMenuLoad, add a new menu item and link it to

   admin/plugins/direct?plugin=<plugin_name>&function=<plugin_action>
   e.g
   admin/plugins/direct?plugin=rewards&function=assignRewards

The best way to to this would be to make use of the createURL method:

   'href' => $this->api->createUrl('plugins/direct', array('plugin' => 'Rewards', 'function' => 'assignRewards')),

5. implement the method newDirectRequest

   public function newDirectRequest(){
       $event = $this->event;
       
       // you can get other params from the request object
       $request = $event->get('request');
       
       //get the function name to call and use the method call_user_func
       $functionToCall = $event->get('function');        
       $content = call_user_func(array($this,$functionToCall)); 
       //set the content on the event      
       $event->setContent($this, $content);        
   }


6. implement the method plugin_action, in our case it is assignRewards(). In this method currently i am just trying to return some dummy content, you can have sophisticated page rendered.

   public function assignRewards() {
        $content = "<h1> plugin content set succesfully </h1>";
        return $content;        
    }


7. The full Rewards.php File:Rewards1.zip


8. This is how the UI looks now.