How to create pseudo fields in Drupal using 2 hooks
Tags: drupal, fieldapi
Published: 2023-03-04 20:50:58

---
title: How to create pseudo fields in Drupal using 2 hooks
published: true
date: 2023-03-04 17:35:31 UTC
tags: Drupal,FieldAPI
canonical_url: https://davidloor.com/2023/03/04/how-to-create-pseudo-fields-in-drupal/
---

![Drupal logo and how to create fields](https://i0.wp.com/davidloor.com/wp-content/uploads/2023/03/Screenshot-2023-03-04-at-12.32.18-PM.png?resize=800%2C299&ssl=1)

## What are pseudo fields?

In Drupal, pseudo fields are fields that do not store data in the database, but rather dynamically generate content on the fly. Pseudo fields allow site builders and developers to add custom content to entities (such as nodes, users, and taxonomy terms) without needing to create a separate field for it.

Pseudo-fields behave like regular fields in many respects. However, instead of storing data in the database, they use a callback function to generate their content when the entity is viewed.

## How to create pseudo fields?

### On Drupal 8, 9, 10

Using `hook_ENTITY_TYPE_view()` in combination with `hook_entity_extra_field_info()`, you can define custom pseudo fields and specify exactly how they should be rendered when the entity is displayed.

In the following example, we will add a pseudo field to a paragraph entity type which will render a [google classroom share button](https://developers.google.com/classroom/guides/sharebutton) wherever the paragraph is used. The field will appear in the manage display as the fields created in the entity.

![](https://i0.wp.com/davidloor.com/wp-content/uploads/2023/03/Screenshot-2023-03-04-at-12.08.01-PM.png?resize=800%2C292&ssl=1)
_The Drupal pseudo field will appear in the list of fields in the manage display tab._

### The recipe

Make sure that you add the two following lines at the top of the file.

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

In your custom module’s `my_module.module` file, define the pseudo field using the `hook_entity_extra_field_info()` function:

```
/**
 * Implements hook_entity_extra_field_info().
 */
function MY_MODULE_entity_extra_field_info() {
  $display_fields = [];

  $display_fields['paragraph']['google_classroom_button']['display']['google_classroom_button'] = [
    'label' => t('Google Classroom button'),
    'description' => t('Shows the google classroom button'),
    'weight' => 100,
    'visible' => TRUE,
  ];

  return $display_fields;
}
```

In this example, we’re defining a pseudo field called `google_classroom_button` for the `paragraph` entity type and bundle **google\_classroom\_button**.

If you want to add the field to a content type, you should replace the word **paragraph** with **node** and **google\_classroom\_button** with the name of your content type, in the code above.

```

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function MY_MODULE_paragraph_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {

  if ($display->getComponent('google_classroom_button')) {
    $currentUrl = Url::fromRoute('<current>',[] , ['absolute' => 'true'])->toString();
    $output = '<script src="https://apis.google.com/js/platform.js" async defer></script>';
    $output .= '<div class="g-sharetoclassroom" data-size="64" data-url="' . $currentUrl . '" ></div>';
    $build['google_classroom_button'] = [
      '#type' => 'markup',
      '#markup' => $output,
      '#allowed_tags' => ['script', 'div'],
    ];
  }
}
```

The code above is checking whether the “google\_classroom\_button” component has been added to the display configuration of the Paragraph entity. If it has, then it generates a custom output for the “google\_classroom\_button” pseudo field.

## Use cases of pseudo fields

- Displaying computed values or aggregates (such as the number of comments on a node)
- Embedding content from other sources (such as YouTube videos or social media feeds)
- Displaying custom markup or HTML (such as a list of related content)

### Final thoughts

- This is a quick way to get extra information in an entity without dealing with twig templates.
- The pseudo field will not be available when creating a view for the entity.