Attachment Field Function As Separated Add-on

Hi team,



We’re working on adding an attachment field to Mautic form, we’re able to do it by updating/adding files in core. But should be better to modify it as Add-on.



We’re having the following issues that can not modify it as Add-on:

  1. If you want to add a File to menu Choose Form Field you must add File to fields array: $fields=“File”(bundlesFormBundleViewsBuilderindex.html.php.)
  2. When Form submit from page it will transfer data to PublicController and call submitAction to handle data from form, the original submitAction does not have code to handle upload file so we must add code to handle it.(bundlesFormBundleViewsBuilder)
  3. In send email Action don’t have attachment so we must add attachment to it (bundlesFormBundleViewsHelperFormSubmitHelper)

    etc…



    All those thing can’t be done by Add-on so we can’t create an Add-on to add attachment File to Form Bundle.



    Could you have a look at these and advise us how could we get these sorted or is there any document for these?



    Thank you for your help.



    Regards,

    Bryan.

Hi team,

We’re working on adding an attachment field to Mautic form, we’re able to do it by updating/adding files in core. But should be better to modify it as Add-on.

We’re having the following issues that can not modify it as Add-on:

  1. If you want to add a File to menu Choose Form Field you must add File to fields array: $fields[‘file’]=“File”(bundlesFormBundleViewsBuilderindex.html.php.)
  2. When Form submit from page it will transfer data to PublicController and call submitAction to handle data from form, the original submitAction does not have code to handle upload file so we must add code to handle it.(bundlesFormBundleViewsBuilder)
  3. In send email Action don’t have attachment so we must add attachment to it (bundlesFormBundleViewsHelperFormSubmitHelper)
    etc…

All those thing can’t be done by Add-on so we can’t create an Add-on to add attachment File to Form Bundle.

Could you have a look at these and advise us how could we get these sorted or is there any document for these?

Thank you for your help.

Regards,
Bryan.

Hi Bryan,

It sounds like some core adjustments are going to be required regardless to support that type of thing. So, you may have to make the necessary core changes that would provide support to addons then submit as a PR.

Thanks,
Alan

Hi Alan,

Spoke about enhanced form functionality a while back and you had said that the concept of extra steps/triggers or something into the core was a good concept and would look for something like that to be added to core.

This is to enable extra form functions (like paypal, files, etc) would be able to be done as add-ons, without having to add to/burden the core form code.

Surely, with the concept of having a Mautic add-ons marketplace it is something that core should support - being able to add extra functionality to forms, add new menu items/functions in the admin area, add new function to other areas, etc … via add-ons.

Bryan works with me, which is why he’s in here asking the questions … and have already committed substantial time to Mautic dev, customisations, promotion … but finding it difficult how to implement add-on and extra functionality … so is there a way to do this, or we just need to wait for Mautic to allow for this to happen … if it happens.

Just needing some direction. Thanks in advance.

Hi Alan,

Thanks for your reply. Below are the issues that we’re having when adding an attachment field to Mautic form:

  1. If you want to add a File to menu Choose Form Field you must add File to fields array: $fields[‘file’]=“File”(bundlesFormBundleViewsBuilderindex.html.php.)
  2. When Form submit from page it will transfer data to PublicController and call submitAction to handle data from form, the original submitAction does not have code to handle upload file so we must add code to handle it.(bundlesFormBundleControllerPublicController)
  3. In send email Action don’t have attachment so we must add attachment to it (bundlesFormBundleViewsHelperFormSubmitHelper)
  4. Need add download action, mautic.form.type.field_propertyfile in config of formbundle (bundlesFormBundleConfigconfig.php)
  5. Add field header of file in controller(bundlesFormBundleControllerFieldController).
  6. Add file to fieldtype to build form file.(bundlesFormBundleFormTypeFieldType.php)
  7. Add script to validate file in script.html.php.(bundlesFormBundleViewBuilderscript.html.php)
  8. Add file to timeline of lead so lead can see an download file.(bundlesFormBundleViewsSubscribedEventsTimelineindex.html.php)
  9. When add file to form some time script automatic run error because line break, to fix this we must add preg_replace(’/s+/’, ’ ',$html); to getAutomaticJavascript of formmodel(bundlesFormBundleModelFormModel)
  10. In email bundle don’t send file attachment in email (if it has), so to send file must add more code to email model to check if has file and send file attachment.(bundlesEmailBundleModelEmailModel).
  11. Add file additional like: FormFieldFileType.php to folder form, file.html.php to bundlesFormBundleViewsField

Could you please review and advise?

Regards,
Bryan.

Hi!

Going to try and answer these. Not sure what the overall goal is; whether you want to submit it to core or if you want to handle it as an addon. Of course PRs for core changes are more than welcome for review. We definitely want to extend Mautic’s features! If you go the addon route and as you’re working on it, if there is no way around it but to do a core hack, please submit it as a PR so we can possibly improve support for future integrations! Anyway, if you’re wanting to include the whole feature into core, it seems that you have a pretty good handle on the places that need adjustment; just need to write the code and submit it on Github :slight_smile: It looks like you’re looking in the right places.

If you want to go the addon route, here are some insights I hope helps. I’ve also included some thoughts on core enhancements that we should consider while reading your list.

First off, have you guys seen Mautic Developer Documentation yet? It’s a work in progress developer docs. Got side tracked into working on other things but it has definitely enough to get started particularly Mautic Developer Documentation and Mautic Developer Documentation.

Also, Symfony’s docs are very helpful as Mautic is built on Symfony and each addon is treated more or less as a Symfony bundle. The difference is that we provide a simple means via a config file to register the services, menus, routes, etc. See Mautic Developer Documentation.

We also make use of Doctrine entities for data manipulation so may be helpful to have a basic understanding of it (Databases and the Doctrine ORM (Symfony Docs)).

A couple pages of Symfony docs that may help are Forms (Symfony Docs) and Doctrine Events (Symfony Docs) as we make use of Symfony’s form services, types, views and layouts and also rely heavily on event dispatching for addon support. There are examples of the event subscribers in our docs (referring to #extending-forms).

Note that any code I’ve posted here is purely off the top of my head and has not been tested :slight_smile:

To get the custom field and form action registered, use an event listener to register the field as laid out in Mautic Developer Documentation. That will get your field listed in the drop down. As part of that registration, you can also register the custom form service that will generate a form of custom fields for you (formType).

As part of the registration for the action, you define a callback that is executed. It is there that you’ll put the logic of processing the action after the form has been submitted. So this would be where you would handle the upload. However, see #3.

So here is the first of the core changes that are required to assist with this. I’m doing some email refactoring now. It would be very easy for me to add the option to configure an email to attach an asset or assets configured through the Assest Manager. I can also build in the option of passing in an option to MauticEmailBundleModelEmailModel::sendEmail to override configured assets with ones specified. That would allow a function calling the function to set what asset to send at the time the email is sent. So I’ll do this as part of the changes I’m working on.

With that in mind, my first thought is that the uploads this enhancement does should create the file as an Asset (MauticAssetBundleEntityAsset).

	/** @var MauticCoreBundleFactoryMauticFactory $factory */					

	$files = $factory->getRequest()->files->get('fieldname');

	/** @var MauticAssetBundleModelAssetModel $asssetModel */
	$asssetModel = $factory->getModel('asset');

$assetIds = array();
     foreach ($files as $file) {
		/** @var MauticAssetBundleEntityAsset $asset */
		$asset = new Asset();
		$asset->setFile($file);
		$asset->setName('some name'); // defaults to file name
		// ... etc
		$asset->setUploadDir($factory->getParameter('upload_dir'));
		$asset->preUpload();
		$asset->upload();

		// Save the asset
		$asssetModel->saveEntity($asset);
	
		$assetIds[] = $asset->getId();
	}

	/** @var MauticLeadBundleModelLeadModel $leadModel */
	$leadModel = $factory->getModel('lead');
	$lead      = $leadModel->getCurrentLead();
	
	/** @var MauticEmailBundleModelEmailModel $emailModel */
	$emailModel = $factory->getModel('email');
	
	// Requires some core changes to work; will be part of the next release due within a couple weeks
	$email->sendEmail($emailId, $lead, array('assets' => $assetIds));
	

This will be required if you want to submit the changes to core. For an addon, you would register it in the addon’s config.php file instead.

I’m not sure I’m following this. What is meant by a field header? This controller handles saving the field’s configuration when building the form.

If building an addon, this is done through Mautic Developer Documentation. However, if submitting a core enhancement, then the changes would need to be made here to support the file type.

It’s true we don’t have a way to inject custom client side validation code from addons. So this could be a core enhancement. However, we do provide custom validation for server side via the validator callback when registering the custom form submit action. Mautic Developer Documentation

If you hook into the asset system as #3 above, this will already be done for you.

I’ll have to see this one in action to comment on it. Definitely submit it as a PR with a description and how to reproduce the steps to test so we can pull and test.

See #3.

Yes; either core or addon, these will be required. If core, it’ll go into the FormBundle. If an addon, it’ll go into your addon’s bundle structure.

Hope that helps!
Alan

thanks @alanhartless

Dev’s will be checking through your replies - looks like there’s some extra info that’ll help a lot there :wink:

With the core enhancements you are looking at, we’d be looking at having features that would allow for extra fields and functions/call-backs - is this what you are doing?

This is not just for “simple” things like file uploads, but also to allow call-backs from external services, such as payment processor “success” page to pass extra data beyond initial form submission, etc.

Thanks for the replies and assistance so far :wink:

I think a key takeaway here is not to think from the position of what “you” are doing… as in Alan, or me … but rather what “we” can do as a community. I would definitely recommend looking at things from the perspective of what do we as a community want to see Mautic do and what code needs to be written for that to happen. While the Mautic core team serves to provide direction, leadership, and some level of assistance to make sure the community is successful, the work performed, the quality of the product, and the future releases of new ideas will come from community contributions. This means anyone and everyone is encouraged to take part in building new features and submitting the PR’s. I would hate to hear anyone be under the impression they need to wait for someone in a “core team position” to build a new feature. This is the true beauty of open source and one of the underlying tenants of Mautic…our community believe in empowerment and equality.

I am excited about the ideas you have shared and the ways in which you see Mautic improving. I look forward to seeing what you and your devs work on and create. Together, as a community, we can continue to make Mautic stand head and shoulders above every other option.

Thanks!
David

Hi Alan,

Thank you very much for your helps those helped us a lot to continue the development. However, we’re still having the following issue:

  1. Waiting for you and Mautic team to finish “I can also build in the option of passing in an option to MauticEmailBundleModelEmailModel::sendEmail to override configured assets with ones specified. That would allow a function calling the function to set what asset to send at the time the email is sent. So I’ll do this as part of the changes I’m working on.”

Please keep us updated once you’ve completed this as we’re waiting for this to complete the development.

Thanks and regards,
Bryan.

Thanks David,

I see some of the features/functions detailed above were in the latest release, as discussed with the core team in previous months - that will help a lot and we’ll tweak our dev to be in-line with the new core release features … hopefully with some freebie add-ons for the community as well.

Good to see the core team aren’t expecting that ALL “the work performed, the quality of the product, and the future releases of new ideas will come from community contributions” :wink:

Part of the problem is that the community don’t know what the core team are developing and working on - at least not that I know about. So, we rely on conversations/discussions in Slack/Github/Forum around what is happening, how to get things done, etc … which means we do have certain expectations of where the core product is going. I don;t have a solution of how to communicate this effectively between yourselves and the community, except perhaps products like Aha! … not sure if this would be of value to the project and community so we sorta know what each other is doing/working on? (cheap startup licence for 5 Aha! accounts)

The 1.1 release looks really good and we’re already sinking our teeth into it.

We’re looking for a solution to allow someone to attach a file to a mautic form. Did this ever get anywhere?

we developed a working extension which works on our mautic installs, but didn’t get accepted into core:
https://github.com/MaxOnTheHill/mautic-max/pull/5

… we need to apply the patch after an update, but this was working well when we pushed this to github. I think we had a few updates since then, but haven’t updated here, as was sorta dead. Hopefully this gives you a good start to use on your own installs.

Max

@MaxOnTheHill could you link the PR where the attachment field didn’t get accepted into core? Can’t find it.

I asked in the slack chat how they wanted the pull-request submitted, etc and then didn’t complete a pull request as it became a bit too painful.

Feel free to utilise the code linked above as a base for anything wanted for core/otherwise … it’s public in github to use.

I’m interested more in the decision not to submit the PR. I’ve read the discussion at

where Alan and David are writing that it’d be awesome if you could submit the PR and your last comment is

Which seems to be quite positive about submitting the PR. The reason why I’m asking is if you feel there is some obstacle to submit a PR to Mautic project. Maybe I could do something to make the process more enjoyable.

it was the discussions above and in slack that deterred proceeding with a pull request or ongoing mautic dev.

I can ask the team to update the github archive with latest code we’ve used for this functionality, but are no longer actively maintaining it.

Not sure what the reference might be here. We’ve been very excited to see what you would be able to do and I believe we as a community are eager to support you in every way. And once the Marketplace is available you’ll have even more opportunities as a business. The recent release of webhooks has further empowered devs to build powerful plugins and combined with the REST API provide a fantastic platform for growth.

-David

@MaxOnTheHill - if you would point to the Pull Request on Mautic’s repo I would like to personally look into it and see what problems arose during the merge as it seems this was a feature identified by many as interesting. I’m sure @escopecz would agree and is interested in following up on this as well.

Thanks!

I have asked my team to update the code for this feature to our latest version in our repo so you can look to merge it to core/otherwise … should be done over the weekend and then available to the community/core.

Following the development in this case. We’re soon to make a decision on whether we can use Mautic in a use case which includes file attachments. If this is close to done I’ll push the decision a bit.