1

I need to link/sync a paragraph with another field on the parent node of this paragraph.

Background:

All my nodes have some node-type-specific fields for strict-structured data (e.g. a product node has product image & price; a location node has a geolocation field). All nodes share one huge, repeating paragraph "generic content" field with a lot of paragraph types for unstructered data (marketing texts, emotional images, bullet points,...). Basically that paragraph is used as a sort of site building tool.

Requirement:

I now need to display the node's structured data fields within the "generic content" paragraph field, so that editors can mix and move the structered content with the generic content paragraphs.

Didn't work:

My initial idea was to create a paragraph with an entity reference to field types where editors could select the basefield. However, that didn't really work because I can't limit the autocomplete to fields of the current node, leading to a unusable list with a lot of invalid entries.

Question:

Is there any way to create references to fields within a node? It has to be a mouse- and editor-friendly solution, my editors are trained in the Drupal Admin, but they have no access to code.

0

1 Answer 1

2

Technically, you could add a reference field to field config entities. But you can't limit the options there and I'm sure you don't want to allow all the fields, and you need custom theming logic for this anyway. That would just allow you to select the field name.

You can't link to a field value, the combination of entity and field doesn't have its own identifier.

If I understand you correctly, then the simplest option is to make a custom list field with allowed values like this:

field_name_a|Field A field_name_b|Field B

With as many fields as you need. You could also register a allowed options callback and try to dynamically limit the options based the current page/node type, but that's a bit tricky.

Then you need custom logic in preprocess/twig templates for that paragraph type that gets the value for the selected field from the parent of the paragraph and renders it

2
  • Thanks for pointing me to list fields, took a look at options.api... Couldn't I simply create an empty dummy text list and use hook_options_list_alter and modify/add stuff to the $options array? I'm new to Drupal coding, and the parameters of the hook are easier to understand than those from callback_allowed_values_function
    – Hudri
    Commented Aug 16, 2017 at 8:55
  • I'm not sure, you would have to try. One thing, if I see that correctly is that this hook only affects the widget, aka when editing the content. So it doesn't seem like a good option to add options there as they wouldn't exist when viewing and also might not pass validation.
    – Berdir
    Commented Aug 16, 2017 at 9:17

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.