phpcachingcontent-management-systemsilverstripe

silverstripe static publisher - pages affected by DataObject Changes


is there a possibility to trigger an update of the cache if a DataObject is edited? for example updating a News DataObject should update the cache of pages, that are displaying these NewsObjects.

many thanx, Florian


Solution

  • Here is what I could do using the StaticPublishQueue module. In your NewsDataObject.php:

    function onAfterWrite() {
        parent::onAfterWrite();
    
        $url = array();
        $pages = $this->Pages(); //has_many link to pages that include this DataObject
        foreach($pages as $page) {
            $pagesAffected = $page->pagesAffected();
            if ($pagesAffected && count($pagesAffected) > 0) {
                $urls = array_merge((array)$urls, (array)$pagesAffected);
            }
        }
        URLArrayObject::add_urls($urls);
    }
    

    This takes each of the pages that references your DataObject, asks it for all it's URL and the URL of any related pages (e.g. Virtual Pages that reference that page), compiles all the URLs into a big array, then adds that array to the static publishing queue. The queue will gradually process until all the affected pages are republished.

    The event system allows you to add a layer of abstraction between the republishing and the triggers for republishing, but for something simple you don't necessarily need to use it. Instead, you can add pages to the queue directly. (You might also like to read this blog post describing the StaticPublishQueue module)