You are here

public static function Invite::preCreate in Invite 8

Changes the values of an entity before it is created.

Load defaults for example.

Parameters

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

mixed[] $values: An array of values to set, keyed by property name. If the entity type has bundles the bundle key has to be specified.

Overrides EntityBase::preCreate

File

src/Entity/Invite.php, line 68

Class

Invite
Defines the Invite entity.

Namespace

Drupal\invite\Entity

Code

public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
  parent::preCreate($storage_controller, $values);

  // Generate unique registration code.
  do {
    $reg_code = user_password(10);
    $result = Database::getConnection()
      ->query('SELECT reg_code FROM {invite} WHERE reg_code = :reg_code', [
      ':reg_code' => $reg_code,
    ])
      ->fetchField();
  } while ($result !== FALSE);
  $values += [
    'user_id' => \Drupal::currentUser()
      ->id(),
    'created' => \Drupal::time()
      ->getRequestTime(),
    'expires' => \Drupal::time()
      ->getRequestTime() + \Drupal::config('invite.invite_config')
      ->get('invite_expiration') * 24 * 60 * 60,
    'invitee' => 0,
    'type' => !empty($values['type']) ? $values['type'] : '',
    'status' => 1,
    'reg_code' => $reg_code,
    'data' => [],
  ];
}