0

I'm going this route as I can't get a view to behave as needed.

In simple terms I have

content type 1 containing custom field 1 with a custom formatter

content type 2 containing custom field 2 with a custom formatter

content type 3 containing custom field 3 with a custom formatter

Each node will display correctly using the formatter if the node is viewed directly so I'm happy with that, however for some of the pages I need to display I need to use all (or partial) content from these content types as a single "entity". What I'm trying to do is generate a response that in effect wrap the output of all 3 sets of fields into a single output that can then be pushed back into Drupal's normal page processing so that other modules can work on the aggregated output before the HTML rendering phase. Additionally, if possible I'd like to be able to use different formatters other than the field default for the fields depending on the use context so a web page used the default formatter, a rest feed used formatter 2, an XML feed used formatter 3

I'm using Drupal 8.9 but will soon be moving to Drupal 9.

6
  • It's unclear how the content types are connected. First you need a data model and then you can write custom code for it or build an SQL query in Views.
    – 4uk4
    Commented Jun 16, 2020 at 15:05
  • Thanks, it's not the data model that I have the issue with, I've build relevant relationships within the content types so I can easily get all the data into the controller in code. What I'm trying to do is to get these chunks of data out again so I may be wanting name & "address" from content type 1, "job titles" from content type 2, "family history" from 3 and so on, however (as an example), the dates on the job titles don't use a default Drupal / PHP format so have to go through the custom formatter. Commented Jun 16, 2020 at 15:16
  • 2
    OK, then you would start with the parent content type which references the others and configure a custom view mode which displays the other referenced entities in their own custom view modes containing the fields you want. In Views you would build the relationships and then you can mix fields of all related entities.
    – 4uk4
    Commented Jun 16, 2020 at 15:23
  • So no way to do it outside of views? The idea behind a custom controller was to give the flexibility I can't get in views (spent a while trying different options and then started looking for an alternative) Commented Jun 16, 2020 at 15:35
  • 2
    You can use a custom controller to query the entities, but then it's still a good idea to use custom view modes to display the formatted fields. You can print isolated fields but it is not recommended to do this for node fields, see drupal.stackexchange.com/questions/240002/…
    – 4uk4
    Commented Jun 16, 2020 at 15:51

0

Your Answer

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

Browse other questions tagged or ask your own question.