0

I have created a new field type, widget and formatter using the Drupal Console generator and everything works perfectly until I try to change the viewElements() function in the Formatter class.

The original code simply loops through the elements and adds the field value to the #markup of the $elements render array.

public function viewElements() {
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#markup' => $this->viewValue($item)
    ]
  }
  return $elements;
}

protected function viewValue(FieldItemInterface $item) {
  // The text value has no text format assigned to it, so the user input
  // should equal the output, including newlines.
  return nl2br(Html::escape($item->value));
}

This works and prints the value as expected. However, if I change what is in the #markup to anything else e.g.

$element[$delta] => [
  '#markup' => '<p>Print something different</p>',
]

Nothing changes in the output. It still prints out the value of the field.

I have tried flushing all caches, uninstalled and reinstalled the module, but nothing seems to make it change.

Could anyone point me in the right direction please?

  • 1
    If you've changed the code, and you're still seeing the old values, it means you have a caching issue, either at the Drupal level, the server level, or the browser level. You'll have to play around until you can find where it's being cached. – Jaypan Oct 3 '18 at 8:14
  • Yes, I have done that but it has made no difference. This is on my local machine so there is only Drupal or browser caching, both of which have been flushed many, many times – Jimmyb_1991 Oct 3 '18 at 8:59
  • 1
    Well, there is still server caching, even if the server is on your local machine. Try switching to a different formatter, then back to this one. – Jaypan Oct 3 '18 at 9:00
  • Ok, so I have flushed Drupals cache, cleared my browser cache (and even tried in a different browser), restarted Valet + all to absolutely no avail. Is it possible that something in core is overriding my formatter? I have never maid a field before and have no clue what I am doing – Jimmyb_1991 Oct 3 '18 at 10:48
  • Did you try switching to a different formatter and back? – Jaypan Oct 3 '18 at 10:49
0

In the initial code you used $elements[$delta] (note the plural), whereas in

$element[$delta] => [
  '#markup' => '<p>Print something different</p>',
]

you're using the singular form. Is this just a typo in your post or also in the actual the code that fails?

  • Sorry, yes, this is just a typo – Jimmyb_1991 Oct 3 '18 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.