Writing an integration for a new plugin and process its input

Hi all,

I have just started recently with the working with Mautic and with the development of new plugins.

In my current stage I got a small plugin written (also mainly following the development guidelines from https://developer.mautic.org) which fetches some data from a given source.
Now my idea is that this fetched data is stored in the contacts table.

There a pretty straightforward (and what has been recommended in the development guide (link)) approach is to create a new instance of “Mautic\LeadBundle\Entity\Lead” in one of my controllers, set the required fields, and persist in the database.
I have tested this and it works.

To tune this I would lke to provide a configurable mapper concept as like in the Social Plugin (let’s take the Instagram one for now), which is set in the Plugin configuration.
I presume that this is the “Mautic” way of doing that. The development guide describes this as “Integration” but doesn’t show how to add this mapper and instead refers to the documentation of the extended class: “AbstractIntegration”. There I found this method: “getAvailableLeadFields”, which seems to do the job. Checking the code from the Social Plugin I have seen that this method is overwritten. So I created an Integration class in my plugin including this overwritten method. However when I put this into my Integration class the “mapping” register chart still does not appear.

I checked the code of the Mautic bundle for the Integration and there I found this snippet:

// PluginBundle/Views/Integration/form.html.php
/* … */ $hasFields = (!empty($formSettings[‘dynamic_contact_fields’]) || isset($form[‘featureSettings’]) && count($form[‘featureSettings’][‘leadFields’]))

/*… */

<?php if ($hasFields): ?>
    <li class="<?php echo $fieldTabClass; ?> <?php if (isset($activeTab) && $activeTab == 'leadFieldsContainer'): echo 'active'; endif; ?> " id="fields-tab">
        <a href="#fields-container" role="tab" data-toggle="tab">
            <?php echo $view['translator']->trans('mautic.plugin.integration.tab.fieldmapping'); ?>
            <?php if ($hasLeadFieldErrors): ?>
                <i class="fa fa-fw fa-warning text-danger"></i>
            <?php endif; ?>
        </a>
    </li>
<?php endif; ?>

//

So it seems that I have to define an additional form for that?

And there is the point where I got lost since the documentation ended here and the social plugin doesn’t give any hint to me, as well. So I would like to raise these questions:

  • Is there a straight forward example of how to implement such a mapper in the plugin configuration?
  • After this mapper is set in the configuration, how is this information processed in the plugin, so that it fetches its data from somewhere and files it into the Leads table according to this mapping?
    (I haven’t found anything for that. In case I have missed it just point me to the code)?

Are you still struggling with this one ?
I faced similar issue, the Documentation is perhaps not complete. I have written integration service but when i try to access the service I am not really able to get the keys, for example when I write custom method or for that matter use existing overwritten methods and use $this->getKeys() I get null.

So in the controller I was able to solve it like so:

private function getIntegrationObject($lineService){
    /** @var \Mautic\PluginBundle\Helper\IntegrationHelper $integrationHelper */
    $integrationHelper = $this->get('mautic.helper.integration');
    /** @var AbstractIntegration $integrationObject */
    return $integrationHelper->getIntegrationObject($service->getName());
}

where getname just passes the name of my integration, this gets me the object but It feels useless to not being able to use the real integration service, I added the Integration class in config of course just like in documentation and referred to other plugins as well but no good results. So working with workaround mostly.

Hi mayanktiwaridotcom,

Sorry for the silence from my side. I have not seen the notification on my mailbox.

I found a way myself which I can get along with… As you said the documentation is not complete (friendly spoken). So I bascially got most of the information from other plugins.

To your problem:
I was able to inject the integration model via the config.php in two areas:

  • In a command (which you can then execute from the command line like “php app/console …”
  • In a model class

Taking the command as an example here is what I did:

In the class I added a constructor:

public function __construct(IntegrationHelper $integrationHelper) {
$this->integrationHelper->getIntegrationObject(“yourObject”)->getIntegrationSettings()->getIsPublished();
parent::__construct();
}

For the injection I added the following entry in the config.php

‘command’ => array(
‘mautic.aUniqueName’ => array(
‘class’ => ‘MauticPlugin<myBundle>Command<MyCommandClass>’,
‘arguments’ => array(
‘mautic.helper.integration’,
),
),

With that I could access the information form the integration form.

Regarding the “keys”: If you refer to the username/password information, you can then access this like that:

$this->integrationHelper->getIntegrationObject(“yourObject”)->getUserName();
$this->integrationHelper->getIntegrationObject(“yourObject”)->getPassword()

Regarding the injection into a controller: I have not tried to inject the integation helper into a controller, but at least for the commands and the model I got it to work.
If you do not get the service injected into your controller, I would suggest that you create a dummy class in either the model or the command (to follow my example), because there I know that it works. Therefore the commands became very handy to work with, because you can trigger them from the commandline directly in see your result instanly - very usefull for try and error approaches ;-).
You can also post the code snipped from your config.php file in here. Maybe we find something…