Create dynamic text fields on form based on other entities I've created an entity which has fields that users need to fill out. As part of that entity there is a reference field back to a content type 'food'.
The 'food' content type has it's own entity reference called 'sections'.
When a user selects a 'food', i want to dynamically add each 'section' as a title with a textarea box underneath which gets stored against the original entity.
Considered using paragraphs as the 'sections' but they need to be reusable on different 'food' nodes. Also checked the dynamic fields module but this doesn't look like it's well supported.
Fallback approach is going to be to programmatically create a field for each 'section' item as they are added, but with this we could end up with hundreds of fields.
Any ideas on how else this can be done programmatically?
Paragraphs is what you want. What you are describing is Paragraphs' exact use case. We use Paragraphs as a staple of our platform because of its re-usability.
Assigning an existing Paragraph to an Entity Reference Revisions field on a node is as simple as:
// Note that the value will be a nested array, even if cardinality is 1.
$node-&gt;get('field_food')&gt;setValue([
  [
    'target_id' =&gt; $paragraph-&gt;id(),
    'target_revision_id' =&gt; $paragraph-&gt;getRevisionId(),
  ],
]);
$node-&gt;save();

// Another example: assigning multiple references to a single field.
$node-&gt;get('field_myfield')&gt;setValue([
  [
    'target_id' =&gt; $paragraph1-&gt;id(),
    'target_revision_id' =&gt; $paragraph1-&gt;getRevisionId(),
  ],
  [
    'target_id' =&gt; $paragraph2-&gt;id(),
    'target_revision_id' =&gt; $paragraph2-&gt;getRevisionId(),
  ],
  [
    'target_id' =&gt; $paragraph3-&gt;id(),
    'target_revision_id' =&gt; $paragraph3-&gt;getRevisionId(),
  ],
]);
$node-&gt;save();

If you need to generate a paragraph programmatically, that is also easy:
// Note that I'm using the DIC for demonstration's sake.
// Be sure to use dependency injection and other best practices in your project.
$storage = \Drupal::entityTypeManager()-&gt;getStorage('paragraph');
$paragraph = $storage-&gt;create([
  'type' =&gt; 'food',
  'title' =&gt; 'A title',
  'field_a' =&gt; 'a value',
]);
$paragraph-&gt;save();

You can then assign this newly created paragraph to an Entity Reference Revisions field using the method I mentioned above.