Last active
March 3, 2025 17:01
-
-
Save NikLP/88f866ceea2ada1c93cc4161254511b9 to your computer and use it in GitHub Desktop.
Conditionally hide output from a field_group via custom module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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