Skip to main content
4 of 4
deleted 15 characters in body
Pierre.Vriens
  • 36k
  • 40
  • 50
  • 182

How to apply a Field Formatter to a node field programmatically?

I have the following code to get the value that is stored in a field

$nid = 76;
$node = \Drupal::service('entity.manager')->getStorage('node')->load($nid);
foreach ($node->field_aaaa as $delta) {      
  dpm($delta->value);
}

With this I can get the correct value let says:

 * id_string1
 * id_string2
 * id_string3

But my field has a Field Formatter named title_formatter that will take the string id_string1 and will show something like this:

String 1

In the UI this works fine if I chose my Title Formatter (title_formatter) for the field I can see the 'String' when I viewing my node.

But how to apply the Field Formatter programmatically to the field values?

Adrian Cid Almaguer
  • 18.5k
  • 14
  • 79
  • 133