Tips & tricks

Add a tab on a core object

In order to add a new tab on a core object, you will have to:

First, in the plugin_init_{plugin_name} function, add the following:

<?php
//[...]
Plugin::registerClass(
   'PluginMyExampleMyClass', [
      'addtabon' => [
         'Computer',
         'Phone'
      ]
   ]
);
//[...]

Here, we request to add a tab on Computer and Phone objects.

Then, in your inc/myclass.php (in which PluginMyExampleMyClass is defined):

<?php
function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
   switch ($item::getType()) {
      case Computer::getType():
      case Phone::getType():
         return __('Tab from my plugin', 'myexampleplugin');
         break;
   }
   return '';
}

static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
   switch ($item::getType()) {
      case Computer::getType():
         //display form for computers
         $this->displayTabContentForComputer($item);
         break;
      case Phone::getType():
         $this->displayTabContentForPhone($item);
         break;
   }
   if ($item->getType() == 'ObjetDuCoeur') {
      $monplugin = new self();
      $ID = $item->getField('id');
     // j'affiche le formulaire
      $monplugin->nomDeLaFonctionQuiAfficheraLeContenuDeMonOnglet();
   }
   return true;
}

private function displayTabContentForComputer(Computer $item) {
   //...
}

private function displayTabContentForPhone(Phone $item) {
   //...
}

On the above example, we have used two different methods to display tab, depending on item type. You could of course use only one if there is no (or minor) differences at display.

Add a tab on one of my plugin objects

In order to add a new tab on your plugin object, you will have to:

Then, in your inc/myclass.php:

<?php
function defineTabs($options=array()) {
   $ong = array();
   //add main tab for current object
   $this->addDefaultFormTab($ong);
   //add core Document tab
   $this->addStandardTab(__('Document'), $ong, $options);
   return $ong;
}


/**
 * Définition du nom de l'onglet
**/
function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
   switch ($item::getType()) {
      case __CLASS__:
         return __('My plugin', 'myexampleplugin');
         break;
   }
   return '';
}


/**
 * Définition du contenu de l'onglet
**/
static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
   switch ($item::getType()) {
      case __CLASS__:
         self::myStaticMethod();
         break;
   }
   return true;
}

Add several tabs

On the same model you create one tab, you may add several tabs.

<?php
function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
   $ong = [
      __('My first tab', 'myexampleplugin'),
      ___('My second tab', 'myexampleplugin')
      ];
   return $ong;
}

static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
   switch ($tabnum) {
      case 1 : //"My first tab"
         //do something
         break;
      case 2 : //"My second tab""
         //do something else
         break;
   }
   return true;
}

Add an object in dropdowns

Just add the following to your object class (inc/myobject.class.php):

<?php
function plugin_myexampleplugin_getDropdown() {
   return ['PluginMyExampleMyObject' => PluginMyExampleMyObject::getTypeName(2)];
}

Creative Commons License