Skip to content

Instantly share code, notes, and snippets.

@MatthieuScarset
Created September 19, 2023 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthieuScarset/827e6153b4037db090a1b40a69aeb369 to your computer and use it in GitHub Desktop.
Save MatthieuScarset/827e6153b4037db090a1b40a69aeb369 to your computer and use it in GitHub Desktop.
Drupal - Programmatically delete field_group from form and view displays
<?php
$entity_type = 'node';
$group_name = 'the_group_id';
$context = 'form';
$form_displays = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->loadByProperties(['targetEntityType' => 'node']);
foreach ($form_displays as $display) {
$mode = $display->getMode();
$bundle = $display->getTargetBundle();
if (field_group_exists($group_name, $entity_type, $bundle, $context, $mode)) {
$group = field_group_load_field_group($group_name, $entity_type, $bundle, $context, $mode);
field_group_delete_field_group($group);
}
}
$context = 'view';
$view_displays = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->loadByProperties(['targetEntityType' => 'node']);
foreach ($view_displays as $display) {
$mode = $display->getMode();
$bundle = $display->getTargetBundle();
if (field_group_exists($group_name, $entity_type, $bundle, $context, $mode)) {
$group = field_group_load_field_group($group_name, $entity_type, $bundle, $context, $mode);
field_group_delete_field_group($group);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment