Score:-2

How can I get the field value from preprocess_field?

je flag

I would like to get the content of the field with is formatted text to manipulate it a bit (strip from HTML tags and limit) and display it as another variable in twig.

How can I get the value of the field?

This is what I've written so far, and I tried to output the value in the "if statement", but no luck so far:

function bootstrap4grow_preprocess_field(&$variables) {
  if ($variables['field_name'] == 'field_training_seo') {
  }
}
Score:4
ne flag

It's all in the $variables. Consider using Xdebug to find out yourself.

/**
 * Implements template_preprocess_field().
 */
function MYTHEME_preprocess_field(&$variables) {
  if ($variables['element']['#field_name'] === 'field_MYFIELD') {
    /** @var \Drupal\Core\Field\FieldItemList $items */
    $items = $variables['element']['#items'];
    foreach ($items->getValue() as $delta => $item) {
      // Depending on the field type you find the value in $item['value'],
      // $item['target_id'] etc.
      $value = $item['value'];
      // ....
    }
  }
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.