How to pass variable of paragraphs type to another preprocess function I want to override the fontawesomeicons.html.twig (Fontawesome Module). But I only want to do this for "my-very-special-paragraphs-type" and not in general. I think it is a good idea to declare a variable of "my-very-special-paragraphs-type" in a preprocess function and use the variable in the twig-file. 

function mytheme_preprocess() {
  $var = my-very-special-paragraphs-type
}


the fontawesomeicons.html.twig:  

{% if var == 'my-very-special-paragraphs-type' %}  
&lt;div class="fontawesome-icons pb-4 t-a-c"&gt;
    &lt;span class="fa-stack fa-4x"&gt;{{ icons }}&lt;/span&gt;
&lt;/div&gt;
{% else %}
&lt;div class="fontawesome-icons"&gt;
   {% if layers == '1' %}
      &lt;span class="fa-layers fa-fw"&gt;{{ icons }}&lt;/span&gt;
   {% else %}
      {{ icons }}
   {% endif %}
&lt;/div&gt;
{% endif %}


But I do not know how to do this. Can anybody help?

This is the hook of fontawesome module:
This was quite a ride.

First of all: I didn't got this running in the theme. I needed to create a custom module AND move its implementation of the fontawesomeicons hook last.

Next thing was: hook_preprocess_fontawesomeicons doesn't contain a hint of the parent entity the icons are used in. So I used hook_preprocess_field first to pass the paragraph type over to drupal_static() and retrieved it back then in hook_preprocess_fontawesomeicons.

/**
 * Implements hook_module_implements_alter().
 */
function MYMODULE_module_implements_alter(&amp;$implementations, $hook) {

  if ($hook == 'fontawesomeicons') {

    // Run MYMODULE_preprocess_fontawesomeicons() last.
    $group = $implementations['MYMODULE'];
    unset($implementations['MYMODULE']);
    $implementations['MYMODULE'] = $group;
  }
}

/**
 * Implements hook_preprocess_field__FIELD_TYPE().
 */
function MYMODULE_preprocess_field__fontawesome_icon(&amp;$variables) {

  if ($variables['element']['#entity_type'] == 'paragraph') {

    $paragraph = $variables['element']['#object'];

    $ptype = &amp;drupal_static('paragraph_type', TRUE);
    $ptype = $paragraph-&gt;getType();
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function MYMODULE_preprocess_fontawesomeicons(&amp;$variables) {

  $ptype = &amp;drupal_static('paragraph_type', FALSE);

  if ($ptype) {
    $variables['paragraph_type'] = $ptype;
  }
}


Inside any variation of fontawesomeicons.html.twig you now can:

{% if paragraph_type == 'my-very-special-paragraphs-type' %}
  {# do your magic here #}
{% endif %}




I never heard of drupal_static() until today. Read more:


How do I pass data between hooks that don't interact?
Share a value between two different hooks