0

I use paragraphs with my content type. I need to show weight (order) value of each paragraph in my twig template. I can change weight with select box when I edit the node, but I can't figure out how to get it in twig.

I thought it would be something like this:

function ws_preprocess_paragraph(&$variables) 
{
    $par = $variables['paragraph'];
    $myweight = $par->weight;
}

Please help)

  • The weight you change in the node edit form is not stored anywhere. It is used to rearrange the items of a multi value field (not only for paragraphs) and renumber the deltas of the field. – 4k4 May 15 '17 at 8:01
  • Thanks! Is there anyway to get this deltas and calculate the weights? – 4eveZIP May 15 '17 at 8:45
  • I don't know what you want to do, but you find the deltas in the field, not the referenced paragraphs. – 4k4 May 15 '17 at 8:54
  • The aim of all this to show order number of paragraphs 0, 1, 2, 3, 4 in paragraph twig template. May be some other way? – 4eveZIP May 15 '17 at 8:55
  • Use {{ loop.index0 }} in a field template like field--field-paragraph.html.twig – 4k4 May 15 '17 at 9:00
0

You'll find it here for its fields:

$variables['content']['FIELD_NAME']['#weight'];

and then again in every item of a field:

$variables['content']['FIELD_NAME'][0]['#weight'];
$variables['content']['FIELD_NAME'][1]['#weight'];
$variables['content']['FIELD_NAME'][2]['#weight'];
$variables['content']['FIELD_NAME'][..]['#weight'];
  • Thanks for your answer. It's not working. $variables['content'] doesn't have #weigth key and $par->getDisplayOptions('view') displays error Call to undefined method Drupal\paragraphs\Entity\Paragraph::getDisplayOptions() – 4eveZIP May 15 '17 at 8:50
  • Sorry .. answered to quick and without verification of the code ;) Updated! – rémy May 15 '17 at 9:11

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.