You are here

function regcode_load_single in Registration codes 8

Same name and namespace in other branches
  1. 7.2 regcode.module \regcode_load_single()
  2. 7 regcode.module \regcode_load_single()

Loads a registration code.

@example regcode_load(1231); // Loads the regcode with rid=1231 regcode_load(NULL, ['code'=>'foobar']); // Loads the "foobar" regcode

Parameters

int|null $id: The database primary key (rid).

array $conditions: An associative array containing the search conditions.

Return value

object|bool The regcode object or FALSE if the code does not exist.

1 call to regcode_load_single()
regcode_code_validate in ./regcode.module
Validates a regcode.

File

./regcode.module, line 170
Main functionality and hooks of regcode module.

Code

function regcode_load_single($id, $conditions = []) {

  // Build the query.
  $query = \Drupal::database()
    ->select('regcode')
    ->fields('regcode', [
    'rid',
    'uid',
    'created',
    'lastused',
    'begins',
    'expires',
    'code',
    'is_active',
    'maxuses',
    'uses',
  ])
    ->range(0, 1);

  // Allow mixed search parameters.
  if (!empty($id)) {
    $query
      ->condition('rid', $id);
  }
  else {
    foreach ($conditions as $field => $value) {
      $query
        ->condition($field, $value);
    }
  }

  // Run the query and grab a single regcode.
  $regcode = $query
    ->execute()
    ->fetchObject();
  if (!$regcode) {
    return FALSE;
  }

  // Entity loaders expect arrays of objects. entity_load() and
  // this function both invoke the hook below.
  $reg_codes = [
    $regcode->rid => $regcode,
  ];
  \Drupal::moduleHandler()
    ->invokeAll('regcode_load', [
    $reg_codes,
  ]);
  return $reg_codes[$regcode->rid];
}