Skip to content

Commit

Permalink
Add hooks. (#515)
Browse files Browse the repository at this point in the history
* Add hooks.

* Convert procedural hooks to OOP.

* Coding standards.

* Add hooks on services.

* Use autowiring.

* Add alias to service for autowiring.

* Minor update.

* Do not autowire config.

* Coding standards.
  • Loading branch information
puresyntax71 authored Jan 28, 2025
1 parent 54e1af4 commit c454fcc
Show file tree
Hide file tree
Showing 7 changed files with 628 additions and 377 deletions.
405 changes: 28 additions & 377 deletions civicrm_entity.module

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions civicrm_entity.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ services:
class: 'Drupal\civicrm_entity\CiviCrmApi'
arguments: ['@civicrm']

Drupal\civicrm_entity\CiviCrmApiInterface: '@civicrm_entity.api'

civicrm_entity.field_definition_provider:
class: Drupal\civicrm_entity\Entity\FieldDefinitionProvider

Expand Down Expand Up @@ -37,3 +39,23 @@ services:
class: Drupal\civicrm_entity\EventSubscriber\SearchApiSubscriber
tags:
- { name: event_subscriber }

Drupal\civicrm_entity\Hook\EntityAlterHooks:
class: Drupal\civicrm_entity\Hook\EntityAlterHooks
autowire: true

Drupal\civicrm_entity\Hook\EntityHooks:
class: Drupal\civicrm_entity\Hook\EntityHooks
autowire: true

Drupal\civicrm_entity\Hook\FormHooks:
class: Drupal\civicrm_entity\Hook\FormHooks
autowire: true

Drupal\civicrm_entity\Hook\QueryHooks:
class: Drupal\civicrm_entity\Hook\QueryHooks
autowire: true

Drupal\civicrm_entity\Hook\RulesHooks:
class: Drupal\civicrm_entity\Hook\RulesHooks
autowire: true
88 changes: 88 additions & 0 deletions src/Hook/EntityAlterHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Drupal\civicrm_entity\Hook;

use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;

/**
* Hook implementations for entity alters.
*/
class EntityAlterHooks {

/**
* Constructor for EntityHooks.
*/
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityDisplayRepositoryInterface $entityDisplayRepository,
protected ModuleHandlerInterface $moduleHandler,
) {
}

/**
* Implements hook_entity_view_display_alter().
*
* There is no way to handle this in the entity type's view build.
*/
#[Hook('entity_view_display_alter')]
public function entityViewDisplayAlter(EntityViewDisplayInterface $display, array $context): void {
$entity_type = $this->entityTypeManager->getDefinition($context['entity_type']);
assert($entity_type !== NULL);
if ($entity_type->get('civicrm_entity') && $entity_type->hasKey('bundle')) {
$entity_display_repository = $this->entityDisplayRepository;
assert($entity_display_repository instanceof EntityDisplayRepositoryInterface);
$entity_view_mode_ids = array_keys($entity_display_repository->getViewModeOptions($entity_type->id()));
$view_mode = !empty($context['view_mode']) && in_array($context['view_mode'], $entity_view_mode_ids) ? $context['view_mode'] : $entity_display_repository::DEFAULT_DISPLAY_MODE;
$root_display = $entity_display_repository->getViewDisplay(
$entity_type->id(),
$entity_type->id(),
$view_mode
);
$display->set('content', $root_display->get('content'));
$display->set('hidden', $root_display->get('hidden'));

if ($root_display instanceof LayoutBuilderEntityViewDisplay) {
$layout_builder_settings = $root_display->getThirdPartySettings('layout_builder');
foreach ($layout_builder_settings as $setting_key => $setting) {
$display->setThirdPartySetting('layout_builder', $setting_key, $setting);
}
}
$ds_settings = $root_display->getThirdPartySettings('ds');
if (!empty($ds_settings) && is_array($ds_settings)) {
foreach ($ds_settings as $setting_key => $setting) {
$display->setThirdPartySetting('ds', $setting_key, $setting);
}
}
}
}

/**
* Implements hook_entity_view_alter().
*/
#[Hook('entity_view_alter')]
public function entityViewAlter(array &$build, EntityInterface $entity, EntityDisplayInterface $display): void {
$entity_type = $entity->getEntityType();
if ($entity_type->get('civicrm_entity') && $entity_type->hasKey('bundle') && $this->moduleHandler->moduleExists('field_group')) {
$entity_display_repository = $this->entityDisplayRepository;
$entity_view_mode_ids = array_keys($entity_display_repository->getViewModeOptions($entity_type->id()));

$context = [
'entity_type' => $display->getTargetEntityTypeId(),
'bundle' => $entity_type->id(),
'entity' => $entity,
'display_context' => 'view',
'mode' => in_array($display->getMode(), $entity_view_mode_ids) ? $display->getMode() : $entity_display_repository::DEFAULT_DISPLAY_MODE,
];

field_group_attach_groups($build, $context);
}
}

}
Loading

0 comments on commit c454fcc

Please sign in to comment.