Skip to main content
2 of 4
added 81 characters in body
Adrian Cid Almaguer
  • 18.5k
  • 14
  • 79
  • 133

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\node\Entity\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 have 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 I don't know how to apply the Field Formatter programmatically to the field values.

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