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!

1 Like

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