From 0efd01d076e632b62f8b2138d8163583eeb0065e Mon Sep 17 00:00:00 2001 From: Guy Marriott Date: Fri, 16 Nov 2018 15:14:30 +1300 Subject: [PATCH 1/3] NEW Using the breadcrumbs API of reports if available --- src/Reports/ElementsInUseReport.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Reports/ElementsInUseReport.php b/src/Reports/ElementsInUseReport.php index 30ea0b60..f52f1676 100644 --- a/src/Reports/ElementsInUseReport.php +++ b/src/Reports/ElementsInUseReport.php @@ -6,6 +6,7 @@ use SilverStripe\Forms\GridField\GridField; use SilverStripe\ORM\DataList; use SilverStripe\Reports\Report; +use SilverStripe\View\ArrayData; use SilverStripe\View\Requirements; class ElementsInUseReport extends Report @@ -109,6 +110,30 @@ public function getReportField() return $field; } + /** + * When used with newer versions of silverstripe-reports this method will automatically be added as breadcrumbs + * leading up to this report. + * + * @return ArrayData[] + */ + public function getBreadcrumbs() + { + $params = $this->getSourceParams(); + + // Only apply breadcrumbs if a "ClassName" filter is applied. This implies that we came from the + // "element type report". + if (!isset($params['ClassName'])) { + return []; + } + + $report = ElementTypeReport::create(); + + return [ArrayData::create([ + 'Title' => $report->title(), + 'Link' => $report->getLink(), + ])]; + } + /** * Unsanitise a model class' name from a URL param * From 5d36bf1087c2a9c229f77b328b76fc6973cf877e Mon Sep 17 00:00:00 2001 From: Guy Marriott Date: Tue, 20 Nov 2018 18:06:41 +1300 Subject: [PATCH 2/3] NEW Updating ElementsInUseReport to use a constant for the string used as a filter key for element types --- src/Reports/ElementTypeReport.php | 7 +++++-- src/Reports/ElementsInUseReport.php | 11 ++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Reports/ElementTypeReport.php b/src/Reports/ElementTypeReport.php index c77e1214..cb9d1aa1 100644 --- a/src/Reports/ElementTypeReport.php +++ b/src/Reports/ElementTypeReport.php @@ -62,7 +62,8 @@ protected function getElementTypes() public function columns() { - $inUseReport = new ElementsInUseReport; + // Get from Injector so substitutions are used... + $inUseReport = ElementsInUseReport::singleton(); return [ 'Icon' => [ @@ -73,7 +74,9 @@ public function columns() 'formatting' => function ($value, $item) use ($inUseReport) { return sprintf( '%s', - $inUseReport->getLink('?filters[ClassName]='. $item->ClassName), + $inUseReport->getLink( + '?filters[' . $inUseReport::CLASS_NAME_FILTER_KEY . ']=' . $item->ClassName + ), $item->Type, $item->TypeNice ); diff --git a/src/Reports/ElementsInUseReport.php b/src/Reports/ElementsInUseReport.php index f52f1676..1d15adaf 100644 --- a/src/Reports/ElementsInUseReport.php +++ b/src/Reports/ElementsInUseReport.php @@ -11,6 +11,11 @@ class ElementsInUseReport extends Report { + /** + * The string used in GET params to filter the records in this report by element type + */ + const CLASS_NAME_FILTER_KEY = 'ClassName'; + public function title() { return _t(__CLASS__ . '.ReportTitle', 'Content blocks in use'); @@ -21,8 +26,8 @@ public function sourceRecords($params = []) /** @var DataList $elements */ $elements = BaseElement::get()->exclude(['ClassName' => BaseElement::class]); - if (isset($params['ClassName'])) { - $className = $this->unsanitiseClassName($params['ClassName']); + if (isset($params[static::CLASS_NAME_FILTER_KEY])) { + $className = $this->unsanitiseClassName($params[static::CLASS_NAME_FILTER_KEY]); $elements = $elements->filter(['ClassName' => $className]); } @@ -122,7 +127,7 @@ public function getBreadcrumbs() // Only apply breadcrumbs if a "ClassName" filter is applied. This implies that we came from the // "element type report". - if (!isset($params['ClassName'])) { + if (!isset($params[static::CLASS_NAME_FILTER_KEY])) { return []; } From 9a08c63ff156afb4dc677331e22955dd8a7da084 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 26 Nov 2018 15:00:23 +0100 Subject: [PATCH 3/3] Update docblock type for constant and specify "newer" version of reports --- src/Reports/ElementsInUseReport.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Reports/ElementsInUseReport.php b/src/Reports/ElementsInUseReport.php index 1d15adaf..2de59def 100644 --- a/src/Reports/ElementsInUseReport.php +++ b/src/Reports/ElementsInUseReport.php @@ -13,6 +13,8 @@ class ElementsInUseReport extends Report { /** * The string used in GET params to filter the records in this report by element type + * + * @var string */ const CLASS_NAME_FILTER_KEY = 'ClassName'; @@ -116,7 +118,7 @@ public function getReportField() } /** - * When used with newer versions of silverstripe-reports this method will automatically be added as breadcrumbs + * When used with silverstripe/reports >= 4.4, this method will automatically be added as breadcrumbs * leading up to this report. * * @return ArrayData[]