# Change which module group is the default selected

**URL:** <https://community.wpbeaverbuilder.com/t/change-which-module-group-is-the-default-selected/8738>\
**Category:** Snippets\
**Created:** [May 20, 2019, 10:24am UTC](https://community.wpbeaverbuilder.com/t/change-which-module-group-is-the-default-selected/8738 "2019-05-20T10:24:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![codepuncher](https://community.wpbeaverbuilder.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@codepuncher](https://community.wpbeaverbuilder.com/u/codepuncher)\
**Post date:** [May 20, 2019, 10:24am UTC](https://community.wpbeaverbuilder.com/t/change-which-module-group-is-the-default-selected/8738/1 "2019-05-20T10:24:17Z")

</div>

Hi all,

Using the snippet below, you can change the default module group from “Standard Modules” to a choice of your own.

Requirements:  
PHP 7  
[illuminate/support](https://packagist.org/packages/illuminate/support) package (provides Laravel Collections)

```auto
<?php

declare(strict_types=1); // PHP 7 feature.

/**
 * Sets the project module group as the default instead of Standard Modules
 */
add_filter('fl_builder_content_panel_data', function (array $data): array {
    $group_name = 'My Custom Module Group';
    // Get the key of the projects module group.
    $group_key = sanitize_key($group_name);

    if (! $group_key) {
        return $data;
    }

    // Convert module views/groups into a Collection.
    $module_views = collect($data['tabs']['modules']['views']);
    // Get our project module group.
    $project_modules = $module_views->first(function (array $group) use ($group_key): bool {
        return $group_key === $group['handle'];
    });

    if (null === $project_modules) {
        return $data;
    }

    // Remove our project module group.
    $module_views = $module_views->reject(function (array $group) use ($group_key): bool {
        return $group_key === $group['handle'];
    });
    // Add our project module group to the start.
    $module_views->prepend($project_modules);
    $data['tabs']['modules']['views'] = $module_views->toArray();

    return $data;
});

```

If you don’t want to use Laravel Collections (or you can’t), then you can use the native PHP functions such as `array_filter($array)`.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![normsim6257](https://community.wpbeaverbuilder.com/user_avatar/community.wpbeaverbuilder.com/normsim6257/32/691_2.png) [@normsim6257](https://community.wpbeaverbuilder.com/u/normsim6257)\
**Post date:** [May 22, 2019, 1:43am UTC](https://community.wpbeaverbuilder.com/t/change-which-module-group-is-the-default-selected/8738/2 "2019-05-22T01:43:49Z")

</div>

Awesome! I was actually wondering how this could be done. I’ll have to take some time to convert to native PHP (not familiar with Laravel at all.)

---

<div class="post-metadata">

**Author:** ![Krijn](https://community.wpbeaverbuilder.com/letter_avatar_proxy/v4/letter/k/96bed5/32.png) [@Krijn](https://community.wpbeaverbuilder.com/u/Krijn)\
**Post date:** [July 16, 2020, 12:13pm UTC](https://community.wpbeaverbuilder.com/t/change-which-module-group-is-the-default-selected/8738/3 "2020-07-16T12:13:53Z")

</div>

Anyone converted it to native PHP?

I tried but i’m not experienced enough to make the convert.

Would love to get the native PHP code.

---

<div class="post-metadata">

**Author:** ![dwatts3624](https://community.wpbeaverbuilder.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dwatts3624](https://community.wpbeaverbuilder.com/u/dwatts3624)\
**Post date:** [October 18, 2020, 4:40pm UTC](https://community.wpbeaverbuilder.com/t/change-which-module-group-is-the-default-selected/8738/4 "2020-10-18T16:40:53Z")

</div>

Thanks @codepuncher for the solution here! Super helpful!

I know it’s been a while but I ended up converting this. I also added some additional logic to handle the separators since they end up getting pushed next to each other depending on what your setup looks like.

```
<?php

add_filter( 'fl_builder_content_panel_data', function ( array $data ): array {
    $group_name = RE_FL_BUILDER_GROUP;

    // Get the key of the projects module group.
    $group_handle = sanitize_key( $group_name );

    if ( !$group_handle ) {
        return $data;
    }

    $module_views = $data['tabs']['modules']['views'];

    // Find the index of the matching group
    $target_index = null;
    foreach( $module_views as $key => $view ) {
        if ( isset( $view['handle'] ) && $view['handle'] == $group_handle ) {
            $target_index = $key;
            break;
        }
    }

    // If the index can't be found exit
    if ( !$target_index ) {
        return $data;
    }

    // Remove the new default and add it back in to the start
    $new_default = $module_views[$target_index];
    unset( $module_views[$target_index] );
    array_unshift( $module_views, $new_default );

    // Now let's clean up the separators
    $new_views = array_filter( $module_views, function( $item ) {
        return !( isset( $item['type'] ) && $item['type'] == 'separator' );
    } );

    // Create a new separator objects. Since splice will take a full array
    // we need to nest our new value
    $separator = [ [
        'type' => 'separator'
    ] ];

    // Add separator after the first item
    array_splice( $new_views, 1, 0, $separator );

    // Add separator before the last item
    array_splice( $new_views, count( $new_views ) - 1 , 0, $separator );

    $data['tabs']['modules']['views'] = $new_views;

    return $data;
} );
```

---

<div class="post-metadata">

**Author:** ![robfuller](https://community.wpbeaverbuilder.com/user_avatar/community.wpbeaverbuilder.com/robfuller/32/4494_2.png) [@robfuller](https://community.wpbeaverbuilder.com/u/robfuller)\
**Post date:** [August 8, 2024, 8:26pm UTC](https://community.wpbeaverbuilder.com/t/change-which-module-group-is-the-default-selected/8738/5 "2024-08-08T20:26:43Z")

</div>

This works great. Thanks!
