Update DMB entity type to use Condition plugins to control visibility

Created on 2 November 2020, over 3 years ago
Updated 18 January 2024, 5 months ago

The visibility options available to this entity type seem to be very similar to what blocks use. It might be worth updating it to use the same Condition API so that other developers may use plugins to add custom conditions.

My use case for this request is that I'm using the Domain Access module to allow the same site to be accessed via multiple domains, and I would like each DMB entity to be restricted to a specific domain.

✨ Feature request
Status

Active

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States gantal

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡Έ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;
      }
    
    }
    
Production build 0.69.0 2024