Skip to content

Commit

Permalink
create configuration settings to disable pre/post hooks per entity ty…
Browse files Browse the repository at this point in the history
…pe (#514)
  • Loading branch information
jackrabbithanna authored Jan 15, 2025
1 parent 55c5af2 commit e6b2acf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
12 changes: 12 additions & 0 deletions civicrm_entity.module
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ function civicrm_entity_civicrm_pre($op, $objectName, $id, &$params) {
return;
}

// Disable hook per entity type settings.
$disable_hooks_per_type = \Drupal::config('civicrm_entity.settings')->get('disable_hooks_per_type');
if (!empty($disable_hooks_per_type[$entityType])) {
return;
}

/** @var \Drupal\civicrm_entity\CiviEntityStorage $storage */
$storage = \Drupal::entityTypeManager()->getStorage($entityType);

Expand Down Expand Up @@ -545,6 +551,12 @@ function civicrm_entity_civicrm_post($op, $objectName, $objectId, &$objectRef) {
return;
}

// Disable hook per entity type settings.
$disable_hooks_per_type = \Drupal::config('civicrm_entity.settings')->get('disable_hooks_per_type');
if (!empty($disable_hooks_per_type[$entityType])) {
return;
}

/** @var \Drupal\civicrm_entity\CiviEntityStorage $storage */
$storage = \Drupal::entityTypeManager()->getStorage($entityType);

Expand Down
1 change: 1 addition & 0 deletions config/install/civicrm_entity.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ enabled_entity_types: { }
disable_hooks: FALSE
disable_links: FALSE
enable_links_per_type: { }
disable_hooks_per_type: { }
6 changes: 6 additions & 0 deletions config/schema/civicrm_entity.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ civicrm_entity.settings:
disable_links:
type: boolean
label: 'Disable Drupal pages'
disable_hooks_per_type:
type: sequence
label: 'Disable pre/post hooks per entity type'
sequence:
type: integer
label: 'Entity type disabled'
28 changes: 22 additions & 6 deletions src/Form/CivicrmEntitySettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {

$enabled_entity_types = $config->get('enabled_entity_types') ?? [];
$enable_links_per_type = $config->get('enable_links_per_type') ?? [];
$disable_hooks_per_type = $config->get('disable_hooks_per_type') ?? [];
foreach ($civicrm_entity_types as $key => $entity_info) {
$form['enabled_entity_types'][$key]['#type'] = 'fieldset';

Expand Down Expand Up @@ -192,20 +193,34 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#open' => FALSE,
];

$form['advanced_settings']['disable_links'] = [
'#type' => 'checkbox',
'#title' => $this->t('Disable Drupal pages'),
'#default_value' => $config->get('disable_links'),
'#description' => $this->t('Globally disables Drupal versions of view page and, add, edit, and delete forms for all enabled entity types. This option overrides the "per type" Drupal pages.'),
];

$form['advanced_settings']['disable_hooks'] = [
'#type' => 'checkbox',
'#title' => $this->t('Disable pre/post hooks'),
'#default_value' => $config->get('disable_hooks'),
'#description' => $this->t('Not intended for normal use. Provided to temporarily disable Drupal entity hooks for CiviCRM Entity types for special cases, such as migrations. Only disable if you know you need to.'),
];

$form['advanced_settings']['disable_links'] = [
'#type' => 'checkbox',
'#title' => $this->t('Disable Drupal pages'),
'#default_value' => $config->get('disable_links'),
'#description' => $this->t('Globally disables Drupal versions of view page and, add, edit, and delete forms for all enabled entity types. This option overrides the "per type" Drupal pages.'),
$form['advanced_settings']['disable_hooks_per_type'] = [
'#type' => 'fieldset',
'#title' => $this->t('Disable pre/post hooks per entity type'),
'#description' => $this->t('Pre/post hooks are used by Rules integration, or anytime Drupal CRUD hooks are required for an entity type. They have a performance cost though. Disable pre/post hooks per entity type here.'),
'#open' => FALSE,
'#tree' => TRUE,
];

foreach ($civicrm_entity_types as $key => $entity_info) {
$form['advanced_settings']['disable_hooks_per_type'][$key] = [
'#type' => 'checkbox',
'#title' => $entity_info['civicrm entity label'],
'#default_value' => !empty($disable_hooks_per_type[$key]) ?? 0,
];
}
return $form;
}

Expand All @@ -229,6 +244,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('disable_hooks', $form_state->getValue('disable_hooks'))
->set('disable_links', $form_state->getValue('disable_links'))
->set('enable_links_per_type', $enable_links_per_type)
->set('disable_hooks_per_type', $form_state->getValue('disable_hooks_per_type'))
->save();

// Need to rebuild derivative routes.
Expand Down

0 comments on commit e6b2acf

Please sign in to comment.