function metatag_generate_entity_metatags in Metatag 7
Same name and namespace in other branches
- 8 metatag.module \metatag_generate_entity_metatags()
Generate the metatags for a given entity.
Parameters
object $entity: The entity object to generate the metatags for.
string $entity_type: The entity type of the entity.
string $langcode: The language code used for rendering the entity.
string $view_mode: The view mode the entity is rendered in.
bool $cached: TRUE if metatags can be loaded from and saved to the cache. FALSE if the cache should be bypassed.
Return value
mixed A renderable array of metatags for the given entity. If this entity object isn't allowed meta tags, return FALSE (empty).
2 calls to metatag_generate_entity_metatags()
- metatags_get_entity_metatags in ./
metatag.module - Generate the metatags for a given entity.
- metatag_entity_view in ./
metatag.module - Implements hook_entity_view().
File
- ./
metatag.module, line 1219 - Primary hook implementations for Metatag.
Code
function metatag_generate_entity_metatags($entity, $entity_type, $langcode = NULL, $view_mode = 'full', $cached = TRUE) {
// Obtain some details of the entity that are needed elsewhere.
list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
// If this entity object isn't allowed meta tags, don't continue.
if (!metatag_entity_supports_metatags($entity_type, $bundle)) {
return;
}
$revision_id = intval($revision_id);
// Check if a specific metatag config exists, otherwise just use the global
// one, stripping out the bundle.
$instance = metatag_get_entity_metatags_instance($entity, $entity_type, $bundle);
if (!metatag_config_load_with_defaults($instance, FALSE)) {
$instance = "{$entity_type}";
}
// Determine the language this entity actually uses.
$entity_language = metatag_entity_get_language($entity_type, $entity);
// If no language was requested, try the language defined for this page
// request.
if (empty($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
// This entity doesn't have any languages defined, i.e. it uses 'und'. This
// can't conflict with loading the wrong language as entities either have no
// language or they have specific one(s), they can't have both.
if ($entity_language == LANGUAGE_NONE) {
$langcode = LANGUAGE_NONE;
}
elseif (empty($entity->metatags[$langcode]) && !empty($entity->metatags[$entity_language]) && !variable_get('metatag_entity_no_lang_default', FALSE)) {
$langcode = $entity_language;
}
else {
// There's no need to do anything else - either there are meta tag values
// created for the requested language or there aren't.
}
$cid = FALSE;
$key = FALSE;
$metatag_variants = array();
// Caching is enabled.
if ($cached && variable_get('metatag_cache_output', FALSE)) {
// All possible variants of the metatags for this entity are stored in a
// single cache entry.
$cid = "output:{$entity_type}:{$entity_id}";
// All applicable pieces for this current page.
$key_parts = array(
'entity_type' => $entity_type,
'bundle' => $bundle,
'entity_id' => $entity_id,
'revision_id' => $revision_id,
// Cache separately based on the language of the passed-in entity and the
// overall active language of the page.
'langcode' => $langcode,
'language_content' => $GLOBALS['language_content']->language,
'view_mode' => $view_mode,
);
$key = metatag_cache_default_cid_parts($key_parts);
if ($cache = metatag_cache_get($cid)) {
$metatag_variants = $cache->data;
}
}
// If a cached object exists for this key, return it.
if (!empty($key) && isset($metatag_variants[$key])) {
$output = $metatag_variants[$key];
}
else {
// Separate the meta tags.
$metatags = isset($entity->metatags) ? $entity->metatags : array();
// Build options for meta tag rendering.
$options = array(
'entity' => $entity,
'entity_type' => $entity_type,
'view_mode' => $view_mode,
);
// Ensure we actually pass a language object rather than language code.
$languages = language_list();
if (isset($languages[$langcode])) {
$options['language'] = $languages[$langcode];
}
// Include token replacement data. Don't reload the entity object as doing
// so would conflict with content editorial workflows.
if (!empty($entity_id)) {
$token_type = token_get_entity_mapping('entity', $entity_type);
$options['token data'][$token_type] = $entity;
}
// Render the metatags and save to the cache.
$output = metatag_metatags_view($instance, $metatags, $options);
// If output caching is enabled, store the data for later.
if (!empty($key) && !empty($cid)) {
$metatag_variants[$key] = $output;
metatag_cache_set($cid, $metatag_variants);
}
}
return $output;
}