You are here

class WishlistEventSubscriber in Commerce Wishlist 8.3

Defines the wishlist event subscriber.

On adding an item to wishlist, the "added to wishlist" message will be shown.

Hierarchy

Expanded class hierarchy of WishlistEventSubscriber

1 string reference to 'WishlistEventSubscriber'
commerce_wishlist.services.yml in ./commerce_wishlist.services.yml
commerce_wishlist.services.yml
1 service uses WishlistEventSubscriber
commerce_wishlist.wishlist_subscriber in ./commerce_wishlist.services.yml
Drupal\commerce_wishlist\EventSubscriber\WishlistEventSubscriber

File

src/EventSubscriber/WishlistEventSubscriber.php, line 18

Namespace

Drupal\commerce_wishlist\EventSubscriber
View source
class WishlistEventSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs a new WishlistEventSubscriber object.
   *
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   */
  public function __construct(MessengerInterface $messenger, TranslationInterface $string_translation) {
    $this->messenger = $messenger;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      WishlistEvents::WISHLIST_ENTITY_ADD => 'displayAddToWishlistMessage',
    ];
    return $events;
  }

  /**
   * Displays an add to wishlist message.
   *
   * @param \Drupal\commerce_wishlist\Event\WishlistEntityAddEvent $event
   *   The add to wishlist event.
   */
  public function displayAddToWishlistMessage(WishlistEntityAddEvent $event) {
    $purchased_entity = $event
      ->getEntity();
    $this->messenger
      ->addStatus($this
      ->t('@entity added to <a href=":url">your wishlist</a>.', [
      '@entity' => $purchased_entity
        ->label(),
      ':url' => Url::fromRoute('commerce_wishlist.page')
        ->toString(),
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WishlistEventSubscriber::$messenger protected property The messenger.
WishlistEventSubscriber::displayAddToWishlistMessage public function Displays an add to wishlist message.
WishlistEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
WishlistEventSubscriber::__construct public function Constructs a new WishlistEventSubscriber object.