How to Create a Mautic Plugin: Step 2

By DB Hurley · PUBLISHED September 08, 2015 · UPDATED March 11, 2020

In our previous article we began looking at creating your own Mautic plugin to integrate a third party software with your awesome new open source marketing automation software (yes, Mautic). In this lesson we'll continue to create a Mautic plugin, but first as a very quick review, in our last tutorial we started with a basic introduction to plugin creation. Remember we started with planning a plugin, examining the third party API, and looking at the developer resources provided by Mautic. If you want to read more you can check out the previous article and then come back here when you’re ready.

This second tutorial in creating your Mautic plugin we will start looking at the specifics of some of the files we’re going to write and how we take advantage of both the Mautic API and third party API calls.

Basic Files

There are several basic files we need to write to have our plugin registered within Mautic. We created the files in our last tutorial and this graphic shows them to you again.

 

Image
Create mautic plugin file structure

 

The files we are going to focus on today include the config.php file, the MauticDeskBundle.php, and FormSubscriber.php. These are the quickest of the files and so we can cover them quickly. The other files you see in this screenshot are more specific to this particular integration and they will be covered in the next tutorial. For now let’s look at these first three files in more detail and line by line.

config.php

This first file is the config file which Mautic will use when loading the plugin into the system. In this file we are going to name our plugin, provide a description, version and author. Then we need to define the services we want our plugin to use. Here’s the file:

Services: The services are broken into two types, events and forms. Events are those items which you want to trigger as a result of some action within Mautic. Forms are the functions and calls to run when a form is loaded within Mautic.

e.g. Real-life, the form services will allow your plugin to collect more information from forms within Mautic from the business user (not the site visitor).

The Desk plugin that we are writing will need both an event service and a form service. We’ll dig into each of these services later but notice that the class defined for each of these services is the path to the associated file.

MauticDeskBundle.php

This file is the root file that can be used when extending the Plugin base class. In this plugin we do not need to extend anything so the file below is merely a holding file that routes everything to the Plugin Base parent class.

FormSubscriber.php

This file is associated with the event service we defined earlier in our config.php file. This FormSubscriber is the event that gets triggered based on some action within Mautic. Let’s look at the file and then break it down.

Namespacing and used classes are the first thing you’ll find in the file. Here we define what we’ll be using or referencing later in the file.

This class, FormSubscriber, extends a CommonSubscriber class available for all plugins.

The public function onFormBuild accepts a parameter of the FormBuilderEvent (as you can see type-hinted). Here we define the action for this particular service event. We will create the group, description, label, formType, formTheme, and callback function that we want triggered by this submit action. Lastly we add the submitAction to the event.

In this array of actions we have a few special key-value pairs to mention. First, the formType defines the configuration or parameters your plugin will have as part of the plugin configuration fields. (Don’t worry we’ll return to this later when we look at the form service and the plugin configuration).

The callback defines the location and the function you want to be called when the event is triggered.

Don’t let the formType confuse you, at this point in the process, it’s best to consider this merely a way for your admin configuration settings to be added to your plugin when it’s triggered.

Next Step

The next piece in the plugin tutorial will be to focus on the admin configuration for your plugin (see, it’s coming together) and then explore how the triggered response is pushed to the third party. This plugin may be a short series but will add great functionality. I expect after this tutorial series you will have no problem taking this example and building your own plugins easily to push Mautic data to other systems, the Mautic API is a great way to integrate open source marketing automation with everything else.

Comment

Enzo dell'Aquila
Permalink

I can't wait over for next part. When we can read the full guide?
DB Hurley

In reply to by Enzo dell'Aquila

Permalink

Soon! We're working through it a little bit at a time, and should be getting the next installment next week. Thanks for the encouragement and let us know if you think of something else you'd like to see!
Ben
Permalink

Hi, I want to send information from Mautic to suiteCrm. I know that there is already a integration that push leads from Mautic to SuiteCRM but I need a custom one, I just want to update some fields on the lead or add an opportunity (etc..). I decide to create a new Plugin with my custom SuiteCrmIntegration but I can't make it to work. I've created the Plugin structure, the Integration appear in Campaign > Push leads to Integration. Is this the correct way to do what I want? I am new in Symfony and I don't really know what should I do next... could someone please write me the exactly steps I have to make, also how do I make debug to this Integrations? when I run the cron is shows that the events was run ok. Also if there are other config steps to make in Mautic interface? Where should I add the curl (api) code? in what file? Thank you for any response and sorry for my english.
jm
Permalink

Hi, Was just wondering when we could get to read the next part?
Isby
Permalink

I second Ben's comment.
heathdutton
Permalink

Would be nice if this included an example for how to structure a plugin repository, so that the plugin could be included via composer using the "mautic-plugin" installer type.
Upendra Manve
Permalink

I followed the documentation on https://developer.mautic.org/#plugins and was able to Create my own custom plugin.
Vincenzo
Permalink

Hi there. Long time as gone since last upgrade of this thread! Anyway, does anyone can advice or suggest how to create a plugin who put new contacts data into a third party app such as Yetiforce? Thx in advance to all guys.