You are here

function message_example_trim_body in Message 7

Trim the body field for a node/ comment.

Parameters

$entity_type: The entity type (node or comment).

$entity: The entity object.

Return value

Body field text trimmed to 50 chars, with a "...Read more" suffix.

1 call to message_example_trim_body()
message_example_tokens in message_example/message_example.tokens.inc
Implements hook_tokens().

File

message_example/message_example.tokens.inc, line 59
Token integration for the message example module.

Code

function message_example_trim_body($entity_type, $entity) {
  $wrapper = entity_metadata_wrapper($entity_type, $entity);

  // Select the right field acoring to the entity type.
  $field_name = $entity_type == 'node' ? 'body' : 'comment_body';
  $field_value = $wrapper->{$field_name}
    ->value();
  if (!$field_value) {
    return;
  }

  // Get the safe value.
  $field_value = $wrapper->{$field_name}->value
    ->value();
  $options = array(
    'max_length' => 50,
    'html' => TRUE,
  );
  $trim_text = views_trim_text($options, $field_value);

  // Checking if we need to add the "Read more" link.
  if ($field_value != $trim_text) {
    $uri = entity_uri($entity_type, $entity);

    // Add a "Read more" before the closing <p>.
    $trim_text = substr_replace($trim_text, '... ' . l(t('Read more'), $uri['path'], $uri['options']) . '</p>', -4);
  }
  return $trim_text;
}