You are here

class ProfileSubscriber in Commerce Shipping 8.2

Hierarchy

  • class \Drupal\commerce_shipping\EventSubscriber\ProfileSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ProfileSubscriber

1 string reference to 'ProfileSubscriber'
commerce_shipping.services.yml in ./commerce_shipping.services.yml
commerce_shipping.services.yml
1 service uses ProfileSubscriber
commerce_shipping.profile_subscriber in ./commerce_shipping.services.yml
Drupal\commerce_shipping\EventSubscriber\ProfileSubscriber

File

src/EventSubscriber/ProfileSubscriber.php, line 8

Namespace

Drupal\commerce_shipping\EventSubscriber
View source
class ProfileSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      'commerce_order.profiles' => [
        'onProfiles',
      ],
    ];
  }

  /**
   * Adds the shipping profile to the order profiles.
   *
   * The shipping profile is assumed to be the same for all shipments.
   *
   * @param \Drupal\commerce_order\Event\OrderProfilesEvent $event
   *   The order profiles event.
   */
  public function onProfiles(OrderProfilesEvent $event) {
    $order = $event
      ->getOrder();
    if (!$order
      ->hasField('shipments')) {
      return;
    }

    /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
    $shipments = $order
      ->get('shipments')
      ->referencedEntities();
    $shipment = reset($shipments);
    if ($shipment && $shipment
      ->getShippingProfile()) {
      $event
        ->addProfile('shipping', $shipment
        ->getShippingProfile());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProfileSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ProfileSubscriber::onProfiles public function Adds the shipping profile to the order profiles.