Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

in this post, i'll show you how to add loop index in paragraph.html.twig using hook_preprocess_field(&$variables){}

 * @param $variables
function mytheme_preprocess_field(&$variables) {
  if($variables['field_name'] == 'field_paragraph_name'){
    foreach($variables['items'] as $idx => $item) {
      $variables['items'][$idx]['content']['#paragraph']->index = $idx;

and then in your paragraph.html.twig file you can use {{ paragraph.index }}

{% if paragraph.index == 0 %}
  <li class="accordion-item is-active">
{% else %}
  <li class="accordion-item">
{% endif %}


Tags :


Add new comment

Restricted HTML


Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.


Contact Us

All the content is FREE but I still need your help


Become a patreon