Skip to main content
1 of 4
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_details = Drupal\node\Entity\Node::load($nid);
$id = $field->value;  
dpm($id);

With this I can get the correct value let says:

  • id_string

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

String

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 value.

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