How do I pass a theme variable from a template to another? Several fields in a node need to be displayed in a particular way depending upon which newsletter body they are a part of. How do I pass a variable from the enclosing newsletter body to enclosed fields so that I can inform the field of its context and control the way the field is displayed?

I have a theme that has preprocess functions both for the newsletter body and for fields contained within the body.  

I want to set a variable in the body preprocess function, and its related twig template, that can be inspected by the field preprocess function, and/or its own template.  

In other words, I want each field to be aware of the body context in which it is being rendered.  

Specifically, I have mytheme_preprocess_simplenews_newsletter_body() that sets a variable ($variables['summons'] = 'placeholder';) which can be inspected (and also set) by the theme's simplenews-newsletter-body.html.twig template, but (because it's in a different render array) is subsequently neither visible to mytheme_preprocess_field() nor field--node--title.html.twig.    

How do I communicate between the two render arrays?   

Note that


There are apparently global variables like $db_is_active and
$is_admin.
Sometimes, it is possible to achieve this kind of thing by cascading CSS
class attributes from a containing element to a contained element. This is not generally satisfactory and in this particular case, it won't wash on different target mail clients unpredictable treatment of CSS.
The solution I am working with is to use drupal_static(); simplified essentials in code below - take care with the &amp;ampersands. It is giving me control where I need it.  

So, using this approach, I can - taking their global context into account -  per field, set variables in the usual way e.g. in mytheme_preprocess_field() subsequently to be used in a related template like field--node--title--agenda.html.twig

function mytheme_preprocess_simplenews_newsletter_body(&amp;$variables) {
  // prepare to alter static var ...
  // get a reference binding to static var
  $var = &amp;myvariable_function();
  // alter static var
  $var = 'simplenews_newsletter';
}

function mytheme_preprocess_field(&amp;$variables, $hook) {
  // prepare to read static var
  // assign static var
  $var = myvariable_function();
  // $element is the render array for the field
  $element = $variables['element'];
  // filter those cases where it matters that we're theming a newsletter
  if ($element['#view_mode'] == 'email_html' and $var == 'simplenews_newsletter') {
    // Do stuff here like setting a variable for twig ...
    $variables['summons'] = 'some text';
  }
}
function &amp;myvariable_function() {
  $var = &amp;drupal_static(__FUNCTION__);
  if (!isset($var)) {
    // generate contents of static variable
    $var =  'initial_value';
  }
  return $var;
}


I am sure that there are other ways of communicating common context between different levels of a page's render hierarchy. Let me know how you'd do it.