Skip to content

Instantly share code, notes, and snippets.

@NikLP
Last active March 3, 2025 17:01
Show Gist options
  • Select an option

  • Save NikLP/88f866ceea2ada1c93cc4161254511b9 to your computer and use it in GitHub Desktop.

Select an option

Save NikLP/88f866ceea2ada1c93cc4161254511b9 to your computer and use it in GitHub Desktop.
Conditionally hide output from a field_group via custom module
<?php
/**
* Implements hook_field_group_build_pre_render_alter().
* See https://git.drupalcode.org/project/field_group/-/blob/4.x/field_group.api.php
*/
function MYMODULE_field_group_build_pre_render_alter(&$element) {
// Only act on node view displays
if (empty($element['#node']) || !($element['#node'] instanceof NodeInterface)) {
return;
}
$node = $element['#node'];
if (!$node->hasField('directory_org_type')) {
return;
}
// Check if our controlling field has the specific value.
if (
!$node->get('directory_org_type')->isEmpty() &&
$node->get('directory_org_type')->value !== 'educational_setting'
) {
// Hide the specific field group if NOT educational_setting above.
if (isset($element['group_educational_setting'])) {
$element['group_educational_setting']['#access'] = FALSE;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment