Answers.Body: <p>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 <a href="https://docs.craftcms.com/api/v3/craft-services-dashboard.html" rel="nofollow noreferrer">Dashboard service</a> to change the dashboard widgets for a given user, and hide the dashboard options using CSS.</p>
  <h2>Hiding dashboard options from users</h2>
  <p>First install the <a href="https://plugins.craftcms.com/cp-body-classes" rel="nofollow noreferrer">Control Panel Body Classes plugin</a> 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.</p>
  <p>The following snippet hides the dashboard configuration (the
  <em>New Widget</em> dropdown and the configuration button) from the user group <code>collaborator</code>:</p>
  <pre class="lang-php prettyprint-override"><code>use Craft;
  
  if (Craft::$app-&gt;getRequest()-&gt;getIsCpRequest()) {
      /** @var craft\web\View $view */
      $view = Craft::$app-&gt;getView();
      $view-&gt;registerCss(&lt;&lt;&lt;CSS
          body.usergroup-collaborator.currentsection-dashboard #action-button {
              display: none;
          }
      CSS);
  }
  </code></pre>
  <p>If you only want to limit the widgets that are available for a particular user group, you can hide those in the <em>New widget</em> dropdown using CSS as well. You can even enforce this on the server-side with an event hook for <a href="https://docs.craftcms.com/api/v3/craft-services-dashboard.html#event-before-save-widget" rel="nofollow noreferrer">Dashboard::EVENT_BEFORE_SAVE_WIDGET</a>. The callback can prevent saving a widget that the current user shouldn't have access to.</p>
  <h2>Changing dashboard widgets programmatically</h2>
  <p>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).</p>
  <pre class="lang-php prettyprint-override"><code>use craft\web\User;
  use yii\base\Event;
  use yii\web\UserEvent;
  
  Event::on(
      User::class,
      User::EVENT_AFTER_LOGIN,
      fn(UserEvent $e) =&gt; $this-&gt;ensureDefaultDashboardWidgets($e-&gt;sender),
  );
  </code></pre>
  <p>The function needs to remove the default widgets and then register the predefined set of widgets you want to provide to that user group:</p>
  <pre class="lang-php prettyprint-override"><code>use Craft;
  use craft\web\User;
  
  public function ensureDefaultDashboardWidgets(User $user): void
  {
      /** @var craft\elements\User */
      $userIdentity = $user-&gt;getIdentity();
      /** @var craft\services\Dashboard $dashboard */
      $dashboard = Craft::$app-&gt;dashboard;
  
      $userGroups = $userIdentity-&gt;getGroups();
      $isCollaborator = count($userGroups) === 1 &amp;&amp; $userGroups[0]-&gt;handle === 'collaborator';
      if (!$isCollaborator) return;
  
      // remove all existing widgets
      $existingWidgets = $dashboard-&gt;getAllWidgets();
      foreach ($existingWidgets as $widget) {
          $dashboard-&gt;deleteWidget($widget);
      }
  
      $defaultWidgets = [
          $dashboard-&gt;createWidget([
              'type' =&gt; 'craft\\widgets\\MyDrafts',
              'settings' =&gt; ['limit' =&gt; 10],
          ]),
          $dashboard-&gt;createWidget('modules\\MyCustomWidgetsModule\\widgets\\MyCustomWidget'),
      ];
  
      // add all default widgets in order
      foreach ($defaultWidgets as $widget) {
          $dashboard-&gt;saveWidget($widget);
      }
  }
  </code></pre>
  <p>Now you can add more widgets (including your own custom widgets) to the <code>$defaultWidgets</code> array and they will be added to all <code>collaborator</code> 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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-09T13:32:17.687Z
Answers.Text: Thank you for the detailed explanation @moritzLost. Super helpful for my needs.
Answers.UserDisplayName: Parthiban
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-09T11:07:23.227Z
Answers.LastActivityDate: 2021-12-09T11:07:23.227Z
Answers.UserDisplayName: MoritzLost
Body: <p>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?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-09T10:38:46.493Z
Comments.Text: 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?
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-14T09:45:27.473Z
Comments.Text: @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.
Comments.UserDisplayName: Parthiban
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-14T09:53:26.353Z
Comments.Text: @Partihiban I think my answer addresses both of those, or is something missing?
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-14T10:22:10.697Z
Comments.Text: @MoritzLost Yes it works. Thanks.
Comments.UserDisplayName: Parthiban
Tags: <craft3><dashboard-widgets>
Title: Is there a way to display only the selected widgets to the user group or disable them altogether?
ThreadText: # Is there a way to display only the selected widgets to the user group or disable them altogether?
  
  (Asked by: username_0 on 2021-12-09)
  
  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?
  
  **Tags:** craft3, dashboard-widgets
  
  > **Comments:**
  > **username_1**: 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?  
  > **username_0**: @username_1 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.  
  > **username_1**: @Partihiban I think my answer addresses both of those, or is something missing?  
  > **username_0**: @username_1 Yes it works. Thanks.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-12-09. Score: 2 | Accepted answer
  
  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](https://docs.craftcms.com/api/v3/craft-services-dashboard.html) 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](https://plugins.craftcms.com/cp-body-classes) 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`:
  
  ```php
  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](https://docs.craftcms.com/api/v3/craft-services-dashboard.html#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).
  
  ```php
  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:
  
  ```php
  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.
  
  > **Comments:**
  > **username_0**: Thank you for the detailed explanation @moritzLost. Super helpful for my needs.  
  
  ---
  
  