Adding and managing objects

In most of the cases; your plugin will have to manage several objects

Define an object

Objects definitions will be stored into the inc/ directory of your plugin. File name will be the name of your class in lowercase; the class name will be the concatenation of your plugin name and your class name.

For example, if you want to create the MyObject in MyExamplePlugin; you will create the inc/myobject.class.php file; and the class name will be MyExamplePluginMyObject.

Your object will extends one of the common core types (CommonDBTM in our example).

Extra operations are aslo described in the tips and tricks page, you may want to take a look at it.

Add a front for my object (CRUD)

The goal is to build CRUD (Create, Read, Update, Delete) and list views for your object.

You will need:

  • a class for your object (inc/myobject.class.php),
  • a front file to handle display (front/myobject.php),
  • a front file to handle form display (front/myobject.form.php).

First, create the inc/myobject.class.php file that looks like:

class PluginMyExampleMyObject extends CommonDBTM {
   public function showForm($ID, $options = []) {
      global $CFG_GLPI;

      $this->initForm($ID, $options);

      if (!isset($options['display'])) {
         //display per default
         $options['display'] = true;

      $params = $options;
      //do not display called elements per default; they'll be displayed or returned here
      $params['display'] = false;

      $out = '<tr>';
      $out .= '<th>' . __('My label', 'myexampleplugin') . '</th>'

      $objectName = autoName(
         (isset($options['withtemplate']) && $options['withtemplate']==2),

      $out .= '<td>';
      $out .= Html::autocompletionTextField(
            'value'     => $objectName,
            'display'   => false
      $out .= '</td>';

      $out .= $this->showFormButtons($params);

      if ($options['display'] == true) {
         echo $out;
      } else {
         return $out;

The front/myobject.php file will be in charge to list objects. It should look like:

include ("../../../inc/includes.php");

// Check if plugin is activated...
$plugin = new Plugin();
if (!$plugin->isInstalled('myexampleplugin') || !$plugin->isActivated('myexampleplugin')) {

//check for ACLs
if (PluginMyExampleMyObject::canView()) {
   //View is granted: display the list.

   //Add page header
      __('My example plugin', 'myexampleplugin'),


} else {
   //View is not granted.

And finally, the front/myobject.form.php will be in charge of CRUD operations:

include ("../../../inc/includes.php");

// Check if plugin is activated...
$plugin = new Plugin();
if (!$plugin->isInstalled('myexampleplugin') || !$plugin->isActivated('myexampleplugin')) {

$object = new PluginMyExampleMyObject();

if (isset($_POST['add'])) {
   //Check CREATE ACL
   $object->check(-1, CREATE, $_POST);
   //Do object creation
   $newid = $object->add($_POST);
   //Redirect to newly created object form
} else if (isset($_POST['update'])) {
   //Check UPDATE ACL
   $object->check($_POST['id'], UPDATE);
   //Do object update
   //Redirect to object form
} else if (isset($_POST['delete'])) {
   //Check DELETE ACL
   $object->check($_POST['id'], DELETE);
   //Put object in dustbin
   //Redirect to objects list
} else if (isset($_POST['purge'])) {
   //Check PURGE ACL
   $object->check($_POST['id'], PURGE);
   //Do object purge
   $object->delete($_POST, 1);
   //Redirect to objects list
} else {
   //per default, display object
   $withtemplate = (isset($_GET['withtemplate']) ? $_GET['withtemplate'] : 0);
         'id'           => $_GET['id'],
         'withtemplate' => $withtemplate

Creative Commons License