Problem/Motivation

label help description doesn't rendered on view mode, but we need it.

I tried to use hook_entity_view() to append it to the title, or render under "#prefix", but they are not same look as Edit form. Is there anyone has idea how to do it, to achive the similar look of Edit form?

here is the hook_entity_view I am testing.

/**
 * Implements hook_entity_view().
 */
function my_forms_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if ($entity->getEntityTypeId() === 'node' && \Drupal::moduleHandler()->moduleExists('label_help')) {
    foreach ($entity->getFields() as $field_name => $field_item_list) {
      // Get the field definition.
      $field_definition = $field_item_list->getFieldDefinition();
      
      if (!method_exists($field_definition, 'getThirdPartySetting')) {
        // not all fields have getThirdPartySetting 
        continue;
      }

      $label_help_description = $field_definition->getThirdPartySetting('label_help', 'label_help_description');

      if (!empty($label_help_description)) {
        $help_text = [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#attributes' => ['class' => 'label-help-display'],
          '#value' => $label_help_description,
        ];
        // Attach the help text before the field's content in the build array.
        // Adjust this placement as needed for your specific display.
        if (isset($build[$field_name])) {
          $build[$field_name]['#title'] .= " " . $label_help_description . "";
          //$build[$field_name]['#prefix'] = \Drupal::service('renderer')->render($help_text);
        }
      }
    }
  }
}

Comments

bluemoonj created an issue. See original summary.

jwilson3’s picture

This support request is outside the scope of this module's area of responsibility, which is specifically the help text on entity edit forms.

That being said, the issue is your current approach doesn't use the same theme hook that label_help uses on forms. The module renders via #theme => 'label_help' with specific templates for common admin themes (claro, gin, seven) and CSS classes (form-item__description, form-item__description--label-help for Claro).

Here's one (untested) possible approach:

/**
 * Implements hook_entity_view().
 */
function my_forms_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if ($entity->getEntityTypeId() === 'node' && \Drupal::moduleHandler()->moduleExists('label_help')) {
    foreach ($entity->getFields() as $field_name => $field_item_list) {
      $field_definition = $field_item_list->getFieldDefinition();

      if (!method_exists($field_definition, 'getThirdPartySetting')) {
        continue;
      }

      $label_help_description = $field_definition->getThirdPartySetting('label_help', 'label_help_description');
      if (!empty($label_help_description) && isset($build[$field_name])) {
        // Use the same theme hook as the backend form rendering.
        $help_text = [
          '#theme' => 'label_help',
          '#content' => ['#markup' => $label_help_description],
        ];

        // Prepend to the field.
        $build[$field_name]['#prefix'] = \Drupal::service('renderer')->render($help_text);
      }
    }
  }
}

However, there's a catch: the label_help theme integration includes admin-only CSS libraries (Claro/Seven/Gin). The frontend theme won't pick those up.

jwilson3’s picture

Version: 2.0.0-rc3 » 2.0.x-dev
Status: Active » Closed (works as designed)

Marking this one as closed for now. I'm happy to review again if more assistance is needed. Just reopen and provide more context. Thanks.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.