Hello.
In order to avoid a sentence or word cutting when creating "og: description" from the node body, I suggest to replace call to function drupal_substr() with call to text_summary(): line 137 in file opengraph_meta.common.inc:
$ret[self::DESCRIPTION] = !empty($body) ? drupal_substr(strip_tags($body),0,200) : $node->title;
replace with:
$ret[self::DESCRIPTION] = !empty($body) ? text_summary(strip_tags($body),NULL,200) : $node->title;
In this way we will get a nice description, rather than half of a sentence, or in a middle of the word truncated phrase.
Example:
drupal_substr():

"Our foam yoga blocks are a simple tool that will help you ease into difficult poses by strengthening, stretching and aligning your body without strain. Yoga blocks also help to ensure that you practic"

text_summary():

"Our foam yoga blocks are a simple tool that will help you ease into difficult poses by strengthening, stretching and aligning your body without strain."

Comments

erik seifert’s picture

function MY_MODULE_node_load($nodes, $types) {
	if ( !module_exists('opengraph_meta') ) return ;
	foreach($nodes as &$node){
		if (OpenGraphMeta::instance()->tags_are_enabled_for_content_type($node->type)) {
			$node->opengraph_meta = OpenGraphMeta::instance()->load_node_data($node);
			$node->opengraph_meta['description'] = ($node->body[$node->language][0]['safe_summary']) ? $node->body[$node->language][0]['safe_summary'] : text_summary($node->body[$node->language][0]['safe_value']) ;
			if ( empty($node->opengraph_meta['description']) ) $node->opengraph_meta['description'] = $node->title ;
		}
	}
}
sgdev’s picture

I would agree with this as well. I would also suggest performing a PHP "trim" to remove spaces I occasionally see at the front of the og:description content and allowing the user to select if they want to use the $node->body or $node->teaser for their description. In most situations, the teaser more than meets the need and provides a nice, neat description.

sgdev’s picture

@erikseifert: does it make sense to do that on a node_load, or in the template preprocess? We do something similar on the theme_preprocess_page to avoid running extra code every time a node_load is done (which in Drupal is often).

Edit: Well the module does it on the nodeapi hook... if staying true to what the module is trying to do it's probably the right place.

erik seifert’s picture

Normally node load should be cached. So there is no problem to do this in node_load hook.

@see entity_load

If you want to change the functionality from website to website it could go to the theme layer.

A really better approach could be this:

Set a default value for Meta Field with tokens ;-)

Like:

[node:summary]

racinggrinner’s picture

Thanks erikseifert for the useful snippet.

I have modified it to assign the og:description to a custom field which appears within specific content types.

This is useful for existing sites which have a custom teaser field (for example) and the field is preferred to the node body for the og:description.

<?php
/**
 * Assign the Opengraph meta description to the YOUR_CUSTOM_FIELD
 */
function MY_MODULE_node_load($nodes, $types) {

  switch ($types[0]){
    case 'CONTENT_TYPE_1' :
    case 'CONTENT_TYPE_2' :
    case 'CONTENT_TYPE_3' :

    if (!module_exists('opengraph_meta')) return ;
    foreach($nodes as &$node){
      if (OpenGraphMeta::instance()->tags_are_enabled_for_content_type($node->type)) {
        $node->opengraph_meta = OpenGraphMeta::instance()->load_node_data($node);

        if (isset($node->YOUR_CUSTOM_FIELD['und'][0]['value'])) {
          $node->opengraph_meta['description'] = $node->YOUR_CUSTOM_FIELD['und'][0]['value'];
        }
        elseif (isset($node->body['und'][0]['summary'])) {
          $node->opengraph_meta['description'] = $node->body['und'][0]['summary'];
        }
      }
    }
    
    break;
  }
}
?>