Instantly share code, notes, and snippets.
Created
May 21, 2015 01:46
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save reubenmoes/4fe70ad22696fdf36277 to your computer and use it in GitHub Desktop.
Drupal add graph metatags with preprocess_html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_preprocess_html() | |
* | |
* Adds open graph tags | |
* | |
* @param array $variables | |
*/ | |
function module_preprocess_html(&$variables) { | |
/* | |
* Init Metatags | |
* Don't need to do this on admin pages | |
*/ | |
//Fetch node | |
if(!path_is_admin(current_path())) { | |
$node = menu_get_object(); | |
$meta = array(); | |
/** | |
* Title | |
* | |
* Use site name for homepage | |
* Use default page title or node title | |
*/ | |
if ( drupal_is_front_page() ) { | |
$meta['title'] = trim(variable_get('site_name')); | |
} | |
else { | |
//Get drupal title (views pages, node title) | |
$meta['title'] = drupal_get_title(); | |
//Fallback to node title (needed if node title is hidden) | |
if(empty($meta['title'])) { | |
$meta['title'] = trim($node->title); | |
} | |
} | |
/** | |
* Url | |
*/ | |
$meta['url'] = url(current_path(), array('absolute' => TRUE)); | |
/** | |
* Site name | |
*/ | |
$meta['site_name'] = trim(variable_get('site_name')); | |
/** | |
* Description | |
* | |
* Add tokens to $content_tokens in the order that you want them | |
* to be checked. The first token that returns a result will be | |
* used. | |
* | |
*/ | |
//Set default | |
$raw_description = variable_get('site_slogan'); | |
//Tokens to check. Given in order | |
$content_tokens = array( | |
'[node:metatag:description]', | |
'[node:field-content:summary]', | |
'[node:field_content]', | |
); | |
//Check tokens. Break at first token find | |
foreach ($content_tokens as $token) { | |
$token_value = token_replace($token, array('node' => $node), array('clear' => true)); | |
if($token_value) { | |
$raw_description = $token_value; | |
break; | |
} | |
} | |
//Set value | |
//trim to 100 words | |
//strip html | |
//trim white space | |
//remove line breaks | |
$meta['description'] = str_replace(array("\r", "\r\n", "\n"), '', trim(dnv_custom_limit_words(strip_tags($raw_description),100))); | |
/** | |
* Image | |
* | |
* Add tokens to $image_tokens in the order that you want them | |
* to be checked. The first token that returns a result will be | |
* used. | |
* | |
*/ | |
//Set default | |
$image_path = ''; | |
//Tokens to check. Given in order | |
$image_tokens = array( | |
'[node:field-neighbourhood-map:file:url]', | |
'[node:field-banner:0:field-banner-image:file:url]', | |
); | |
//Check tokens. Break at first token find | |
foreach ($image_tokens as $token) { | |
$token_value = token_replace($token, array('node' => $node), array('clear' => true)); | |
if($token_value) { | |
$image_path = $token_value; | |
break; | |
} | |
} | |
//Set value | |
$meta['image'] = $image_path; | |
/** | |
* Print open graph tags | |
*/ | |
$open_graph_attributes = array('title', 'site_name', 'url', 'description', 'image'); | |
foreach ( $meta as $tag => $value ) { | |
if ( $value && in_array($tag, $open_graph_attributes) ) { | |
drupal_add_html_head(array( | |
'#tag' => 'meta', | |
'#attributes' => array( | |
'property' => 'og:'.$tag, | |
'content' => $value, | |
), | |
), 'og_'.$tag); | |
} | |
} | |
/** | |
* Print Twitter card stuff | |
*/ | |
$twitter_card_attributes = array('title', 'description', 'image', 'url'); | |
drupal_add_html_head(array( | |
'#tag' => 'meta', | |
'#attributes' => array( | |
'name' => 'twitter:card', | |
'content' => 'summary', | |
), | |
), 'twitter_card'); | |
foreach ( $meta as $tag => $value ) { | |
if ( $value && in_array($tag, $twitter_card_attributes) ) { | |
drupal_add_html_head(array( | |
'#tag' => 'meta', | |
'#attributes' => array( | |
'name' => 'twitter:'.$tag, | |
'content' => $value, | |
), | |
), 'twitter_'.$tag); | |
} | |
} | |
/* | |
* End of Metatags | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment