<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.limesurvey.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Potable</id>
	<title>LimeSurvey Manual - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.limesurvey.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Potable"/>
	<link rel="alternate" type="text/html" href="https://www.limesurvey.org/manual/Special:Contributions/Potable"/>
	<updated>2026-08-16T09:08:23Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>https://www.limesurvey.org/manual/index.php?title=Add_new_menu_and_view_by_a_plugin&amp;diff=92288</id>
		<title>Add new menu and view by a plugin</title>
		<link rel="alternate" type="text/html" href="https://www.limesurvey.org/manual/index.php?title=Add_new_menu_and_view_by_a_plugin&amp;diff=92288"/>
		<updated>2018-03-19T11:12:59Z</updated>

		<summary type="html">&lt;p&gt;Potable: &amp;quot;name&amp;quot; argument missing for the Surveymenu::staticAddMenu function.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;How to write a plugin that adds a menu to the admin menubar &amp;amp; when clicked renders some content.&lt;br /&gt;
&lt;br /&gt;
= 3.0.0.beta-3 and above = &lt;br /&gt;
&lt;br /&gt;
Since 3.0.0 it is possible to manually add and create menu entries.&lt;br /&gt;
Depending on your plugin you can use this function as a benchmark to add either a menuentry or a full menu for your plugin.&lt;br /&gt;
Please also have a look at the manual page for menucreation.&lt;br /&gt;
&lt;br /&gt;
For this special case there is the newly added plugin event &amp;quot;onPluginRegistration&amp;quot; that is called once when the plugin is discovered.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  public function onPluginRegistration(){&lt;br /&gt;
    $pluginName = $this-&amp;gt;get(&#039;pluginName&#039;);&lt;br /&gt;
    //Maybe add a check for the correct pluginName here&lt;br /&gt;
    //Check if the menu/menu entries are already created use yii database methods for that&lt;br /&gt;
    &lt;br /&gt;
    $menuArray = array(&lt;br /&gt;
        &amp;quot;parent_id&amp;quot; =&amp;gt; 1, //1 -&amp;gt; main surveymenu, 2-&amp;gt; quickemenu, NULL -&amp;gt; new base menu in sidebar&lt;br /&gt;
        &amp;quot;name&amp;quot; =&amp;gt; &amp;quot;[your plugin menus name]&amp;quot;,&lt;br /&gt;
        &amp;quot;title&amp;quot; =&amp;gt; &amp;quot;[your plugin menus title]&amp;quot;,&lt;br /&gt;
        &amp;quot;position&amp;quot; =&amp;gt; &amp;quot;side&amp;quot;, // possible positions are &amp;quot;side&amp;quot; and &amp;quot;collapsed&amp;quot; state 3.0.0.beta-2&lt;br /&gt;
        &amp;quot;description&amp;quot; =&amp;gt; &amp;quot;[your plugins menu description]&amp;quot;&lt;br /&gt;
    );&lt;br /&gt;
    $newMenuId = Surveymenu::staticAddMenu($menuArray);&lt;br /&gt;
&lt;br /&gt;
    // repeat this as often as you need it&lt;br /&gt;
    $menuEntryArray = array(&lt;br /&gt;
        &amp;quot;name&amp;quot; =&amp;gt; &amp;quot;[name of the action]&amp;quot;,&lt;br /&gt;
        &amp;quot;title&amp;quot; =&amp;gt; &amp;quot;[title for the action]&amp;quot;,&lt;br /&gt;
        &amp;quot;menu_title&amp;quot; =&amp;gt; &amp;quot;[title for the action]&amp;quot;, &lt;br /&gt;
        &amp;quot;menu_description&amp;quot; =&amp;gt; &amp;quot;[description for the action]&amp;quot;,&lt;br /&gt;
        &amp;quot;menu_icon&amp;quot; =&amp;gt; &amp;quot;[icon for action]&amp;quot;, //it is either the fontawesome classname withot the fa- prefix, or the iconclass classname, or a src link&lt;br /&gt;
        &amp;quot;menu_icon_type&amp;quot; =&amp;gt; &amp;quot;fontawesome&amp;quot;, // either &#039;fontawesome&#039;, &#039;iconclass&#039; or &#039;image&#039;&lt;br /&gt;
        &amp;quot;menu_link&amp;quot; =&amp;gt; &amp;quot;[admin/]controller/sa/action&amp;quot;, //the link will be parsed through yii&#039;s createURL method&lt;br /&gt;
        &amp;quot;addSurveyId&amp;quot; =&amp;gt; true, //add the surveyid parameter to the url&lt;br /&gt;
        &amp;quot;addQuestionGroupId&amp;quot; =&amp;gt; true, //add gid parameter to url&lt;br /&gt;
        &amp;quot;addQuestionId&amp;quot; =&amp;gt; true, //add qid parameter to url&lt;br /&gt;
        &amp;quot;linkExternal&amp;quot; =&amp;gt; false, //open link in a new tab/window&lt;br /&gt;
        &amp;quot;hideOnSurveyState&amp;quot; =&amp;gt; null, //possible values are &amp;quot;active&amp;quot;, &amp;quot;inactive&amp;quot; and null&lt;br /&gt;
        &amp;quot;manualParams&amp;quot; =&amp;gt; &amp;quot;&amp;quot; //please read up on this setting as it may render the link useless&lt;br /&gt;
    );&lt;br /&gt;
    SurveymenuEntries::staticAddMenuEntry($newMenuId, $menuEntryArray);&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2.55 to 2.73 =&lt;br /&gt;
&lt;br /&gt;
1. Create a new plugin, in our case called &#039;&#039;&#039;PageDemo&#039;&#039;&#039;. 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.&lt;br /&gt;
&lt;br /&gt;
 plugins/&lt;br /&gt;
   PageDemo/&lt;br /&gt;
     PageDemo.php&lt;br /&gt;
     views/&lt;br /&gt;
       index.php&lt;br /&gt;
&lt;br /&gt;
This is the PageDemo.php file so far, with basic settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
use \ls\menu\MenuItem;&lt;br /&gt;
use \ls\menu\Menu;&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Demo for adding new admin page in LimeSurvey 2.56.1&lt;br /&gt;
 * @since 2016-11-22&lt;br /&gt;
 * @author Olle Härstedt&lt;br /&gt;
 */&lt;br /&gt;
class PageDemo extends \ls\pluginmanager\PluginBase&lt;br /&gt;
{&lt;br /&gt;
    static protected $description = &#039;Short demo on how to show a new admin page&#039;;&lt;br /&gt;
    static protected $name = &#039;PageDemo&#039;;&lt;br /&gt;
    protected $storage = &#039;DbStorage&#039;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. Subscribe to event &#039;&#039;&#039;beforeAdminMenuRender&#039;&#039;&#039; to append menus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Init plugin and subscribe to event&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function init()&lt;br /&gt;
    {&lt;br /&gt;
        $this-&amp;gt;subscribe(&#039;beforeAdminMenuRender&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3. Add a method called &#039;&#039;&#039;beforeAdminMenurender&#039;&#039;&#039; to the plugin class and append a menu object to the event:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Append menus to top admin menu bar&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function beforeAdminMenuRender()&lt;br /&gt;
    {&lt;br /&gt;
        // Create the URL to the plugin action&lt;br /&gt;
        $url = $this-&amp;gt;api-&amp;gt;createUrl(&lt;br /&gt;
            &#039;admin/pluginhelper&#039;,&lt;br /&gt;
            array(&lt;br /&gt;
                &#039;sa&#039;     =&amp;gt; &#039;fullpagewrapper&#039;,&lt;br /&gt;
                &#039;plugin&#039; =&amp;gt; $this-&amp;gt;getName(),&lt;br /&gt;
                &#039;method&#039; =&amp;gt; &#039;actionIndex&#039;  // Method name in our plugin&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        // Append menu&lt;br /&gt;
        $event = $this-&amp;gt;getEvent();&lt;br /&gt;
        $event-&amp;gt;append(&#039;extraMenus&#039;, array(&lt;br /&gt;
            new Menu(array(&lt;br /&gt;
                &#039;label&#039; =&amp;gt; &#039;Menu label&#039;,&lt;br /&gt;
                &#039;href&#039;  =&amp;gt; $url&lt;br /&gt;
            ))&lt;br /&gt;
        ));&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4. Add the method actionIndex to the plugin class. This method will call &#039;&#039;&#039;renderPartial&#039;&#039;&#039; to render a view in the views/ folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * @return string html &lt;br /&gt;
     */&lt;br /&gt;
    public function actionIndex()&lt;br /&gt;
    {&lt;br /&gt;
        return $this-&amp;gt;renderPartial(&#039;index&#039;, array(), true);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5. Add some HTML to the view index.php in the views/ folder&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;html&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&#039;container-fluid&#039;&amp;gt;&lt;br /&gt;
    &amp;lt;h3 class=&#039;pagetitle&#039;&amp;gt;A page&amp;lt;/h3&amp;gt;&lt;br /&gt;
    &amp;lt;div class=&#039;row&#039;&amp;gt;&lt;br /&gt;
        &amp;lt;div class=&#039;col-sm-6&#039;&amp;gt;&lt;br /&gt;
            &amp;lt;p&amp;gt;Left column&amp;lt;/p&amp;gt;&lt;br /&gt;
        &amp;lt;/div&amp;gt;&lt;br /&gt;
        &amp;lt;div class=&#039;col-sm-6&#039;&amp;gt;&lt;br /&gt;
            &amp;lt;p&amp;gt;Right column&amp;lt;/p&amp;gt;&lt;br /&gt;
        &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
6. Result&lt;br /&gt;
&lt;br /&gt;
[[File:pagedemo.png]]&lt;br /&gt;
&lt;br /&gt;
= 2.06 lts/2.6.x lts =&lt;br /&gt;
&lt;br /&gt;
1. Create a new plugin. let us call it Rewards&lt;br /&gt;
&lt;br /&gt;
2. register for events afterAdminMenuLoad and newDirectRequest in the constructor&lt;br /&gt;
&lt;br /&gt;
3. implement the above methods methods in the new plugin class&lt;br /&gt;
&lt;br /&gt;
4. In the method afterAdminMenuLoad, add a new menu item and link it to &lt;br /&gt;
    admin/plugins/direct?plugin=&amp;lt;plugin_name&amp;gt;&amp;amp;function=&amp;lt;plugin_action&amp;gt;&lt;br /&gt;
    e.g&lt;br /&gt;
    admin/plugins/direct?plugin=rewards&amp;amp;function=assignRewards&lt;br /&gt;
The best way to to this would be to make use of the createURL method:&lt;br /&gt;
    &#039;href&#039; =&amp;gt; $this-&amp;gt;api-&amp;gt;createUrl(&#039;plugins/direct&#039;, array(&#039;plugin&#039; =&amp;gt; &#039;Rewards&#039;, &#039;function&#039; =&amp;gt; &#039;assignRewards&#039;)),&lt;br /&gt;
&lt;br /&gt;
5. implement the method newDirectRequest &lt;br /&gt;
&lt;br /&gt;
    public function newDirectRequest(){&lt;br /&gt;
        $event = $this-&amp;gt;event;&lt;br /&gt;
        &lt;br /&gt;
        // you can get other params from the request object&lt;br /&gt;
        $request = $event-&amp;gt;get(&#039;request&#039;);&lt;br /&gt;
        &lt;br /&gt;
        //get the function name to call and use the method call_user_func&lt;br /&gt;
        $functionToCall = $event-&amp;gt;get(&#039;function&#039;);        &lt;br /&gt;
        $content = call_user_func(array($this,$functionToCall)); &lt;br /&gt;
        //set the content on the event      &lt;br /&gt;
        $event-&amp;gt;setContent($this, $content);        &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. implement the method plugin_action, in our case it is assignRewards(). &lt;br /&gt;
In this method currently i am just trying to return some dummy content, you can have sophisticated page rendered. &lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt;public function assignRewards() {&lt;br /&gt;
        $content = &amp;quot;&amp;lt;h1&amp;gt; plugin content set succesfully &amp;lt;/h1&amp;gt;&amp;quot;;&lt;br /&gt;
        return $content;        &lt;br /&gt;
    }&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
7. The full Rewards.php &lt;br /&gt;
[[File:Rewards1.zip]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
8. This is how the UI looks now.&lt;br /&gt;
[[File:Screenshot_2014-08-21_01.34.05.png]]&lt;/div&gt;</summary>
		<author><name>Potable</name></author>
	</entry>
</feed>