Change which module group is the default selected

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 package (provides Laravel Collections)

<?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!

3 Likes

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.)

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.

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;
} );
1 Like

This works great. Thanks!