0

I have a FieldFormatter that outputs a DIV, a table and attaches a JS library.

Normally in Drupal (e.g. Form API) it's possible to label the render array keys however you like? Weirdly in this case, only numbering them seems to work.

If it's a typo, I'm not spotting it, and there are no errors in the log.

Are there differences in how FieldFormatter rendering works?

Working numeric keys:

  public function viewElements(FieldItemListInterface $items, $langcode) {

    // various logic ...

    $build = [];

    $build[0] = [
      // placeholder div for the YouTube player
      '#type' => 'container',
      '#attributes' => [
        'id' => ['youtube-timecode-player'],
      ],
    ];

    // various logic ...

    // attach our timecode.js javascript make the video ID available
    $build[0]['#attached']['library'][] = 'mymodule/youtube_timecode';
    $build[0]['#attached']['drupalSettings']['mymodule']['video_id'] = $video_id;

    $build[1] = [
      '#type' => 'table',
      '#caption' => $this->t('Questions'),
      '#header' => [$this->t('Time'), $this->t('Question')],
      '#rows' => $timecodes
    ];

    return $build;
  }

Non-working text keys (no output and no errors):

(I've changed 0 to video and 1 to timecode_list)

public function viewElements(FieldItemListInterface $items, $langcode) {

    // various logic ...

    $build = [];

    $build['video'] = [
        // placeholder div for the YouTube player
        '#type' => 'container',
        '#attributes' => [
            'id' => ['youtube-timecode-player'],
        ],
    ];

    // various logic ...

    // attach our timecode.js javascript make the video ID available
    $build['video']['#attached']['library'][] = 'mymodule/youtube_timecode';
    $build['video']['#attached']['drupalSettings']['mymodule']['video_id'] = $video_id;

    $build['timecode_list'] = [
        '#type' => 'table',
        '#caption' => $this->t('Questions'),
        '#header' => [$this->t('Time'), $this->t('Question')],
        '#rows' => $timecodes
    ];

    return $build;

}
  • Is the table also not outputted with textual keys, or only the video (and container?)? – Jaypan Sep 24 '19 at 7:49
  • @Jaypan sorry not sure I understand you right, but the table I'm calling 'timecode_list' and I don't believe I've got a mix of numeric and text keys. – William Turrell Sep 24 '19 at 7:56
  • I mean, is nothing at all outputted, or are you getting any output at all (from the render array, not the rest of the page) – Jaypan Sep 24 '19 at 22:10
3

The return value of FormatterInterface::viewElements is defined as:

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

The theming layer can't handle other arrays for field values, see How to print all values of a multi-value field, except the first?

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.