You are here

function simplemeta_get_page_meta in Simple Meta 7

Same name and namespace in other branches
  1. 8.2 simplemeta.module \simplemeta_get_page_meta()
  2. 8 simplemeta.module \simplemeta_get_page_meta()
  3. 6.2 simplemeta.module \simplemeta_get_page_meta()
  4. 7.2 simplemeta.module \simplemeta_get_page_meta()

Get page's meta data.

@staticvar array $meta

Parameters

string $path: page's path to fetch meta data

string $language: language code

bool $reset: whether to reset static cache

Return value

object|FALSE object representing metadata or FALSE on failure

1 call to simplemeta_get_page_meta()
simplemeta_preprocess_html in ./simplemeta.module
Implements $module_preprocess_$hook().

File

./simplemeta.module, line 336
SimpleMeta module.

Code

function simplemeta_get_page_meta($path = NULL, $language = '', $reset = FALSE) {
  static $meta = array();
  if (!isset($path)) {
    $path = $_GET['q'];
  }
  if (!isset($meta[$path]) || $reset) {
    $meta[$path] = FALSE;
    $cid = $path . ':' . $language;
    if ($cache = cache_get($cid, 'cache_simplemeta')) {
      $meta[$path] = $cache->data;
    }
    else {
      $row = NULL;
      $original_map = arg(NULL, $path);
      $parts = array_slice($original_map, 0, MENU_MAX_PARTS);
      $ancestors = simplemeta_path_get_ancestors($parts);
      array_unshift($ancestors, $path);
      $query = db_select('simplemeta', 's')
        ->fields('s')
        ->condition('s.path', $ancestors, 'IN')
        ->condition('s.language', $language, '=')
        ->orderBy('s.fit', 'DESC')
        ->range(0, 1);
      $row = $query
        ->execute()
        ->fetchObject();

      // If there is no language-specific meta, try to load language-neutral.
      if (!$row && $language) {
        $query = db_select('simplemeta', 's')
          ->fields('s')
          ->condition('s.path', $ancestors, 'IN')
          ->condition('s.language', '', '=')
          ->orderBy('s.fit', 'DESC')
          ->range(0, 1);
        $row = $query
          ->execute()
          ->fetchObject();
      }
      if ($row) {
        $row->data = unserialize($row->data);
        $row->view = array();
        $info = simplemeta_get_info();
        foreach ($info as $key => $definition) {
          $themed = theme($definition['theme'], array(
            'meta' => $row,
          ));
          if ($themed) {
            $row->view[$key] = $themed;
          }
        }
        cache_set($cid, $row, 'cache_simplemeta');
        $meta[$path] = $row;
      }
    }
  }
  return $meta[$path];
}