You are here

class CartEventSubscriber in Commerce Core 8.2

Same name in this branch
  1. 8.2 modules/log/src/EventSubscriber/CartEventSubscriber.php \Drupal\commerce_log\EventSubscriber\CartEventSubscriber
  2. 8.2 modules/cart/src/EventSubscriber/CartEventSubscriber.php \Drupal\commerce_cart\EventSubscriber\CartEventSubscriber
  3. 8.2 modules/promotion/src/EventSubscriber/CartEventSubscriber.php \Drupal\commerce_promotion\EventSubscriber\CartEventSubscriber

Hierarchy

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
commerce_cart.cart_subscriber in modules/cart/commerce_cart.services.yml
Drupal\commerce_cart\EventSubscriber\CartEventSubscriber

File

modules/cart/src/EventSubscriber/CartEventSubscriber.php, line 13

Namespace

Drupal\commerce_cart\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
CartEventSubscriber::$messenger protected property The messenger.
CartEventSubscriber::displayAddToCartMessage public function Displays an add to cart message.
CartEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CartEventSubscriber::__construct public function Constructs a new CartEventSubscriber object.
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.