7

In Drupal 8, I was thinking that's a simple task but it's not :

function hmytheme_preprocess_field(&$variables, $hook) {
    $element = $variables['element'];
    if (isset($element['#field_name'])) {

        if($element['#field_name'] == "bar"){
             element['#items']->value ="foo";
             //or
             element['#items']->set(0,"foo");
        }
    }
}

this is not afecting the render. How to ? I don't whant to save the altered value

5

It's too late to change the field object, because the field is already prepared for rendering. You have to find the field value inside of the render array, for example in #markup, and change it there. Where you find the value depends how your field display is configured in the view mode.

While possible, there's often a better way. You could make your own formatter that returns the structure that you want. #text is a formatted text field, you could make a filter that transforms the text the way you want. Depends on what you want to do exactly.

(Comment from @Berdir)

See the drupal docs how to create a custom field formatter:

https://www.drupal.org/docs/8/creating-custom-modules/create-a-custom-field-formatter

  • 1
    wooookay, so it was in $variables['content']['field_bar][0]['#text']... In each hook it seam to be a different way... is there a logic or we just have to look inside the $variable to do this ? Or meabe it's not a good idea to change the render here? – Cawet Apr 25 '16 at 17:05
  • It's totally fine to change the render array in preprocess. That's what the hook is supposed to be used for. But you have to deal with several types of render elements and adjust your code accordingly. They change for every field type and the field formatter you choose and configure in the view mode. – 4k4 Apr 25 '16 at 18:29
  • While possible, there's often a better way. You could make your own formatter that returns the structure that you want. #text is a formatted text field, you could make a filter that transforms the text the way you want. Depends on what you want to do exactly. – Berdir Apr 25 '16 at 20:04
  • I wouldn't go this far to answer this question about theming. But if you are willing to make a custom module a field formatter or text filter plugin is the right thing to do. Not only the reason @Berdir mentioned, but also because your code becomes independent from the theme and can easily extend any site. – 4k4 Apr 25 '16 at 20:17
  • Can u add it as a sample code answer pls – Crazyrubixfan Feb 20 '18 at 9:41
1

As above mentioned you have to alter the #markup, example for field "field_block_iframe" below:

/**
 * Implements hook_preprocess_field().
 *
 * @param $variables
 * @param $hook
 */
function THEME_preprocess_field__field_block_iframe(&$variables, $hook) {
  $variables['items'][0]['content']['#markup'] = 'Going down, to the core.';
}

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.