Score:1

How can I programmatically render the summary of a textfield?

br flag
tis

I can output a textarea field with the following code:

$entity->fieldname->view();

How do I need to change that code for getting the summary of that field?

(Where can I find a documentation of that view-function?)

berliner avatar
bd flag
The documentation is here: [FieldItemBase::view](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21FieldItemBase.php/function/FieldItemBase%3A%3Aview/8.2.x) and it points to [EntityViewBuilderInterface::viewFieldItem](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityViewBuilderInterface.php/function/EntityViewBuilderInterface%3A%3AviewFieldItem/9.3.x) for the available options.
Score:6
cn flag

You have to use the field formatter text_summary_or_trimmed:

$build[] = $entity->fieldname->view([
  'label' => 'hidden',
  'type' => 'text_summary_or_trimmed',
  'settings' => [
    'trim_length' => 600,
  ],
]);

The easiest way to find the field formatter name and settings is configuring a field in a display mode of a content type and exporting the configuration.

How to find the documenation, see How to apply a Field Formatter to a node field programmatically?

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.