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;
}