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)

6
  • 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.
    – 4uk4
    May 15, 2017 at 8:01
  • Thanks! Is there anyway to get this deltas and calculate the weights?
    – 4eveZIP
    May 15, 2017 at 8:45
  • I don't know what you want to do, but you find the deltas in the field, not the referenced paragraphs.
    – 4uk4
    May 15, 2017 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, 2017 at 8:55
  • Use {{ loop.index0 }} in a field template like field--field-paragraph.html.twig
    – 4uk4
    May 15, 2017 at 9:00

3 Answers 3

0

The weight of a paragraph is not stored in the paragraph itself, but in the host field.

So if you need to get the weight of a paragraph from within the paragraph (e.g. from within hook_preprocess_paragraph()), you have to go upwards to the parent entity and find your the weight there:

function MYMODULE_preprocess_paragraph(&$variables) {
  $paragraph = $variables['paragraph'];
  $parent = $paragraph->getParentEntity();
  // get all paragraph IDs sorted by weight
  $weights = array_map( function($a) {return $a['target_id'];}, $parent->get($paragraph->get('parent_field_name')->value)->getValue());
  // find the weight of my paragraph ID
  $weight = array_search($paragraph->id(), $weights);
  // ...do stuff with $weight... e.g. assign it to a Twig variable
  // $variables['weight'] = $weight;
}

Please note that the weight is easier to get if you use the paragraph host field template instead (see @4k4's comment below the original question):

Use {{ loop.index0 }} in a field template like field--field-paragraph.html.twig

0

I'm sorting fields in paragraph template (paragraph.html.twig) in a next way:

{% set items = content|sort((a, b) => a['#weight'] <=> b['#weight']) %}

Note: that there could be "odd" fields like "_layout_builder", so to avoid rendering items without weight just use condition:

{% for item in items %}
  {% if item['#weight'] is defined %}
    {{ item }}
  {% endif %}
{% endfor %}
-1

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'];
2
  • 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, 2017 at 8:50
  • Sorry .. answered to quick and without verification of the code ;) Updated!
    – rémy
    May 15, 2017 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.