Altering a Paragraphs Form

Last updated on
10 November 2024

To Alter a Paragraphs Form by a Plugin you have to activate the module Plugin Form Alter to be used as replacement of hook_form_alter().
In order to alter a Paragraphs form you need to create a new class in your [modulename]/src/Plugin/FormAlter folder.
Your class will have to extend the FormAlterBase class and will have to use the @ParagraphsFormAlter annotation, as shown below.
You can alter a Paragraphs form by using the paragraph_type property. 

<?php

namespace Drupal\my_module\Plugin\FormAlter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\pluginformalter\Plugin\FormAlterBase;

/**
 * Class MyModuleParagraphsFormAlter.
 *
 * @ParagraphsFormAlter(
 *   id = "my_module_paragraphs_form_alter",
 *   label = @Translation("Altering title_text paragraphs form."),
 *   paragraph_type = {
 *    "title_text"
 *   },
 * )
 *
 * @package Drupal\my_module\Plugin\FormAlter
 */
class MyModuleParagraphsFormAlter extends FormAlterBase {

  /**
   * {@inheritdoc}
   */
  public function formAlter(array &$form, FormStateInterface $form_state, $form_id) {
    // Add some dummy markup.
    $form['subform']['dummy_markup'] = [
      '#markup' => '<h3>Here we go</h3>',
    ];
  }

}
?>

Help improve this page

Page status: No known problems

You can: