<p>The <code>{% cache %}</code> tag tracks <a href="https://craftcms.com/docs/plugins/working-with-elements" rel="nofollow noreferrer">Elements</a> (Users, Entries, Categories, Tags, etc.), so if you're want to do this using a plugin, then the plugin's field type needs to inherit from <a href="https://craftcms.com/classreference/fieldtypes/BaseElementFieldType" rel="nofollow noreferrer">BaseElementFieldType</a> and needs to implement its own element type.</p>

<p>From there, it's just a matter of calling <a href="https://craftcms.com/classreference/services/TemplateCacheService#includeElementInTemplateCaches-detail" rel="nofollow noreferrer"><code>craft()-&gt;templateCache-&gt;includeElementInTemplateCaches()</code></a>.</p>

CC BY-SA 3.0
2017-04-04T08:15:41.760Z
Thanks so much for getting back to me Brad! If I could just clarify / confirm something: the field types I am outputting the values of in the plugin are actually native Craft fields (e.g. `myImageField` above is just a regular assets field). Would that change anything?
Mike Vaux
CC BY-SA 3.0
2017-03-31T23:32:10.307Z
2017-03-31T23:32:10.307Z
Brad Bell
<p>I have a complex template which uses various modules and sub-components for which I opted for handling all the logic and rendering directly in PHP, as it felt like it was too complex a job for twig.</p>

<p>Currently, when I change a field (e.g. swap out an image) inside one of the modules, the surrounding cache isn't busted, so the site doesn't update.</p>

<p>My question is: How can I make Craft aware of fields used inside a plugin within a <code>{% cache %}</code> tag, so that when a field is changed, the surrounding cache is busted?</p>

<p>I am aware of the <a href="https://craftcms.com/classreference/services/TemplateCacheService" rel="nofollow noreferrer">TemplateCacheService</a>, but unsure if / how I can use that in this case (I had a bit of an experiment with no success).</p>

<p>The <code>modules</code> represent a Neo field, but my assumption is that this would work very similarly if not identically to if this was a matrix field.</p>

<p>&nbsp;</p>

<p>A super-reduced test case looks something like the following:</p>

<pre><code>{% for module in modules %}
    {% cache using key module.id %}
        {{ craft.control.renderModule(module) }}
    {% endcache %}
{% endfor %}
</code></pre>

<p><em>_modules.twig</em></p>

<p>&nbsp;</p>

<pre><code>&lt;?php

namespace Craft;

class ControlVariable
{

    public function renderModule($module)
    {
        return craft()-&gt;control_modules-&gt;renderModule($module);
    }
}
</code></pre>

<p><em>ControlVariable.php</em></p>

<p>&nbsp;</p>

<pre><code>&lt;?php

namespace Craft;

class Control_ModulesService extends BaseApplicationComponent
{

public function __construct()
{}

public function renderModule($module)
{
    ?&gt;

    &lt;!-- How can Craft know when this image changes? --&gt;
    &lt;img src="&lt;?= $module-&gt;myImageField-&gt;first()-&gt;getUrl(); ?&gt;"&gt;

    &lt;?php
}
</code></pre>

<p><em>Control_ModulesService.php</em></p>

<p>&nbsp;</p>

<p>FWIW, in the following setup, the cache is successfully busted when changing the image field in the CMS:</p>

<pre><code>{% for module in modules %}
    {% cache using key module.id %}
        &lt;img src="{{ module.myImageField.first().getUrl() }}"&gt;      
    {% endcache %}
{% endfor %}
</code></pre>

<p><em>_modules.twig</em></p>

<plugins><cache><plugin-neo>
How can I make Craft's caching aware of fields used inside a plugin?
# How can I make Craft's caching aware of fields used inside a plugin?

(Asked by: username_0 on 2017-03-24)

I have a complex template which uses various modules and sub-components for which I opted for handling all the logic and rendering directly in PHP, as it felt like it was too complex a job for twig.
Currently, when I change a field (e.g. swap out an image) inside one of the modules, the surrounding cache isn't busted, so the site doesn't update.
My question is: How can I make Craft aware of fields used inside a plugin within a {% cache %} tag, so that when a field is changed, the surrounding cache is busted?
I am aware of the TemplateCacheService, but unsure if / how I can use that in this case (I had a bit of an experiment with no success).
The modules represent a Neo field, but my assumption is that this would work very similarly if not identically to if this was a matrix field.
 
A super-reduced test case looks something like the following:
{% for module in modules %}
    {% cache using key module.id %}
        {{ craft.control.renderModule(module) }}
    {% endcache %}
{% endfor %}

_modules.twig
 
control_modules->renderModule($module);
    }
}

ControlVariable.php
 


    
    

    
Control_ModulesService.php
 
FWIW, in the following setup, the cache is successfully busted when changing the image field in the CMS:
{% for module in modules %}
    {% cache using key module.id %}
              
    {% endcache %}
{% endfor %}

_modules.twig

**Tags:** plugins, cache, plugin-neo


---

## 1 answers

### Answer by username_1 on 2017-03-31. Score: 1

The `{% cache %}` tag tracks [Elements](https://craftcms.com/docs/plugins/working-with-elements) (Users, Entries, Categories, Tags, etc.), so if you're want to do this using a plugin, then the plugin's field type needs to inherit from [BaseElementFieldType](https://craftcms.com/classreference/fieldtypes/BaseElementFieldType) and needs to implement its own element type.

From there, it's just a matter of calling [`craft()->templateCache->includeElementInTemplateCaches()`](https://craftcms.com/classreference/services/TemplateCacheService#includeElementInTemplateCaches-detail).

> **Comments:**
> **username_0**: Thanks so much for getting back to me Brad! If I could just clarify / confirm something: the field types I am outputting the values of in the plugin are actually native Craft fields (e.g. `myImageField` above is just a regular assets field). Would that change anything?  

---

