Skip to content

DashboardService's PHP API

You can use DashboardService's PHP API to manage custom dashboards. To obtain this service, inject the Ibexa\Contracts\Dashboard\DashboardServiceInterface.

The service exposes two functions:

  • createCustomDashboardDraft(?Location $location = null): Content - returns a new content item in draft state of dashboard content type. If no location is given, it creates a copy of the dashboard of the user currently logged in. If a location is given, it creates a copy with the given location. The default name of the customized dashboard is set as My dashboard. This new Content draft is located in the current user custom dashboard container.
  • createDashboard(DashboardCreateStruct $dashboardCreateStruct): Content - publishes the given dashboard creation structure (Ibexa\Contracts\Dashboard\Values\DashboardCreateStruct) under dashboard.predefined_container_remote_id.

Customize dashboard using DashboardService

The following example is a command deploying a custom dashboard to users of content groups. Using the admin account, it loads the group members, logs each one in, creates a custom dashboard by copying a default one, and then publishes the draft version of customized dashboard. First argument is the Content ID of the dashboard to copy. Following arguments are the Content IDs of the user groups.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php declare(strict_types=1);

namespace App\Command;

use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\Repository;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Dashboard\DashboardServiceInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DashboardCommand extends Command
{
    protected static $defaultName = 'doc:dashboard';

    private DashboardServiceInterface $dashboardService;

    private Locationservice $locationService;

    private ContentService $contentService;

    private UserService $userService;

    private PermissionResolver $permissionResolver;

    public function __construct(
        DashboardServiceInterface $dashboardService,
        Repository $repository
    ) {
        $this->dashboardService = $dashboardService;
        $this->locationService = $repository->getLocationService();
        $this->contentService = $repository->getContentService();
        $this->userService = $repository->getUserService();
        $this->permissionResolver = $repository->getPermissionResolver();

        parent::__construct(self::$defaultName);
    }

    public function configure(): void
    {
        $this->setDescription('Set a custom dashboard to user group.')
            ->addArgument('dashboard', InputArgument::REQUIRED, 'Location ID of the dashboard model')
            ->addArgument('group', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'User Group Content ID(s)');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $dashboardModelLocationId = (int)$input->getArgument('dashboard');
        $userGroupLocationIdList = array_map('intval', $input->getArgument('group'));

        foreach ($userGroupLocationIdList as $userGroupLocationId) {
            try {
                $admin = $this->userService->loadUserByLogin('admin');
                $this->permissionResolver->setCurrentUserReference($admin);
                foreach ($this->userService->loadUsersOfUserGroup($this->userService->loadUserGroup($userGroupLocationId)) as $user) {
                    $this->permissionResolver->setCurrentUserReference($user);
                    $dashboardDraft = $this->dashboardService->createCustomDashboardDraft($this->locationService->loadLocation($dashboardModelLocationId));
                    $this->contentService->publishVersion($dashboardDraft->getVersionInfo());
                }
            } catch (\Throwable $throwable) {
                dump($throwable);
            }
        }

        return self::SUCCESS;
    }
}

The following line runs the command with 74 as the model dashboard's Content ID, 13 the user group's Content ID, and on the SiteAccess admin to have the right user_content_type_identifier config:

1
php bin/console doc:dashboard 74 13 --siteaccess=admin