You are here

class AddressEventsSubscriber in Commerce Demo 8

Hierarchy

  • class \Drupal\commerce_demo\EventSubscriber\AddressEventsSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AddressEventsSubscriber

1 string reference to 'AddressEventsSubscriber'
commerce_demo.services.yml in ./commerce_demo.services.yml
commerce_demo.services.yml
1 service uses AddressEventsSubscriber
commerce_demo.address_events_subscriber in ./commerce_demo.services.yml
Drupal\commerce_demo\EventSubscriber\AddressEventsSubscriber

File

src/EventSubscriber/AddressEventsSubscriber.php, line 9

Namespace

Drupal\commerce_demo\EventSubscriber
View source
class AddressEventsSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AddressEvents::INITIAL_VALUES][] = [
      'onInitialValues',
    ];
    return $events;
  }

  /**
   * Alters the initial values.
   *
   * @param \Drupal\address\Event\InitialValuesEvent $event
   *   The initial values event.
   */
  public function onInitialValues(InitialValuesEvent $event) {
    if ($event
      ->getFieldDefinition()
      ->getTargetEntityTypeId() == 'profile' && $event
      ->getFieldDefinition()
      ->getTargetBundle() == 'customer') {
      $new_values = $this
        ->getRandomAddress() + $event
        ->getInitialValues();
      $event
        ->setInitialValues($new_values);
    }
  }

  /**
   * Gets a random address value.
   *
   * @return array
   */
  protected function getRandomAddress() {
    $addresses = [
      [
        'given_name' => 'Celia',
        'family_name' => 'Engeseth',
        'address_line1' => '8502 Pilgrim St.',
        'locality' => 'Mokena',
        'administrative_area' => 'IL',
        'postal_code' => '60448',
      ],
      [
        'given_name' => 'Josefine',
        'family_name' => 'Christiansen',
        'address_line1' => '7691 East 6th St',
        'locality' => 'Lewiston',
        'administrative_area' => 'ME',
        'postal_code' => '04240',
      ],
      [
        'given_name' => 'Oliver',
        'family_name' => 'Mantyla',
        'address_line1' => '315 Addison Court ',
        'locality' => 'New Windsor',
        'administrative_area' => 'NY',
        'postal_code' => '12553',
      ],
      [
        'given_name' => 'Heinz Dieter',
        'family_name' => 'Reinke',
        'address_line1' => '45 Bow Ridge Ave',
        'locality' => 'West Chicago',
        'administrative_area' => 'IL',
        'postal_code' => '60185',
      ],
    ];
    return $addresses[array_rand($addresses)];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddressEventsSubscriber::getRandomAddress protected function Gets a random address value.
AddressEventsSubscriber::getSubscribedEvents public static function
AddressEventsSubscriber::onInitialValues public function Alters the initial values.