You are here

function context_prefix_prefixes in Context 5

Queries the database & modules for valid prefixes based on prefixing method.

Modules that wish to provide in-code prefixes should implement the hook_context_prefix_prefixes(). Which should return an array of prefixes by by provider.

For example:

return array( 'my_module => array( array('prefix' => 'foo', 'id' => 1), array('prefix' => 'bar', 'id' => 2), ), );

3 calls to context_prefix_prefixes()
context_prefix_admin in context_prefix/context_prefix.module
Page callback for the context_prefix administration page.
context_prefix_goto in context_prefix/context_prefix.module
A wrapper around drupal_goto() that abstracts out the prefix/context setting You provide both a normal drupal path ('node/43') and a context prefix ('dsi') and context_prefix_goto will determine the correct location to use.
context_prefix_parse in context_prefix/context_prefix.module
Parses a query string of various types (url, domain, etc.) and returns an array of any found prefixes and their respective providers/id values.

File

context_prefix/context_prefix.module, line 163

Code

function context_prefix_prefixes($requested_method = CONTEXT_PREFIX_PATH, $reset = FALSE) {
  static $prefixes;
  if (!isset($prefixes) || $reset) {
    $prefixes = array();

    // Invoke context_prefix_prefixes() and gather all prefixes
    // provided "in code" (or stored by their respective modules)
    foreach (module_invoke_all('context_prefix_prefixes') as $provider => $items) {
      $method = variable_get('context_prefix_method_' . $provider, CONTEXT_PREFIX_PATH);

      // If using a prefix pair we don't need to cache the valid prefixes.
      if ($method == CONTEXT_PREFIX_PAIR) {
        $prefix = variable_get('context_prefix_method_' . $provider . '_key', false);
        if ($prefix != false) {
          $prefixes[$method][$prefix] = array(
            'provider' => $provider,
            'id' => null,
          );
        }
      }
      else {
        foreach ($items as $item) {
          if ($item['prefix'] && $item['id']) {
            $prefixes[$method][$item['prefix']] = array(
              'provider' => $provider,
              'id' => $item['id'],
            );
          }
        }
      }
    }

    // Gather database prefixes.
    $result = db_query("SELECT DISTINCT(provider) FROM {context_prefix}");
    while ($item = db_fetch_object($result)) {
      $method = variable_get('context_prefix_method_' . $item->provider, CONTEXT_PREFIX_PATH);

      // Don't load all data base prefixes for keyed pairs.
      if ($method == CONTEXT_PREFIX_PAIR) {
        $prefix = variable_get('context_prefix_method_' . $item->provider . '_key', false);
        if ($prefix != false) {
          $prefixes[$method][$prefix] = array(
            'provider' => $item->provider,
            'id' => null,
          );
        }
      }
      else {
        $result2 = db_query("SELECT * FROM {context_prefix} WHERE provider = '%s'", $item->provider);
        while ($row = db_fetch_object($result2)) {
          $prefixes[$method][$row->prefix] = array(
            'provider' => $row->provider,
            'id' => $row->id,
          );
        }
      }
    }
  }
  return isset($prefixes[$requested_method]) ? $prefixes[$requested_method] : array();
}