Skip to main content
Menu
Offcanvas

How to hook a paragraph form

Find out how to hook a form from the paragraph module in the BO or on the front to add classes, a placeholder, etc. The method is identical to the hooks for Form API forms but the hook is specific to this module.

Hook paragraph Drupal

It's easy to hook a form with hook_form_alter. With this hook, you can add classes to elements, modify the text of elements, etc.

The paragraph module allows you to make groups of repeatable fields. You therefore need another hook to modify this field group. We can therefore use:

hook_field_widget_complete_WIDGET_TYPE_form_alter

You need to replace WIDGET_TYPE with paragraphs (or with entity_reference_paragraphs if you use the legacy Paragraphs widget).

Example code to add suffix and classes

function paragraphs_field_widget_single_element_paragraphs_form_alter(&$element, &$form_state, $context) {
    //dump($element);
    $element['subform']['field_annee']['#attributes']['class'] = 'col-md-6';
    $element['subform']['field_url']['#attributes']['class'] = 'col-md-6';
    $element['subform']['field_annee']['#prefix'] = '<div class="row">';
    $element['subform']['field_url']['#suffix'] = '</div>';
}

To see what you can modify, simply do a

dump($element);

hook to target the entire paragraph block

hook_field_widget_single_element_WIDGET_TYPE_form_alter()

With this hook, you can modify the block label, etc.

Example code to change the field group title

function paragraphs_field_widget_complete_paragraphs_form_alter(&$element, &$form_state, $context) {

    $element['widget']['#title'] = 'Publication/réalisation';
    //dump($element);
}

You need to replace WIDGET_TYPE with paragraphs (or with entity_reference_paragraphs if you use the legacy Paragraphs widget).

Other useful resources

Add new comment

Similar blog posts

How to delete local changes with git that you haven't committed ?

LIRE LA SUITE