Skip to content

Instantly share code, notes, and snippets.

@opi
Last active November 11, 2021 14:46
Show Gist options
  • Save opi/5bcefdee9b89f8751a06d4b892f28e91 to your computer and use it in GitHub Desktop.
Save opi/5bcefdee9b89f8751a06d4b892f28e91 to your computer and use it in GitHub Desktop.
Extra field example for drupal 8/9
<?php
/**
* Implements hook_entity_extra_field_info().
*/
function MYMODULE_entity_extra_field_info() {
$extra = [];
foreach (NodeType::loadMultiple() as $bundle) {
$extra['node'][$bundle->Id()]['display']['extra_field_on_every_node_bundle'] = [
'label' => t('Extra field on every node bundle'),
'description' => t('bla bla'),
'weight' => 100,
'visible' => FALSE,
];
}
$extra['node']['MY_BUNDLE']['display']['extra_field_on_single_node_bundle'] = [
'label' => t('Extra field on a single node bundle'),
'description' => t('blabla'),
'weight' => 100,
'visible' => FALSE,
];
return $extra;
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function MYMODULE_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($display->getComponent('extra_field_on_every_node_bundle')) {
$build['extra_field_on_every_node_bundle'] = [
'#type' => 'view',
'#name' => 'my_view',
'#display_id' => 'page_1',
'#arguments' => [$arg1, $arg2]
];
}
if ($display->getComponent('extra_field_on_single_node_bundle')) {
$build['extra_field_on_single_node_bundle'] = [
'#markup' => Markup::create('Foo <strong>Bar</strong>'),
];
}
}
@steveoriol
Copy link

steveoriol commented Oct 26, 2021

Salut opi ;-)
c'est pas :
'#display' => 'page_1'
mais :
'#display_id' => 'page_1'

tu peux aussi ajouter :
'#arguments' => [....]
ça peu servir.

Il faut aussi ajouter :

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;

ou utiliser :
function nxt_commun_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment