Creating pseudo-fields in Drupal 8
In this article we are going to look at how we can create and use pseudo-fields in Drupal 8.
What are pseudo-fields?
Pseudo-fields are simple display fields that you can control from the display settings of a particular entity type. An example of such a field is the core Links
field on the node or comment entities.
Why are they useful?
Pseudo-fields are great if you have some content or data that you need to render together with that of a particular entity. And since you don't want to be hacky and hardcode all of this inside a template or preprocessor, you can use pseudo-fields to expose some control over this to the UI. This means that you can use the core drag-and-drop functionality to control their visibility and position relative to regular entity fields.
Pseudo-fields are a great way to display data that is tightly coupled to the entities but is not part of them or their fields.
So how do they work?
There are 2 main steps we need to take in order to create a pseudo-field. First, we need to implement hook_entity_extra_field_info() which is similar to Drupal 7.
/** * Implements hook_entity_extra_field_info(). */function my_module_entity_extra_field_info() { $extra = array(); foreach (NodeType::loadMultiple() as $bundle) { $extra['node'][$bundle->Id()]['display']['my_own_pseudo_field'] = array( 'label' => t('My own field'), 'description' => t('This is my own pseudo-field'), 'weight' => 100, 'visible' => TRUE, ); } return $extra;}
We mustn't forget to use the NodeType
class at the top of the file:
use Drupal\node\Entity\NodeType;
With this implementation we are creating one pseudo-field called My own field that will show up on the display settings of all the node bundles. After clearing the cache, you can already see it if you go to the display settings of any node bundle you have.
The second step is making this field actually render something when the node is being viewed. For this we need to implement hook_entity_view() or any of its variants:
/** * Implements hook_ENTITY_TYPE_view(). */function my_module_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode, $langcode) { if ($display->getComponent('my_own_pseudo_field')) { $build['my_own_pseudo_field'] = [ '#type' => 'markup', '#markup' => 'This is my custom content', ]; }}
And again, let's use the necessary classes at the top:
use \Drupal\Core\Entity\EntityInterface;use \Drupal\Core\Entity\Display\EntityViewDisplayInterface;
Above we went with the hook_ENTITY_TYPE_view()
variant that applies to the node entity. Inside, we are being passed an EntityViewDisplayInterface
display object which we can use to check whether or not our own component (the one we defined earlier) exists in this display. If it does, we add our custom data to the $build
render array that is passed by reference. This check allows the user interface to determine the visibility of this component on each view mode, as well as the weight (position relative to the other components which can be either entity fields or other such pseudo-fields).
And that's about it. You can save, edit the display settings of your nodes, create view modes and specify exactly for which one and where this custom content should show up.
Hope this helps.
var switchTo5x = true;stLight.options({"publisher":"dr-8de6c3c4-3462-9715-caaf-ce2c161a50c"});