I am trying to figure out Drupal 8's $variables array ...

So I have a MYTHEME.theme file, complete with a working MYTHEME_preprocess_field() method (I know it works because I use it for other fields that need customization).

Right now I'm focusing on the title of an article displayed on the front page of my site. I can access it with the following:

if ($variables['field_name'] == 'title') {
    $title = $variables['element'][0]['#context']['value'];
    // ...

What I want to do is replace all instances of ® (the registration mark) with <sup>®</sup>. However, setting this value in the array to something else does not change it on the site.

Is there something I'm missing? A few sources I found said to alter (or add) the ['#markup'] element in the array, but there's a dearth of information about this online.

How do I change the value of a field? This seems like it should be something basic and easy to do.


In Drupal 8 front page is a view called frontpage if the view is not modified it should show: "Content" by default (instead of fields). If so the following can be applied to change each node title

function THEMENAME_preprocess_node(&$variables) {
  //for views that show: "Content" (not fields)
    $variables["label"][0]["#context"]["value"] .= " end of title";

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.