7

I want to give content editors the ability to add a Feed of some sort (Maybe Twitter, Tumblr, etc) to a particular node. I'd like to use Paragraphs because I want them to be able to drop this Feed in between various other pieces of content.

The Paragraph Bundle would just have one field to accept the resource URL. I would do all the necessary processing behind the scenes and would send the formatted content back to the node via a twig template.

I've got a version working using a block instead of a paragraph bundle, and I'd like to use the same custom module to allow for a paragraph, but I'm unsure in Drupal 8 how best to structure this.

The best I can come up with, is a preprocess function of some sort- preprocess_node, or preprocess_paragraph (which I think exist in D8) that would kick off the whole thing, but I was wondering if there was some kind of base Entity class that has a method I can overwrite or something. (Similar to how the BlockBase class has the "build" method.)

3
  • Welcome to Drupal Answers! I am sorry, but this site is not for looking for general ideas. We answer to well defined questions for which there is an objective answer.
    – apaderno
    Jan 16, 2016 at 3:39
  • This has a pretty clear answer to me (field formatters, as I wrote), I don't see this as too broad.
    – Berdir
    Jan 16, 2016 at 10:18
  • 3
    There's template_preprocess_paragraph(&$variables) and there's template_preprocess_paragraph__PARAGRAPH_TYPE(&$variables) as well.
    – leymannx
    Feb 6, 2017 at 8:17

1 Answer 1

2

A paragraph is a content entity with fields, that is viewed within its parent entity. It uses exactly the same mechanism to be displays as nodes, comments, and so on. Through field formatters.

You can implement your own field formatter plugin that transforms your (link?) field in any way you want. You receive field items and have to return a render array. That's the only restriction.

Like all plugins, it's a class with an annotation, in a specific namespace/folder. The default formatter for link fields is LinkFormatter. The most important method is viewElements().

0

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