Skip to content

Instantly share code, notes, and snippets.

@abelperezlindo
Last active December 1, 2022 13:16
Show Gist options
  • Save abelperezlindo/84b9ac3ffb0d0cc7a6292bad8764c369 to your computer and use it in GitHub Desktop.
Save abelperezlindo/84b9ac3ffb0d0cc7a6292bad8764c369 to your computer and use it in GitHub Desktop.
Placing Components with Drupal's Extra Fields in drupal 9
<?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