We have a content type which has a customized template. In general, the body content of these nodes is short -- one to two sentences at most.
In some cases, however, the body content is a paragraph or more.
If the body content is shorter than 200 words, we would like to display the body field as-is.
If the body content is longer than 200 words, we would like to display the summary with a customized "read more" link.
Besides digging into the content array and counting words in the template, is there a graceful way to do this with view modes or field templates?
At the moment our clunky solution is to render both display types and swap between them with javascript, but this displays the "read more" link even when the body content is short enough to be displayed without the need for a summary.
node--custom.tpl.php
<?php
// Other fields above..
$summary = field_view_field('node', $node, 'body', [
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
]);
// Render summary of body, which is visible by default
print render($summary);
// Render full body as hidden with 'element-invisible' class (see field--body--timeline.tpl.php)
print render($content['body']);
?>
field--body--custom.php
<?php if($element['#view_mode'] !== 'full'): // Display trimmed version ?>
<div class="<?= $classes; ?>">
<!-- this is the summary field -->
<?php foreach($items as $delta => $item):
print render($item);
endforeach; ?>
<a class="fullTextButtonOn">Read More…</a>
</div>
<?php else:
// Display full body text with the element-invisible class and hidden by default ?>
<div class="element-invisible <?= $classes; ?>">
<!-- this is the full body field -->
<?php foreach($items as $delta => $item):
print render($item);
endforeach; ?>
<a class="fullTextButtonOff">…Read Less</a>
</div>
<?php endif; ?>
Resulting HTML on render
<div class="field field-name-body field-type-text-with-summary field-label-hidden">
<!-- this is the summary field -->
<p>Leverage agile frameworks to provide a robust synopsis for high level overviews. Iterative approaches to corporate strategy foster collaborative thinking to further the overall value proposition. Organically grow the holistic world view of disruptive innovation via workplace diversity and empowerment.</p>
<a class="fullTextButtonOn">Read More…</a>
</div>
<div class="element-invisible field field-name-body field-type-text-with-summary field-label-hidden">
<!-- this is the full body field -->
<p>Leverage agile frameworks to provide a robust synopsis for high level overviews. Iterative approaches to corporate strategy foster collaborative thinking to further the overall value proposition. Organically grow the holistic world view of disruptive innovation via workplace diversity and empowerment.</p>
<p>Bring to the table win-win survival strategies to ensure proactive domination. At the end of the day, going forward, a new normal that has evolved from generation X is on the runway heading towards a streamlined cloud solution. User generated content in real-time will have multiple touchpoints for offshoring.</p>
<a class="fullTextButtonOff">…Read Less</a>
</div>