- πΊπΈUnited States justclint
We are also using domain access https://www.drupal.org/project/domain β and trying to restrict specific bundle types to domains.
We've been able to add new notification bundles with custom domain data field but seems like all the conditional logic is in DmbNotificationService.php.
Is there a recommended approach to adding custom viewing conditions per bundle type?
Thanks!
- πΊπΈUnited States justclint
I was able to get this done with a service decoration.
mymodule.service.yml
dismissible_message_bar.notification_service.decorate: class: '\Drupal\mymodule\Service\DmbNotificationDecorateService' decorates: dismissible_message_bar.notification_service decoration_priority: 9 public: false arguments: ['@dismissible_message_bar.notification_service.decorate.inner', '@domain.negotiator', '@path.current', '@entity_type.manager', '@request_stack', '@renderer', '@language_manager', '@path.matcher', '@path_alias.manager']
mymodule/src/Service/DmbNotificationDecorateService.php
namespace Drupal\mymodule\Service; use Drupal\dismissible_message_bar\Service\DmbNotificationService; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\path_alias\AliasManagerInterface; use Drupal\Core\Path\CurrentPathStack; use Drupal\Core\Path\PathMatcherInterface; use Drupal\Core\Render\Renderer; use Drupal\domain\DomainNegotiator; use Symfony\Component\HttpFoundation\RequestStack; /** * Modifies the Dmb Notifications service. */ class DmbNotificationDecorateService extends DmbNotificationService { /** * Original service object. * * @var \Drupal\dismissible_message_bar\Service\DmbNotificationService */ protected $originalService; /** * Domain negotiator. * * @var \Drupal\domain\DomainNegotiator */ protected $domainNegotiator; /** * DmbNotificationDecorateService constructor. * * @param \Drupal\dismissible_message_bar\Service\DmbNotificationService $original_service * Service to decorate. * @param \Drupal\domain\DomainNegotiator $domain_negotiator * Domain access. * @param \Drupal\Core\Path\CurrentPathStack $current_path_stack * Current path stack. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * Entity type manager. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * Request Stack. * @param \Drupal\Core\Render\Renderer $renderer * Renderer service. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * Language manager service. * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher * Path matcher service. * @param \Drupal\path_alias\AliasManagerInterface $alias_manager * Alias manager service. */ public function __construct( DmbNotificationService $original_service, DomainNegotiator $domain_negotiator, CurrentPathStack $current_path_stack, EntityTypeManagerInterface $entity_type_manager, RequestStack $request_stack, Renderer $renderer, LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher, AliasManagerInterface $alias_manager ) { $this->originalService = $original_service; $this->domainNegotiator = $domain_negotiator; parent::__construct( $current_path_stack, $entity_type_manager, $request_stack, $renderer, $language_manager, $path_matcher, $alias_manager ); } /** * Returns array of all notifications. * * @return array * Notification values to insert into drupal settings. */ public function returnAllNotifications():array { $parentNotifications = parent::returnAllNotifications(); $result = []; if (!empty($parentNotifications)) { foreach ($parentNotifications as $parentNotification) { $display = TRUE; $notification = $this->entityTypeManager->getStorage('dmb_notifications_entity')->load($parentNotification['id']); // Handle domains. if ($notification->hasField('field_domain_access') && !$notification->get('field_domain_access')->isEmpty()) { $field_domain_access = $notification->field_domain_access->getValue(); $enabled_domains = []; foreach ($field_domain_access as $domain_array) { $enabled_domains[] = $domain_array['target_id']; } $getActiveDomain = $this->domainNegotiator->getActiveDomain(); $currentDomainId = $getActiveDomain->id(); if (!in_array($currentDomainId, $enabled_domains)) { $display = FALSE; } } if ($display) { $result[$parentNotification['id']] = $parentNotification; } } } return $result; } }