You are here

function readmore_field_formatter_view in Readmore 7

Implements hook_field_formatter_view().

File

./readmore.field.inc, line 115
Field formatter hooks to implement a readmore.

Code

function readmore_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'readmore':
      drupal_add_js(READMORE_MODULE_PATH . '/js/readmore.js');
      foreach ($items as $delta => $item) {
        $text = _text_sanitize($instance, $langcode, $item, 'value');
        $text_length = drupal_strlen($item['value']);
        $trim_length = $display['settings']['readmore_trim_length'];
        $text_read_more = $display['settings']['readmore_more_text'];
        $text_read_less = $display['settings']['readmore_less_text'];

        // Don't do anything if text length less than defined.
        if ($text_length > $trim_length) {

          // Add Read less if need.
          if ($display['settings']['readmore_show_readless']) {
            $text .= ' ' . l($text_read_less, current_path(), array(
              'attributes' => array(
                'class' => array(
                  'readless-link',
                ),
              ),
            ));
          }

          // Get trimmed string.
          $summary = readmore_truncate_string($text, isset($item['format']) ? $item['format'] : NULL, $trim_length, $display['settings']['readmore_wordsafe'], $display['settings']['readmore_trim_on_break']);

          // Add readmore link.
          $summary .= '<span>';
          $summary .= $display['settings']['readmore_ellipsis'] ? t('...') : NULL;
          $summary .= l($text_read_more, current_path(), array(
            'attributes' => array(
              'class' => array(
                'readmore-link',
              ),
            ),
          ));
          $summary .= '</span>';

          // Close all HTML tags.
          $summary = _filter_htmlcorrector($summary);
          $element[$delta] = array(
            '#markup' => theme('readmore', array(
              'text' => $text,
              'summary' => $summary,
            )),
          );
        }
        else {
          $element[$delta] = array(
            '#markup' => $text,
          );
        }
      }
      break;
  }
  return $element;
}