drupalfielddrupal-8form-api

Drupal 8: How do I create a content reference/textarea compound field?


I have a company content type being used on a website-in-progress. Companies consist of a company name, name sorting mechanism, logo image and URL. I have a requirement to show companies taking part in different use cases in various parts of the website.

Challenge:

In the past I used a company content type for a "corporate members" list on a page, also assigning a "member level" field. But then, companies started being listed as event sponsors with specifics for what they were sponsoring, also with different membership levels in a "corporate giving club". It was messy while added/included in the old company content type. It caused confusion for content editors and developers.

Possible Solution:

In other content types, create compound fields such as "Sponsors", "Corporate members", "Donors", select lists with company entities, or sponsorship detail text areas with company entities. That may keep companies flexible and not messy (keeping company just a company).

Progress:

I found the Double field module, splitting core fields up into two separate parts. This is almost what I'm looking for, providing compound field sets. However, it won't allow references to be selected and is out of the scope of this project per this issue. I'm finding a lot of Drupal 7 examples, but not much Drupal 8 yet.

I'm starting to dig into the examples for developers module for inspiration and will post a solution unless you beat me to it. I'm also open to alternative ideas.

What is the best way to proceed? Specific code examples are not required. I would accept clues that lead me to a final working solution.


Solution

  • The solution turned out to be simpler than creating a custom module.

    The Field Collection module allowed for the simple creation of compound fields containing any number and most (if not all) field types, including reference fields. It's also available as a Drupal 8 module (alpha at the time of writing, seems to work fine).