You are here

class MySmsEventSubscriber in SMS Framework 8

Same name and namespace in other branches
  1. 2.x sms.api.php \MySmsEventSubscriber
  2. 2.1.x sms.api.php \MySmsEventSubscriber

Event subscribers for SMS Framework.

Service definition: <code> ```yaml my_module.my_event_subscriber: class: Drupal\my_module\EventSubscriber\MySmsEventSubscriber tags:

  • { name: event_subscriber }

``` </code>

<code> <?php namespace Drupal\my_module\EventSubscriber; ?> </code>

Hierarchy

Expanded class hierarchy of MySmsEventSubscriber

See also

\Drupal\sms_test\EventSubscriber\SmsTestEventSubscriber

File

./sms.api.php, line 43
SMS Framework hooks.

View source
class MySmsEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface {

  /**
   * An example event subscriber.
   *
   * @see \Drupal\sms\Event\SmsEvents::ENTITY_PHONE_NUMBERS
   */
  public function myEntityPhoneNumbers(\Drupal\sms\Event\SmsEntityPhoneNumber $event) {

    // Entity to get phone numbers for.
    $entity = $event
      ->getEntity();

    // Add a phone number for above entity.
    $event
      ->addPhoneNumber('+123456879');
  }

  /**
   * An example event subscriber.
   *
   * @see \Drupal\sms\Event\SmsEvents::MESSAGE_PRE_PROCESS
   */
  public function mySmsMessagePreprocess(\Drupal\sms\Event\SmsMessageEvent $event) {
    $result = [];
    foreach ($event
      ->getMessages() as $message) {

      // Modify or chunk messages.
      $result[] = $message;
    }
    $event
      ->setMessages($result);
  }

  /**
   * An example event subscriber.
   *
   * @see \Drupal\sms\Event\SmsEvents::MESSAGE_POST_PROCESS
   */
  public function mySmsMessagePostProcess(\Drupal\sms\Event\SmsMessageEvent $event) {
    $result = [];
    foreach ($event
      ->getMessages() as $message) {

      // Modify or chunk messages.
      $result[] = $message;
    }
    $event
      ->setMessages($result);
  }

  /**
   * An example event subscriber.
   *
   * @see \Drupal\sms\Event\SmsEvents::MESSAGE_GATEWAY
   */
  public function mySmsMessageGateway(\Drupal\sms\Event\RecipientGatewayEvent $event) {

    // The recipient phone number.
    $event
      ->getRecipient();

    // Add a gateway for a phone number.
    $event
      ->addGateway($a_gateway);

    // Add a gateway with a priority.
    $event
      ->addGateway($a_gateway, 333);
    $event
      ->addGateway($a_gateway, -333);
  }

  /**
   * An example event subscriber.
   *
   * @see \Drupal\sms\Event\SmsEvents::DELIVERY_REPORT_POST_PROCESS
   */
  public function myDeliveryReportPostProcessor(\Drupal\sms\Event\SmsDeliveryReportEvent $event) {
    $event
      ->getReports();
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[\Drupal\sms\Event\SmsEvents::ENTITY_PHONE_NUMBERS][] = [
      'myEntityPhoneNumbers',
    ];
    $events[\Drupal\sms\Event\SmsEvents::MESSAGE_PRE_PROCESS][] = [
      'mySmsMessagePreprocess',
    ];
    $events[\Drupal\sms\Event\SmsEvents::MESSAGE_POST_PROCESS][] = [
      'mySmsMessagePostprocess',
    ];
    $events[\Drupal\sms\Event\SmsEvents::MESSAGE_GATEWAY][] = [
      'mySmsMessageGateway',
    ];
    $events[\Drupal\sms\Event\SmsEvents::DELIVERY_REPORT_POST_PROCESS][] = [
      'myDeliveryReportPostProcessor',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MySmsEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MySmsEventSubscriber::myDeliveryReportPostProcessor public function An example event subscriber.
MySmsEventSubscriber::myEntityPhoneNumbers public function An example event subscriber.
MySmsEventSubscriber::mySmsMessageGateway public function An example event subscriber.
MySmsEventSubscriber::mySmsMessagePostProcess public function An example event subscriber.
MySmsEventSubscriber::mySmsMessagePreprocess public function An example event subscriber.