Add Property Modifier in Import Source via own module

Hello Community,

I try to write an own icingaweb2 director import rule property.

First of all I created a new folder structure:

[root@localhost]# pwd
/usr/share/icingaweb2/modules/directorextension

After that I copied the regex replace property php file into my new folder and renamed it to PropertyModifierTestProperty.php

[root@msd-ic-web01 directorextension]# tree
.
├── library
│   └── Director
│       └── ProvidedHook
│           └── Director
│               └── PropertyModifier
│                   └── PropertyModifierTestProperty.php
├── LICENSE
├── module.info
└── run.php

5 directories, 4 files

Now I changed the Classname and the Namespace:

<?php

namespace Icinga\Module\Directorextension\PropertyModifier;

use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;

class PropertyModifierTestProperty extends PropertyModifierHook
{
    public static function addSettingsFormFields(QuickForm $form)
    {
        $form->addElement('text', 'pattern', array(
            'label'       => 'Regex pattern',
            'description' => $form->translate(
                'The pattern you want to search for. This can be a regular expression like /^www\d+\./'
            ),
            'required'    => true,
        ));

        $form->addElement('text', 'replacement', array(
            'label'       => 'Replacement',
            'description' => $form->translate(
                'The string that should be used as a preplacement'
            ),
        ));
    }

    public function getName()
    {
        return 'This is a test';
    }

    public function transform($value)
    {
        return preg_replace(
            $this->getSetting('pattern'),
            $this->getSetting('replacement'),
            $value
        );
    }
}

At last, I created the run.php and the module.info file

<?php

$this->provideHook('director/PropertyModifierTestProperty');

Now I activated my module in icingaweb.
I thought now, that I’m able to see my new property in the Import Source Property Modifier list, but its missing.

Hope someone can give me a short hint.

Cheers, Morde

The one thing I see is the Library/Director which has to be the name of your Module so Library/Directorextension. But I know no example for a module using this hook, so this is the only idea I have.

Hello Dirk,

thank you very much for your answer.

I moved the folder library\Director to library\Directorextension but unfortunately it is still not in my property modifier list.

.
├── library
│   └── Directorextension
│       └── ProvidedHook
│           └── Director
│               └── PropertyModifier
│                   └── PropertyModifierTestProperty.php
├── LICENSE
├── module.info
└── run.php

5 directories, 4 files

Hello,

To debug the problem I created a new PropertyModifer in the directory module:

cp /usr/share/icingaweb2/modules/directorextension/library/Directorextension/ProvidedHook/Director/PropertyModifier/PropertyModifierTestProperty.php /usr/share/icingaweb2/modules/director/library/Director/PropertyModifier/

After that, I changed the namespace in the file and checked for the new Property. But my Test Property wasn’t there.

Now I found the following file: /usr/share/icingaweb2/modules/director/register-hooks.php

In this file are all Modifier Classes listed in an array.

I added my new class to this file, and voilà, I was able to use my Property.

I see no way to expand this file from my module. So I think it is not possible to add new Properties by a module.

Is anyone able to confirm this?

1 Like

Ah, now I start to remember, so much time has passed since I contributed a modifier there.

I do not think the file is the magic, but the function provideHook.

If you change your run.php to look like this, it could do the trick:

$this->provideHook('director/PropertyModifier', PropertyModifierTestProperty::class);

Well, that changed something :smiley:

image

But the framework do not load my class.

I added this line as well to the run.php, but nothing changed:

use Icinga\Module\Directorextension\PropertyModifierPropertyModifierTestProperty;

I got it!

Change /usr/share/icingaweb2/modules/directorextension/library/Directorextension/ProvidedHook/Director/PropertyModifier/PropertyModifierTestProperty.php to contain:

namespace Icinga\Module\Directorextension\ProvidedHook\Director\PropertyModifier;

and /usr/share/icingaweb2/modules/directorextension/run.php to

use Icinga\Module\Directorextension\ProvidedHook\Director\PropertyModifier\PropertyModifierTestProperty;
3 Likes

That works really nice. Thank you very much!