Last active
November 11, 2021 14:46
-
-
Save opi/5bcefdee9b89f8751a06d4b892f28e91 to your computer and use it in GitHub Desktop.
Extra field example for drupal 8/9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'), | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :
ou utiliser :
function nxt_commun_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {