Skip to content

Instantly share code, notes, and snippets.

@mickadoo
Created August 29, 2018 13:02
Show Gist options
  • Save mickadoo/63821c788ac138e96d545a661f66cc46 to your computer and use it in GitHub Desktop.
Save mickadoo/63821c788ac138e96d545a661f66cc46 to your computer and use it in GitHub Desktop.
Overview of how to log webform
<?php
// Inside civihr_employee_portal.module..
// this will enable logging before data is saved
function civihr_employee_portal_webform_submission_presave($node, &$submission) {
// todo restrict to certain nodes
$logger = Civi::container()->get(ApiChangelogWrapper::class);
$logger->setIsActive(TRUE);
}
// this will just set up the wrapper
function civihr_employee_portal_civicrm_apiWrappers(&$wrappers, $apiRequest) {
// This is a temp solution, will change to container hook
if (!Civi::container()->has(ApiChangelogWrapper::class)) {
Civi::container()->set(ApiChangelogWrapper::class, new ApiChangelogWrapper());
}
// We could also separate the wrapper from the logger (i.e. the wrapper would just call the logger service)
$wrapper = Civi::container()->get(ApiChangelogWrapper::class);
$wrappers[] = $wrapper;
}
// disable logging after inserting data, we have access to all requests here
function civihr_employee_portal_webform_submission_insert($node, $submission) {
$logger = Civi::container()->get(ApiChangelogWrapper::class);
$logger->setIsActive(FALSE);
}
// Inside the wrapper (abbreviated)
class ApiChangelogWrapper implements \API_Wrapper {
/**
* Logs the API request if logging is enabled
*
* @inheritdoc
*/
public function fromApiInput($apiRequest) {
if ($this->isActive) {
// Just a sample filter, these should be configurable class properties
$filter = function ($apiRequest) {
return $apiRequest['action'] === 'create';
};
if ($filter($apiRequest)) {
$this->changes[] = $apiRequest;
}
// this is just a sample where we store the request, we could
// fetch the existing value here and store the changelog
}
return $apiRequest;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment