3

I am using the module Print for print/mail node's content. However, I have problems to send email. I want to hack the module, so it will use the PHP class PHPMailer instead, and will hard-code the sending of the email. Everythings works perfect, but the situation is that there is no API and I have hard-coded directly in the module, so when the module is upgraded, I will lost my coding. Is there any way of making a hook of the desired function so I will get the same working but will still be able to upgrade the module without problems? I will accept other solutions, but the question is more general (I maybe would need to do something similar with other modules which do not provide API in the future). Thanks!!!

5

Usually there are two situations: your hack may be useful for other users or your hack is very very custom and is not likely to be useful for other users.

In the second case you can go with arpitr answer.

If your hack can be useful for other users (like this case) you should open a new Feature request or bug issue in the module's issue queue where you can share your hack as patch to the module. This way:

  • Other people can evaluate and discuss your code and solution pointing out any mistakes or bugs you are not aware of, improving your changes.
  • Maintainers can commit your patch to the module so in next upgrade you don't lose your changes as changes are included in the new release.
  • Any ohter module's user can benefit from the new feature.

The drawbacks is that your code must fit the general usage of the module, so your feature should not break others module's functionalities or usages. This may imply more work but benefits are good enough.

  • 1
    I missed this part completely +1 :) – arpitr Jan 12 '15 at 18:44
  • Your drawback is not so much a drawback because you really should try to keep your code up to the coding standards and not to break things even if you are doing a quick hack. – rooby Jan 12 '15 at 23:14
  • Yes, indeed you are right, rooby. I've edited answer to state that implies more work but it's worth it because of the benefits. – sanzante Jan 13 '15 at 18:53
4

What I usually do, if I am hacking a module and the change is quite small, I create a patch. Patch is simply a difference of two states of code, giving you clear picture of what changes were made. Different ways can be used to create a patch. You can use the patch tool or you can also use git.

Keeping all patches file under sites/all/patches always help in quickly getting the alteration back in working after any code update. A must read by Angie will give you more insight on this.

In case if I am hacking a module and changes are heavy enough, in that case I keep such module in custom directory, being aware of that I am not interested in update path for such module. This is personal choice though :)

3

The Drupal hook system allows you to hook into other modules, even if they don't have an API of their own.

For example, every mail that you send with Drupal can be changed with hook_mail_alter.

Now your problem in specific, even has a module that changes the mailer to PHPMailer: https://www.drupal.org/project/phpmailer . The Drupal 7 version is still in dev, but even if it doesn't work like you want, you can learn a lot from the code. This module does not require you to hack core to change the mailer.

Another module you might be interested in is https://www.drupal.org/project/smtp. It allows you to send mails with an external SMTP (like GMail), if you are having trouble sending mail from your the webserver.

  • Thank you Maarten. Now that I have the code working, will save a copy, and will try the phpmailer module. Surely, as you indicate, it is the most convenient way of proceed rather than making a hook. – Cesar Jan 13 '15 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.