class CartEventSubscriber in Commerce Core 8.2
Same name in this branch
- 8.2 modules/log/src/EventSubscriber/CartEventSubscriber.php \Drupal\commerce_log\EventSubscriber\CartEventSubscriber
- 8.2 modules/cart/src/EventSubscriber/CartEventSubscriber.php \Drupal\commerce_cart\EventSubscriber\CartEventSubscriber
- 8.2 modules/promotion/src/EventSubscriber/CartEventSubscriber.php \Drupal\commerce_promotion\EventSubscriber\CartEventSubscriber
Hierarchy
- class \Drupal\commerce_cart\EventSubscriber\CartEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of CartEventSubscriber
1 string reference to 'CartEventSubscriber'
- commerce_cart.services.yml in modules/
cart/ commerce_cart.services.yml - modules/cart/commerce_cart.services.yml
1 service uses CartEventSubscriber
File
- modules/
cart/ src/ EventSubscriber/ CartEventSubscriber.php, line 13
Namespace
Drupal\commerce_cart\EventSubscriberView source
class CartEventSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a new CartEventSubscriber 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 = [
CartEvents::CART_ENTITY_ADD => 'displayAddToCartMessage',
];
return $events;
}
/**
* Displays an add to cart message.
*
* @param \Drupal\commerce_cart\Event\CartEntityAddEvent $event
* The add to cart event.
*/
public function displayAddToCartMessage(CartEntityAddEvent $event) {
$this->messenger
->addMessage($this
->t('@entity added to <a href=":url">your cart</a>.', [
'@entity' => $event
->getEntity()
->label(),
':url' => Url::fromRoute('commerce_cart.page')
->toString(),
]));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CartEventSubscriber:: |
protected | property | The messenger. | |
CartEventSubscriber:: |
public | function | Displays an add to cart message. | |
CartEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
CartEventSubscriber:: |
public | function | Constructs a new CartEventSubscriber object. | |
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. |