You are here

public function Subscriber::postCreate in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Entity/Subscriber.php \Drupal\simplenews\Entity\Subscriber::postCreate()
  2. 3.x src/Entity/Subscriber.php \Drupal\simplenews\Entity\Subscriber::postCreate()

Acts on a created entity before hooks are invoked.

Used after the entity is created, but before saving the entity and before any of the presave hooks are invoked.

See the Entity CRUD topic for more information.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

Overrides ContentEntityBase::postCreate

See also

\Drupal\Core\Entity\EntityInterface::create()

File

src/Entity/Subscriber.php, line 298

Class

Subscriber
Defines the simplenews subscriber entity.

Namespace

Drupal\simplenews\Entity

Code

public function postCreate(EntityStorageInterface $storage) {
  parent::postCreate($storage);

  // Set the uid field if there is a user with the same email.
  $user_ids = \Drupal::entityQuery('user')
    ->condition('mail', $this
    ->getMail())
    ->execute();
  if (!empty($user_ids)) {
    $this
      ->setUserId(array_pop($user_ids));
  }

  // Copy values for shared fields from existing user.
  if (\Drupal::config('simplenews.settings')
    ->get('subscriber.sync_fields') && ($user = $this
    ->getUser())) {
    foreach ($this
      ->getUserSharedFields($user) as $field_name) {
      $this
        ->set($field_name, $user
        ->get($field_name)
        ->getValue());
    }
  }
}