1

I would to ask you , on how can create an event (programatically) when I change the external ID in contact form, how can I t make some kind of process like validation in another module or service I know I can create something like a hook but I dont know how to start working with hooks in civiccrm, is there any document, book ,link, video, note where I can learn about it? I would really appreciate this, thank you in advance.

2
  • Hi Aneury, are you comfortable with hooks (actions & filters) in WordPress? If so, I can help you do this in a WordPress plugin. Dec 5, 2019 at 14:37
  • I'm not comfortable with hooks yet, but it could be! let me know how to proceed with this, please. Dec 6, 2019 at 15:58

1 Answer 1

1

Create an extension

The first step to create a hook is to create an extension. I'd recommend installing civix which can generate an empty extension by simply typing the command :

civix generate:module myextension

Go check https://docs.civicrm.org/dev/en/latest/extensions/ for more details about extensions.

All you need to know for now is that civix will create a file called myextension.php where you can put your hooks. There are a few hooks commented as examples in the generated files.

Create the hook

A hook is simply a function that start with the name of your extension and finish with the name of the hook. For example hook_civicrm_post will be named myextension_civicrm_post in the extension we've just created.

In your case, I believe hook_civicrm_post is the one you want to use but you can see the documentation about the hooks in :

5
  • If they want to validate the external id then hook_civicrm_validateForm might be a better fit than _post.
    – Demerit
    Dec 4, 2019 at 15:50
  • Yes, depends on what the OP wants. If it's a kind of sync that is needed and that needs to work with API calls, then _post is probably the way to go. If it's for UI validation, then validateForm might be better.
    – samuelsov
    Dec 5, 2019 at 21:06
  • I'm reading about it, is interesting but how or where should I add my function callback with the singleton Civi::dispatcher()>addListener? for me is not clear this part yet, it could be directly in my extesion? it would be loaded when somebody request something related to my custom extension? Dec 6, 2019 at 16:03
  • You probably don't need a dispatcher as the standard hooks will be automatically called if properly named in your (activated) extension. In some specific case, you might need to use the addListener like so : Civi::service('dispatcher')->addListener('hook_civicrm_pageRun', 'reorderdashboard_symfony_civicrm_pageRun', -255); See also : github.com/civicrm/org.civicrm.shoreditch/pull/316/files
    – samuelsov
    Dec 11, 2019 at 22:04
  • Can hooks only be used by extensions? Is there a way to use hooks from within my theme's functions.php file for example?
    – dsmith63
    Dec 17, 2021 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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