Dashboard widget error

Hi, i want to create a custom widget for my app and i add the lib/Dashboard/Widget.php and register it with Application.php too but nothing shows in customize.

lib/Dashboard/Widget.php:

<?php

namespace OCA\Zones\Dashboard;

use OCP\Dashboard\IWidget;
use OCP\AppFramework\Services\IInitialState;
use OCP\IURLGenerator;

class Widget implements IWidget {
    private $url;
    private $initialState;

    public function __construct(IInitialState $initialState, IURLGenerator $url) {
        $this->initialState = $initialState;
        $this->url = $url;
    }

    public function getId(): string {
        return 'zones';
    }

    public function getTitle(): string {
        return 'World Clocks';
    }

    public function getOrder(): int {
        return 10;
    }

    public function getIconClass(): string {
        return '';
    }

    public function getUrl(): ?string {
        return '/index.php/apps/zones';
    }

    public function load(): void {
        $this -> initialState -> provideInitialState('zones', 'myData', []);
    }

    public function getScript(): ?string {
        return $this->url->linkTo('zones', 'src/dashboard.js');
    }
}

lib/Appinfo/Application.php:

<?php

namespace OCA\Zones\AppInfo;

use OCA\Zones\Controller\PageController;
use OCA\Zones\Controller\UserController;
use OCA\Zones\Dashboard\Widget;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IRegistrationContext;

class Application extends App implements IBootstrap {
    public const APP_ID = 'zones';

    public function __construct() {
        parent::__construct(self::APP_ID);

        $container = $this->getContainer();

        $container->registerAlias('PageController', PageController::class);
        $container->registerAlias('UserController', UserController::class);

        $container->registerService('PageIndexRoute', function () {
            return $this->getContainer()->getServer()->getURLGenerator()->linkToRoute(self::APP_ID . '.page.index');
        });
        $container->registerService('UserIndexRoute', function () {
            return $this->getContainer()->getServer()->getURLGenerator()->linkToRoute(self::APP_ID . '.user.index');
        });
    }

    public function register(IRegistrationContext $context): void {
        // Register your dashboard widget
        $context->registerDashboardWidget(Widget::class);
    }

    public function boot(IBootContext $context): void {
        // Add any bootstrap code here
    }
}

Log:

{"reqId":"lU3kaaKjYNrBvc3yQQ39","level":3,"time":"2023-04-22T10:37:33+00:00","remoteAddr":"172.17.0.1","user":"hooman","app":"no app in context","method":"GET","url":"/index.php/apps/dashboard/","message":"Could not load lazy dashboard widget: Could not resolve OCA\\Zones\\Dashboard\\Widget! Class \"OCA\\Zones\\Dashboard\\Widget\" does not exist","userAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.0.0","version":"25.0.5.1","data":{"excepiton":"{\"class\":\"OCP\\AppFramework\\QueryException\",\"message\":\"Could not resolve OCA\\Zones\\Dashboard\\Widget! Class \\\"OCA\\Zones\\Dashboard\\Widget\\\" does not exist\",\"code\":0,\"file\":\"/var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php:122\",\"trace\":\"#0 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(133): OC\\AppFramework\\Utility\\SimpleContainer->resolve()\\n#1 /var/www/nextcloud/lib/private/ServerContainer.php(161): OC\\AppFramework\\Utility\\SimpleContainer->query()\\n#2 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(57): OC\\ServerContainer->query()\\n#3 /var/www/nextcloud/lib/private/Dashboard/Manager.php(78): OC\\AppFramework\\Utility\\SimpleContainer->get()\\n#4 /var/www/nextcloud/lib/private/Dashboard/Manager.php(131): OC\\Dashboard\\Manager->loadLazyPanels()\\n#5 /var/www/nextcloud/apps/dashboard/lib/Controller/DashboardController.php(101): OC\\Dashboard\\Manager->getWidgets()\\n#6 /var/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php(225): OCA\\Dashboard\\Controller\\DashboardController->index()\\n#7 /var/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php(133): OC\\AppFramework\\Http\\Dispatcher->executeController()\\n#8 /var/www/nextcloud/lib/private/AppFramework/App.php(172): OC\\AppFramework\\Http\\Dispatcher->dispatch()\\n#9 /var/www/nextcloud/lib/private/Route/Router.php(298): OC\\AppFramework\\App::main()\\n#10 /var/www/nextcloud/lib/base.php(1047): OC\\Route\\Router->match()\\n#11 /var/www/nextcloud/index.php(36): OC::handleRequest()\\n#12 {main}\"}"}}
{"reqId":"lU3kaaKjYNrBvc3yQQ39","level":3,"time":"2023-04-22T10:37:33+00:00","remoteAddr":"172.17.0.1","user":"hooman","app":"no app in context","method":"GET","url":"/index.php/apps/dashboard/","message":"Could not register lazy dashboard widget: Dashboard widget with this id has already been registered","userAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.0.0","version":"25.0.5.1","exception":{"Exception":"InvalidArgumentException","Message":"Dashboard widget with this id has already been registered","Code":0,"Trace":[{"file":"/var/www/nextcloud/lib/private/Dashboard/Manager.php","line":94,"function":"registerWidget","class":"OC\\Dashboard\\Manager","type":"->"},{"file":"/var/www/nextcloud/lib/private/Dashboard/Manager.php","line":131,"function":"loadLazyPanels","class":"OC\\Dashboard\\Manager","type":"->"},{"file":"/var/www/nextcloud/apps/dashboard/lib/Controller/DashboardController.php","line":101,"function":"getWidgets","class":"OC\\Dashboard\\Manager","type":"->"},{"file":"/var/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php","line":225,"function":"index","class":"OCA\\Dashboard\\Controller\\DashboardController","type":"->"},{"file":"/var/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php","line":133,"function":"executeController","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"/var/www/nextcloud/lib/private/AppFramework/App.php","line":172,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"/var/www/nextcloud/lib/private/Route/Router.php","line":298,"function":"main","class":"OC\\AppFramework\\App","type":"::"},{"file":"/var/www/nextcloud/lib/base.php","line":1047,"function":"match","class":"OC\\Route\\Router","type":"->"},{"file":"/var/www/nextcloud/index.php","line":36,"function":"handleRequest","class":"OC","type":"::"}],"File":"/var/www/nextcloud/lib/private/Dashboard/Manager.php","Line":55,"message":"Could not register lazy dashboard widget: Dashboard widget with this id has already been registered","exception":{},"CustomMessage":"Could not register lazy dashboard widget: Dashboard widget with this id has already been registered"}}

Cool that you are working on your own widget!

you might want to post this question in the developer section of this forum (πŸ’» Development - Nextcloud community) You might also be interested in the tutorial about developing a dashboard widget:
Nextcloud

I hope that helps

1 Like

For the record: This is cross-posted in the other topic forum as well.

I think your log file speaks very clearly:

This sounds rather strange to me.

For one, the error message indicates that the ID was already in use. I would check that there is no other app in the installation that by chance uses the same name. Also be sure there are no two versions of your app installed by accident in some crazy way.

Apart from that, I would use step debugging and see if the register function in the Application class is called once or multiple times per reload. Maybe you find the culprit by analyzing the situation and understanding what the core does. The code looks good to my by just looking through it. I though never used the dashboard widget system so far. So I cannot help you that much specifically here.

I would be interested in your findings.

2 Likes

No, Actually the error message is not correct. There were no other dashboard nor app.

Thanks for your reply. I solved the error but i don’t know how. but this tutorial helped me a lot. However you are right about debugging i should take a look at that.

2 Likes