'SilverStripe\\CMS\\Model\\SiteTree.ImageTracking' // {@see SiteTreeLinkTracking} ); /** * Images tracked by pages are owned by those pages * * @config * @var array */ private static $owned_by = array( 'BackLinkTracking' ); public function updateCMSFields(FieldList $fields) { $fields->insertAfter( 'LastEdited', ReadonlyField::create( 'BackLinkCount', _t('AssetTableField.BACKLINKCOUNT', 'Used on:'), $this->BackLinkTracking()->count() . ' ' . _t('AssetTableField.PAGES', 'page(s)') ) ->addExtraClass('cms-description-toggle') ->setDescription($this->BackLinkHTMLList()) ); } /** * Generate an HTML list which provides links to where a file is used. * * @return string */ public function BackLinkHTMLList() { $html = '' . _t( 'SiteTreeFileExtension.BACKLINK_LIST_DESCRIPTION', 'This list shows all pages where the file has been added through a WYSIWYG editor.' ) . ''; $html .= '
'; return $html; } /** * Extend through {@link updateBackLinkTracking()} in your own {@link Extension}. * * @return ManyManyList */ public function BackLinkTracking() { if(class_exists("Subsite")){ $rememberSubsiteFilter = Subsite::$disable_subsite_filter; Subsite::disable_subsite_filter(true); } $links = $this->owner->getManyManyComponents('BackLinkTracking'); $this->owner->extend('updateBackLinkTracking', $links); if(class_exists("Subsite")){ Subsite::disable_subsite_filter($rememberSubsiteFilter); } return $links; } /** * @todo Unnecessary shortcut for AssetTableField, coupled with cms module. * * @return int */ public function BackLinkTrackingCount() { $pages = $this->owner->BackLinkTracking(); if($pages) { return $pages->count(); } else { return 0; } } /** * Updates link tracking in the current stage. */ public function onAfterDelete() { // Skip live stage if(Versioned::get_stage() === Versioned::LIVE) { return; } // We query the explicit ID list, because BackLinkTracking will get modified after the stage // site does its thing $brokenPageIDs = $this->owner->BackLinkTracking()->column("ID"); if($brokenPageIDs) { // This will syncLinkTracking on the same stage as this file $brokenPages = DataObject::get('SilverStripe\\CMS\\Model\\SiteTree')->byIDs($brokenPageIDs); foreach($brokenPages as $brokenPage) { $brokenPage->write(); } } } }