Date field labels get overridden

Created on 17 June 2024, 11 days ago
Updated 19 June 2024, 9 days ago

Problem/Motivation

I stumbled upon these two lines in openculturas_custom.module (lines 384 and 385):

function openculturas_custom_node_view_alter(array &$build, NodeInterface $entity): void {
    // […]
    $build['field_location']['#title'] = t("Visit the location's profile");
    $build['field_event_description']['#title'] = t('See all about');
    // […]
}

Is there a reason why these labels are overridden in code? Why not change the field labels in config instead? It would make it easier for implementations to use different field labels.

πŸ› Bug report
Status

Active

Version

2.2

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany mrshowerman Munich

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mrshowerman
  • πŸ‡©πŸ‡ͺGermany tobiasb Berlin

    A field label is used for input (form) and output (view), but did not want to change the label for input, therefore we did this via code for the output.

  • πŸ‡©πŸ‡ͺGermany mrshowerman Munich

    Got it.
    But when a site chooses to use the same label for form and output, they have to write their own hook to revert the upstream logic.

    Here's what we did:

    /**
     * Implements hook_ENTITY_TYPE_view_alter().
     */
    function mymodule_node_view_alter(array &$build, NodeInterface $entity): void {
      if ($entity->bundle() === 'date' && $build['#view_mode'] === 'full') {
        foreach (['field_location', 'field_event_description'] as $field_name) {
          $label = $entity->getFieldDefinition($field_name)->getLabel();
          $build[$field_name]['#title'] = $label;
        }
      }
    }
    

    I wonder if there is a better way to achieve different labels in form and view displays that is easier to disable, without having to write custom hooks.

Production build 0.69.0 2024