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
- class \Drupal\commerce_wishlist\EventSubscriber\WishlistEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of WishlistEventSubscriber
1 string reference to 'WishlistEventSubscriber'
1 service uses WishlistEventSubscriber
File
- src/
EventSubscriber/ WishlistEventSubscriber.php, line 18
Namespace
Drupal\commerce_wishlist\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WishlistEventSubscriber:: |
protected | property | The messenger. | |
WishlistEventSubscriber:: |
public | function | Displays an add to wishlist message. | |
WishlistEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
WishlistEventSubscriber:: |
public | function | Constructs a new WishlistEventSubscriber object. |