3

i wrote a plugin with an additional dashboard widget.

i have to refresh the widget data every hour with ajax. so i added a plugin controller in my plugin directory with the idea of re-rendering the widget template (called through ajax).

1) how can i (correctly) access the widget instance from the controller (or service)?

my try is $widget = craft()->dashboard->getWidgetType('name'). this gives me the correct instance. is this the right way?

2) no settings data from widget (needed in widget template)

in the widget template i need access to the widget settings (defined in the widget class with defineSettings()).

in the controller, i get the widget instance (as described in point 1) and do $widget->getBodyHtml(), but the settings attributes are empty. all other parts of the template are rendered correctly.

when the widget is rendered the first time (no ajax request), anything seems to be fine (settings loaded correctly).

can anyone explain my fault?

2
  • 1. This is correct. 2. Are the settings injected into the template at all? As an option in craft()->templates->render()? Commented Jun 18, 2015 at 17:06
  • 1. thank you for confirming this 2. yes, the settings model is injected into the template as a craft()->templates->render() option (return craft()->templates->render('showcase/_statsView', ['settings' => $this->getSettings()]);. when i dump the $this->getSettings() in the widget class (on getBodyHtml-method, the first time it renders the settings property is populated, but the second time (with an ajax-request), the property is empty.
    – zihmm
    Commented Jun 19, 2015 at 6:57

2 Answers 2

3

found a way to solve the problem.

feels a bit 'dirty' to iterate over all active widgets to get the one i need (in the service class). the other option is craft()->dashboard->getUserWidgetById(), but how the heck can i get the id of the widget by given type in a simple way?

how ever, this solution works:

controller method (requested through ajax)

public function actionUpdateWidget()
{
    $this->requireAjaxRequest();

    try
    {
        echo craft()->showcase_widget->get()->getBodyHtml();
    }
    catch (Exception $ex)
    {
        echo $ex->getMessage();
    }

    craft()->end();
}

service class

<?

namespace craft;

use Exception;

class Showcase_WidgetService extends BaseApplicationComponent
{
    protected $widgetType = 'Showcase_Feedback';

    public function get()
    {
        $widget = null;
        $userWidgets = craft()->dashboard->getUserWidgets();

        foreach ($userWidgets as $userWidget)
        {
            if ($userWidget->type === $this->widgetType)
            {
                $widget = craft()->dashboard->populateWidgetType($userWidget);
                break;
            }
        }

        if (!$widget)
        {
             throw new Exception(craft::t('Widget not found'));
        }

        return $widget;
    }
}
2

I'm a bit late to the party here, but for future reference you can get the widget id by looking at the data-id attribute of the widget HTML, or by using $this->model->id in the widget code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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