<p>found a way to solve the problem.</p>

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

<p>how ever, this solution works:</p>

<p><em>controller method (requested through ajax)</em></p>

<pre><code>public function actionUpdateWidget()
{
    $this-&gt;requireAjaxRequest();

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

    craft()-&gt;end();
}
</code></pre>

<p><em>service class</em></p>

<pre><code>&lt;?

namespace craft;

use Exception;

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

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

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

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

        return $widget;
    }
}
</code></pre>

CC BY-SA 3.0
2015-06-19T08:14:20.137Z
2015-06-19T08:14:20.137Z
zihmm
<p>I'm a bit late to the party here, but for future reference you can get the widget id by looking at the <code>data-id</code> attribute of the widget HTML, or by using <code>$this-&gt;model-&gt;id</code> in the widget code.</p>

CC BY-SA 3.0
2016-08-04T10:03:14.057Z
2016-08-04T10:11:43.260Z
2016-08-04T10:11:43.260Z
Gaz
<p>i wrote a plugin with an additional dashboard widget.</p>

<p>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).</p>

<p><strong>1) how can i (correctly) access the widget instance from the controller (or service)?</strong></p>

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

<p><strong>2) no settings data from widget (needed in widget template)</strong></p>

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

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

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

<p>can anyone explain my fault?</p>

CC BY-SA 3.0
2015-06-18T17:06:32.190Z
1. This is correct. 2. Are the settings injected into the template at all? As an option in `craft()->templates->render()`?
Bob Olde Hampsink
CC BY-SA 3.0
2015-06-19T06:57:01.340Z
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
<controller><dashboard-widgets>
Dashboard Widget: Access widget settings from controller / service
# Dashboard Widget: Access widget settings from controller / service

(Asked by: username_0 on 2015-06-18)

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?

**Tags:** controller, dashboard-widgets
> **Comments:**
> **username_1**: 1. This is correct. 2. Are the settings injected into the template at all? As an option in `craft()->templates->render()`?  
> **username_0**: 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.  

---

## 2 answers

### Answer by username_0 on 2015-06-19. Score: 3 | Accepted answer

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
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;
    }
}


---

### Answer by username_2 on 2016-08-04. Score: 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.


---

