hook_entity_extra_field_info, pseudo fields

Public

</> CopyGet raw version
php
  1. <?php
  2.  
  3. /**
  4.  * Code for my_content.module
  5.  */
  6.  
  7. use Drupal\node\Entity\NodeType;
  8. use \Drupal\Core\Entity\EntityInterface;
  9. use \Drupal\Core\Entity\Display\EntityViewDisplayInterface;
  10.  
  11. /**
  12.  * Implements hook_entity_extra_field_info().
  13.  */
  14. function my_content_entity_extra_field_info() {
  15. $extra = [];
  16.  
  17. foreach (NodeType::loadMultiple() as $bundle) {
  18. $extra['node'][$bundle->Id()]['display']['pseudo_title_field'] = [
  19. 'label' => t('Pseudo Title'),
  20. 'description' => t('This is my own pseudo-title field'),
  21. 'weight' => -100,
  22. 'visible' => TRUE,
  23. ];
  24. }
  25.  
  26. return $extra;
  27.  
  28. }
  29.  
  30. /**
  31.  * Implements hook_ENTITY_TYPE_view().
  32.  */
  33. function my_content_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode, $langcode) {
  34. if ($display->getComponent('pseudo_title_field')) {
  35. $build['pseudo_title_field'] = [
  36. '#type' => 'markup',
  37. '#markup' => t('This is my custom content, which could be a title'),
  38. ];
  39. }
  40. }