8 – Custom formatter for paragraphs field

I want to build a custom formatter for a paragraphs field that renders some of the fields of the paragraph. However, I don’t know how to access the single files of the paragraph field.

So far I’ve only written the skeleton of the formatter, but it doesn’t do much, except that I can return a serialised string of all of the entire field, which does include the necessary information, but I only need three particular fields rendered themselves in a nice way (a term reference field with a label, a boolean field as “yes” or “no” and a text field. A Twig template is not an option for this.

The function looks like this:

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = ();

  foreach ($items as $delta => $item) {  
    // Desired fields: field_network_name, field_network_specific_site_code, field_network_verified

    $element($delta) = (
      '#markup' => "Output: " . serialize($item)
    );
  } 

  return $element;
}

I’d need to know how to elegantly access the fields inside $item.