Skip to main content

Drupal 8: How to add an info popover description per field in node full display


function mytheme_preprocess_field(&$variables, $hook) {

  $element = $variables['element'];
  $bundle = 'my_bundle'; //Do this for only specific node bundles

  if ($variables['bundle'] == $bundle && isset($element['#field_name'])) {
      $field_instance = Drupal::entityTypeManager()->getStorage('field_config')->load('node.mcdataset.'.$element['#field_name']);
      if (!is_null($field_instance)) {
        $fieldDescription = $field_instance->getDescription();
        if (!is_null($fieldDescription)) {
//Popover works with bootstrap 
          $variables['items'][0]['content']['#suffix'] = '<div class="tooltip-wrapper"><span class="tooltip-info" data-toggle="popover" data-trigger="hover" data-placement="top" data-content="'.$fieldDescription.'">!</span></div>';
        }
      }
    }
}

 

subtheme theme