0

Hello it is possible to order the output of the paragraphs for example

Type of content Blog I create a blog details field that calls the type of paragraphs and there I select image and description

In my content it goes in this order

  • 1 Image
  • 2 Description

In the view I call that field blog details and they appear to me but based on the order of the node

But I don't want it to look like this

  • 1 description
  • 2 image

and always look in that order

  • Why not make separate fields for image and description of they're always in a specific order? – sonfd Jul 3 at 20:00
  • Hello, within a paragraph field I can choose between several types of paragraphs, in this case I have an image paragraph and a description paragraph and a single widget button that allows me to add which one I want like this image i.stack.imgur.com/9ofvV.png – user99451 Jul 3 at 20:14
  • Oh, I misunderstood. Sorry. Are we you trying to show only the description and image paragraphs in your view? In your image I see 5 total paragraphs, are you trying to show a subset of the total paragraphs added to your field, and show them in a specific order? – sonfd Jul 4 at 10:17
  • Hello exactly in the view it is shown according to the order that was saved in the node But I want a different order of sight – user99451 Jul 4 at 14:22
0

I don't believe there's a way to sort the values of a field at the view level. However, you can easily get the results you want by creating a custom field formatter and then using it to render your field in the view.

You can read more about field formatters by reading the Create a custom field formatter documentation on d.o.

Create a custom formatter that extends the formatter you're using now. I'm guessing you're using the default ERR formatter so you'll want to extend Drupal\entity_reference_revisions\Plugin\Field\FieldFormatter\EntityReferenceRevisionsEntityFormatter.

You'll need to override viewElements() or getEntitiesToView() - I'd override getEntitiesToView().

protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
  // Let the parent class get all entities in the field first.
  $all_entities = parent::getEntitiesToView($items, $langcode);
  $my_entities = [];

  // Get first Description paragraph.
  $desc = $this->getFirstOfType($all_entities, 'MY_DESCRIPTION_BUNDLE');
  if (!empty($desc)) {
    $my_entities[] = $desc;
  }

  // Get first Image paragraph.
  $img = $this->getFirstOfType($all_entities, 'MY_IMAGE_BUNDLE');
  if (!empty($img)) {
    $my_entities[] = $img;
  }

  return $my_entities;
}

protected function getFirstOfBundle($entities, $bundle) {
  foreach($entities as $entity) {
    if ($entity->bundle() == $bundle) {
      return $entity;
    }
  }

  return FALSE;
}
| improve this answer | |
  • Hi thanks for answering I wish I could do it from the views but I will try to use your example – user99451 Jul 5 at 23:06

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.