Daniel Veza
PreviousNext
Using Drupal since 2014
abstract class FieldBlockBase extends BlockBase {
abstract protected function getFieldItemList();
abstract protected function getFormatterSettings();
public function build(): array {
return $this->getFieldItemList()?->view(
$this->getFormatterSettings() + ['label' => 'hidden']
) ?: [
'#cache' => [
'tags' => [
'node_list',
],
],
];
}
}
final class AuthoredByBlock extends FieldBlockBase {
protected function getFieldItemList(): ?FieldItemListInterface {
$node = $this->getContextValue('node');
return $node->get('uid');
}
protected function getFormatterSettings(): array {
return [
'type' => 'author',
];
}
}
Questions