I have created a pseudo field using hook_entity_extra_field_info() and displaying markup using hook_entity_view().

How can I create a checkbox setting in node create form so that I can show / hide this pseudo field per node?

  • There was a similar question about a pseudo a field a while ago. You need to store the data of that selection per node, so why not just make a it an actual field? You can easily check that in a template and do stuff based on it.
    – Berdir
    Aug 31 '17 at 13:03
  • I am displaying a url based on current node id in pseudo field markup. If I create an actual field then how to manipulate / override its markup.
    – Sam
    Aug 31 '17 at 14:45
  • You get the fully created form structure for the field in form alter, you can still customize it there if you want. You could also implement your own widget that extends from the default, for a cleaner, reusable approach But you get a things like storage handling for free if all you need to customize is the default value.
    – Berdir
    Sep 1 '17 at 7:18

In hook_ENTITY_TYPE_view which you use to render your pseudo field there already is the $entity available. Which in your case must be a node object. You simply can get any field value from there.

So all you need to do is to add a Drupal default checkbox/boolean field to your content type, from the UI probably, hide it from display, and then output your pseudo field depending on its value, per node.

 * Implements hook_entity_extra_field_info().
function MYMODULE_entity_extra_field_info() {

  $extra['node']['article']['display']['foo_bar'] = [
    'label'       => t('My pseudo field'),
    'description' => t('My pseudo field description.'),
    'weight'      => 0,
    'visible'     => TRUE,

  return $extra;

 * Implements hook_ENTITY_TYPE_view().
function MYMODULE_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {

  if ($entity->bundle() == 'article') {

    // Get the choice.
    $editors_choice = $entity->field_editors_choice->first()->getValue();
    $editors_choice = isset($editors_choice['value']) ? $editors_choice['value'] : FALSE;

    // And fire accordingly.
    if ($display->getComponent('foo_bar') && $editors_choice) {
      $build['foo_bar'] = [
        '#markup' => 'This is my custom content',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.