Advertising sustains the DA. Ads are hidden for members. Join today

Webform Cookbook

How to disable the Webform settings collapsible details pane for a webform reference field?

Last updated on
28 February 2025

Up to Drupal 9 you can use  hook_field_widget_form_alter()

/**
 * Implements hook_field_widget_form_alter().
 */
function CUSTOM_MODULE_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  /** @var \Drupal\Core\Field\FieldItemListInterface $items */
  $items = $context['items'];
  $field_definition = $items->getFieldDefinition();
  if ($field_definition->getType() !== 'webform') {
    return;
  }
  if ($items->getEntity()->getEntityTypeId() !== 'node') {
    return;
  }
  
  // Hide Webform field settings.
  $element['settings']['#access'] = FALSE;
}

In Drupal 10 you need to use hook_field_widget_single_element_form_alter() which is similar:

/**
 * Implements hook_field_widget_single_element_form_alter().
 */
function CUSTOM_MODULE_field_widget_single_element_form_alter(&$element, FormStateInterface $form_state, $context) {
  /** @var \Drupal\Core\Field\FieldItemListInterface $items */
  $items = $context['items'];
  $field_definition = $items->getFieldDefinition();
  if ($field_definition->getType() !== 'webform') {
    return;
  }
  if ($items->getEntity()->getEntityTypeId() !== 'node') {
    return;
  }
  
  // Hide Webform field settings.
  $element['settings']['#access'] = FALSE;
}

And if your reference field is directly inside a node, you can use a hook_form_alter() like this:

/**
 * Implements hook_form_alter().
 */
function CUSTOM_MODULE_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if (isset($form['webform'])) {
    $form['webform']['widget'][0]['settings']['#access'] = FALSE;
  }
}

If your webform is inside of paragraphs and you want to limit this to certain types you can use hook_field_widget_single_element_form_alter() like this: 

/**
 * Implements hook_field_widget_single_element_form_alter().
 */
function CUSTOM_MODULE_field_widget_single_element_form_alter(&$element, FormStateInterface $form_state, $context) {
  /** @var \Drupal\Core\Field\FieldItemListInterface $items */
  $items = $context['items'];
  $field_definition = $items->getFieldDefinition();
  if ($field_definition->getType() !== 'webform') {
    return;
  }
  $bundle = $items->getEntity()->bundle();
  if ($bundle == 'component_form_cta' || $bundle == 'component_full_width_form') {
    // Hide Webform field settings.
    $element['settings']['#access'] = FALSE;
  }
}

Help improve this page

Page status: No known problems

You can: