Skip to content

Commit

Permalink
Merge pull request #501 from creative-commoners/pulls/4.0/hansel-so-b…
Browse files Browse the repository at this point in the history
…lock-right-now-hansel

NEW Using the breadcrumbs API of reports if available
  • Loading branch information
robbieaverill authored Nov 26, 2018
2 parents cbbec34 + 9a08c63 commit 553e5ab
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Reports/ElementTypeReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -73,7 +74,9 @@ public function columns()
'formatting' => function ($value, $item) use ($inUseReport) {
return sprintf(
'<a class="grid-field__link" href="%s" title="%s">%s</a>',
$inUseReport->getLink('?filters[ClassName]='. $item->ClassName),
$inUseReport->getLink(
'?filters[' . $inUseReport::CLASS_NAME_FILTER_KEY . ']=' . $item->ClassName
),
$item->Type,
$item->TypeNice
);
Expand Down
36 changes: 34 additions & 2 deletions src/Reports/ElementsInUseReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
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
{
/**
* The string used in GET params to filter the records in this report by element type
*
* @var string
*/
const CLASS_NAME_FILTER_KEY = 'ClassName';

public function title()
{
return _t(__CLASS__ . '.ReportTitle', 'Content blocks in use');
Expand All @@ -20,8 +28,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]);
}

Expand Down Expand Up @@ -109,6 +117,30 @@ public function getReportField()
return $field;
}

/**
* When used with silverstripe/reports >= 4.4, 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[static::CLASS_NAME_FILTER_KEY])) {
return [];
}

$report = ElementTypeReport::create();

return [ArrayData::create([
'Title' => $report->title(),
'Link' => $report->getLink(),
])];
}

/**
* Unsanitise a model class' name from a URL param
*
Expand Down

0 comments on commit 553e5ab

Please sign in to comment.