Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre and post hooks to Domain create and edit operations #15104

Merged
merged 1 commit into from
Aug 23, 2019

Conversation

christianwach
Copy link
Member

Overview

Fixes this issue on Lab.

Before

There is no way detect when a new Domain has been created or when a Domain has been edited.

After

One can detect when a new Domain has been created or when a Domain has been edited with the usual hook_civicrm_pre and hook_civicrm_post callbacks.

Technical Details

Sample code to verify:

/**
 * Implements hook_civicrm_config().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
 */
function mytest_civicrm_config(&$config) {

  // Hook into pre.
  Civi::service('dispatcher')->addListener(
    'hook_civicrm_pre',
    'mytest_civicrm_hook_callback',
    -100 // Default priority.
  );

  // Hook into post.
  Civi::service('dispatcher')->addListener(
    'hook_civicrm_post',
    'mytest_civicrm_hook_callback',
    -100 // Default priority.
  );

}

/**
 * Testing "hook_civicrm_pre" and  "hook_civicrm_post" callbacks.
 *
 * Implements hook_civicrm_pre() and hook_civicrm_post() via Symfony hook system.
 *
 * @param object $event The event object.
 * @param string $hook The hook name.
 */
function mytest_civicrm_hook_callback($event, $hook) {

  // Extract args for this hook.
  $params = $event->getHookValues();

  // Get values.
  $op = $params[0];
  $objectName = $params[1];
  
  // Bail if not of type "Domain".
  if ($objectName !== 'Domain') {
    return;
  }
  
  // Further values.
  $objectId = $params[2];
  $objectRef = $params[3];

  error_log( print_r( array(
    'method' => __METHOD__,
    'op' => $op,
    'objectName' => $objectName,
    'objectId' => $objectId,
    'objectRef' => $objectRef,
    'hook' => $hook,
  ), true ) );

}

Creating a new Domain now produces the following in my logs:


[22-Aug-2019 18:37:51 Europe/London] Array
(
    [method] => mytest_civicrm_hook_callback
    [op] => create
    [objectName] => Domain
    [objectId] => 
    [objectRef] => Array
        (
            [name] => My New Domain
            [domain_version] => 5.x
            [page] => CiviCRM
            [noheader] => 1
            [prettyprint] => 1
            [check_permissions] => 1
            [version] => 5.x
        )

    [hook] => hook_civicrm_pre
)

[22-Aug-2019 18:37:51 Europe/London] Array
(
    [method] => mytest_civicrm_hook_callback
    [op] => create
    [objectName] => Domain
    [objectId] => 5
    [objectRef] => CRM_Core_DAO_Domain Object
        (
            [id] => 5
            [name] => My New Domain
            [description] => 
            [config_backend] => 
            [version] => 5.16.2
            [contact_id] => 
            [locales] => 
            [locale_custom_strings] => 
            [resultCopies:protected] => 0
            [_options:protected] => Array
                (
                )

            [_DB_DataObject_version] => 1.8.12
            [__table] => civicrm_domain
            [N] => 0
            [_database_dsn] => 
            [_database_dsn_md5] => daac48f49d8f8ae4779f01eeb16dd31b
            [_database] => my_db_name
            [_query] => Array
                (
                    [condition] => 
                    [group_by] => 
                    [order_by] => 
                    [having] => 
                    [limit_start] => 
                    [limit_count] => 
                    [data_select] => *
                )

            [_DB_resultid] => 
            [_resultFields] => 
            [_link_loaded] => 
            [_join] => 
            [_lastError] => 
        )

    [hook] => hook_civicrm_post
)

I've skipped showing what happens when a Domain is edited since it's basically the same output -- this can easily be verified via the API Explorer.

@civibot
Copy link

civibot bot commented Aug 22, 2019

(Standard links)

@civibot civibot bot added the master label Aug 22, 2019
@eileenmcnaughton eileenmcnaughton merged commit 003c357 into civicrm:master Aug 23, 2019
@christianwach christianwach deleted the lab-1203 branch March 8, 2020 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants