1

I would like to display only the selected widgets to the particular user group. Also is there a way to disable default widgets like craft news, Recent entries etc?

4
  • Do you mean you want to display a fixed selection of widgets to that user group and prevent them from changing them? Or just limit which widgets are available to which specific user groups?
    – MoritzLost
    Dec 9 '21 at 10:38
  • @MoritzLost I would like to display a fixed set of dashboard widgets to the particular user group. and if possible I would like to remove the new widget add option on for those user groups.
    – Parthiban
    Dec 14 '21 at 9:45
  • @Partihiban I think my answer addresses both of those, or is something missing?
    – MoritzLost
    Dec 14 '21 at 9:53
  • 1
    @MoritzLost Yes it works. Thanks.
    – Parthiban
    Dec 14 '21 at 10:22
2

The dashboard is surprisingly unflexible. There's no permission to control whether users are allowed to change their dashboard widgets, no default widget options for new users and no access controls for widgets. But you can use the Dashboard service to change the dashboard widgets for a given user, and hide the dashboard options using CSS.

Hiding dashboard options from users

First install the Control Panel Body Classes plugin to be able to target specific user groups in CSS. Then you can include some CSS (using a site module) in the Control Panel to hide the Dashboard configuration from specific user groups.

The following snippet hides the dashboard configuration (the New Widget dropdown and the configuration button) from the user group collaborator:

use Craft;

if (Craft::$app->getRequest()->getIsCpRequest()) {
    /** @var craft\web\View $view */
    $view = Craft::$app->getView();
    $view->registerCss(<<<CSS
        body.usergroup-collaborator.currentsection-dashboard #action-button {
            display: none;
        }
    CSS);
}

If you only want to limit the widgets that are available for a particular user group, you can hide those in the New widget dropdown using CSS as well. You can even enforce this on the server-side with an event hook for Dashboard::EVENT_BEFORE_SAVE_WIDGET. The callback can prevent saving a widget that the current user shouldn't have access to.

Changing dashboard widgets programmatically

Now this particular user group can't modify their own dashboard widgets any more, but they're still stuck with the default widgets. To change them, you can use event handlers to force a particular set of widgets. You can do this either upon registration or login (I prefer the latter, in case you want to change the available widgets at some point).

use craft\web\User;
use yii\base\Event;
use yii\web\UserEvent;

Event::on(
    User::class,
    User::EVENT_AFTER_LOGIN,
    fn(UserEvent $e) => $this->ensureDefaultDashboardWidgets($e->sender),
);

The function needs to remove the default widgets and then register the predefined set of widgets you want to provide to that user group:

use Craft;
use craft\web\User;

public function ensureDefaultDashboardWidgets(User $user): void
{
    /** @var craft\elements\User */
    $userIdentity = $user->getIdentity();
    /** @var craft\services\Dashboard $dashboard */
    $dashboard = Craft::$app->dashboard;

    $userGroups = $userIdentity->getGroups();
    $isCollaborator = count($userGroups) === 1 && $userGroups[0]->handle === 'collaborator';
    if (!$isCollaborator) return;

    // remove all existing widgets
    $existingWidgets = $dashboard->getAllWidgets();
    foreach ($existingWidgets as $widget) {
        $dashboard->deleteWidget($widget);
    }

    $defaultWidgets = [
        $dashboard->createWidget([
            'type' => 'craft\\widgets\\MyDrafts',
            'settings' => ['limit' => 10],
        ]),
        $dashboard->createWidget('modules\\MyCustomWidgetsModule\\widgets\\MyCustomWidget'),
    ];

    // add all default widgets in order
    foreach ($defaultWidgets as $widget) {
        $dashboard->saveWidget($widget);
    }
}

Now you can add more widgets (including your own custom widgets) to the $defaultWidgets array and they will be added to all collaborator users upon login. You can further refine this; for example, by comparing the existing widgets with the default widgets and only overwriting them if they differ in some way.

1
  • 1
    Thank you for the detailed explanation @moritzLost. Super helpful for my needs.
    – Parthiban
    Dec 9 '21 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.