1

I expected to be able to do it with THEME_preprocess_field, but the data at that point seems to have only the content's markup and not the label. I would need an instance where I can change both, because there's some mutual conditioning between them.

I also tried with THEME_node_view to no effect.

Note: I want to do this in a theme, not a custom module.

3
  • It doesn't, I would need a way to do it in the custom theme, not in a custom module
    – alotropico
    Dec 19 '20 at 7:07
  • 3
    Alter hooks also work on themes.
    – No Sssweat
    Dec 19 '20 at 11:19
  • 1
    This one does not.
    – 4k4
    Dec 20 '20 at 9:43
2

I expected to be able to do it with THEME_preprocess_field, but the data at that point seems to have only the content's markup

There are more template variables:

field.html.twig:

{#
/**
 * @file
 * Default theme implementation for a field.
 * 
 * ...
 *
 * Available variables:
 * - attributes: HTML attributes for the containing element.
 * - label_hidden: Whether to show the field label or not.
 * - title_attributes: HTML attributes for the title.
 * - label: The label for the field.
 * - multiple: TRUE if a field can contain multiple items.
 * - items: List of all the field items. Each item contains:
 *   - attributes: List of HTML attributes for each item.
 *   - content: The field item's content.
 * - entity_type: The entity type to which the field belongs.
 * - field_name: The name of the field.
 * - field_type: The type of the field.
 * - label_display: The display settings for the label.
 *
 * @see template_preprocess_field()
 *
 * @ingroup themeable
 */
#}

So you can change for example label:

mytheme.theme:

/**
 * Implements hook_preprocess_HOOK() for field.html.twig.
 */
function mytheme_preprocess_field(&$variables) {
  $variables['label'] = 'foo';
}

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.