Last active
December 1, 2022 13:16
-
-
Save abelperezlindo/84b9ac3ffb0d0cc7a6292bad8764c369 to your computer and use it in GitHub Desktop.
Placing Components with Drupal's Extra Fields in drupal 9
This file contains hidden or 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 | |
// In mymodule.module : | |
use Drupal\node\Entity\NodeType; | |
use \Drupal\Core\Entity\EntityInterface; | |
use \Drupal\Core\Entity\Display\EntityViewDisplayInterface; | |
/** | |
* Implements hook_entity_extra_field_info(). | |
*/ | |
function my_module_entity_extra_field_info() { | |
$extra = []; | |
foreach (NodeType::loadMultiple() as $bundle) { | |
$extra['node'][$bundle->Id()]['display']['my_own_pseudo_field'] = [ | |
'label' => t('My own field'), | |
'description' => t('This is my own pseudo-field'), | |
'weight' => 100, | |
'visible' => TRUE, | |
]; | |
} | |
return $extra; | |
} | |
/** | |
* Implements hook_ENTITY_TYPE_view(). | |
*/ | |
function my_module_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { | |
if ($display->getComponent('my_own_pseudo_field')) { | |
$build['my_own_pseudo_field'] = [ | |
'#type' => 'markup', | |
'#markup' => 'This is my custom content', | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment