1

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' %}  
<div class="fontawesome-icons pb-4 t-a-c">
    <span class="fa-stack fa-4x">{{ icons }}</span>
</div>
{% else %}
<div class="fontawesome-icons">
   {% if layers == '1' %}
      <span class="fa-layers fa-fw">{{ icons }}</span>
   {% else %}
      {{ icons }}
   {% endif %}
</div>
{% endif %}

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

This is the hook of fontawesome module: enter image description here

0

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(&$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(&$variables) {

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

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

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

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

  $ptype = &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:

  • I can not make it run. I install the module but it is not working. Not with or without s. I use it in a nested paragraph and webfont with CSS. I have cleared allways the cache and tried both paragraph types. – lesley n. Sep 24 '18 at 8:30
  • @lesleyn. – I got it working that way. Would you upload your resulting module maybe on GitHub and ping me the link here, so I can review? – leymannx Sep 24 '18 at 8:50

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.