You are here

function token_custom_load_multiple in Custom Tokens 7

Same name and namespace in other branches
  1. 7.2 token_custom.module \token_custom_load_multiple()

Loads an array of tokens from the database.

Maintains a static cache with the tokens already loaded to avoid unnecessary queries.

Parameters

$names: An array containing the machine names of the tokens to return. If none, then loads and returns all the tokens.

Return value

array An array of token objects, keyed by the token's machine name.

4 calls to token_custom_load_multiple()
token_custom_list_page in ./token_custom.admin.inc
Callback for the token_custom admin list page
token_custom_load in ./token_custom.module
Loads an individual token from the database.
token_custom_tokens in ./token_custom.module
Implements hook_tokens().
token_custom_token_info in ./token_custom.module
Implements hook_token_info().

File

./token_custom.module, line 205
It gives the user the ability to create custom tokens using PHP code for specific replacements that can improve other modules relying on the token Drupal 7 core API.

Code

function token_custom_load_multiple($names = NULL) {
  static $tokens = array();
  static $all_loaded = FALSE;
  if ($names === NULL) {
    if (!$all_loaded) {
      $loaded = array_keys($tokens);
      $query = db_select('token_custom')
        ->fields('token_custom');
      if (!empty($loaded)) {
        $query
          ->condition('machine_name', $loaded, 'NOT IN');
      }
      $results = $query
        ->execute();
      $all_loaded = TRUE;
      foreach ($results as $token) {
        $tokens[$token->machine_name] = $token;
      }
    }
    return $tokens;
  }
  $to_fetch = array();
  foreach ($names as $name) {
    if (!array_key_exists($name, $tokens)) {
      $to_fetch[] = $name;
    }
  }
  if (!empty($to_fetch)) {
    $query = db_select('token_custom')
      ->fields('token_custom')
      ->condition('machine_name', $to_fetch, 'IN');
    $results = $query
      ->execute();
    foreach ($results as $token) {
      $tokens[$token->machine_name] = $token;
    }
  }
  $return = array();
  foreach ($names as $name) {
    if (isset($tokens[$name])) {
      $return[$name] = $tokens[$name];
    }
  }
  return $return;
}