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'
1 service uses AddressEventsSubscriber
File
- src/
EventSubscriber/ AddressEventsSubscriber.php, line 9
Namespace
Drupal\commerce_demo\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddressEventsSubscriber:: |
protected | function | Gets a random address value. | |
AddressEventsSubscriber:: |
public static | function | ||
AddressEventsSubscriber:: |
public | function | Alters the initial values. |