Currently, I already built a custom block programmatically, now I want to add a paragraph-type field programmatically to this block but don't know how to do it. The reason is that I want to add/remove the paragraph-type field dynamically in the form block(just like the repeater field type in WordPress). Something like

$form['article_paragraphs'] = [
'#type' => 'paragraph',
'#title' => $this->t('Create article feature'),
'#description' => $this->t('This field will allow you to add/remove feature(contain the title, description)'),
'#default_value' => $this->configuration['article_paragraphs'],

Thanks in advance.

  • Paragraphs are entities and can only be added to other entities, in this case a custom block type.
    – 4uk4
    Commented Mar 11 at 7:51
  • @auk4 Could you elaborate in more detail and provide me an example? Thank you
    – huyleuit
    Commented Mar 12 at 8:57
  • Start with /admin/structure/block-content and add a block type with a paragraph field. Don't try to deploy this in code, export the config and put the yaml files in the module config install folder (remove the uuid line).
    – 4uk4
    Commented Mar 12 at 9:21
  • @auk4 I already add block type with a paragraph field inside, but then how I can programmatically add more fields (textfield, email, fieldset...) to this block type?
    – huyleuit
    Commented Mar 12 at 10:19


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.