/
SitewideContentTaxonomy.php
65 lines (57 loc) · 1.53 KB
/
SitewideContentTaxonomy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* Provides taxonomy integration for sitewide content report.
*
* Requires https://github.com/silverstripe-labs/silverstripe-taxonomy
*/
class SitewideContentTaxonomy extends Extension
{
/**
* Name of field to get tags from.
*
* @config
*
* @var string
*/
private static $tag_field = 'Terms';
/**
* Update columns to include taxonomy details.
*
* @param string $itemType (i.e 'Pages' or 'Files')
* @param array $columns Columns
*/
public function updateColumns($itemType, &$columns)
{
if ($itemType !== 'Pages') {
return;
}
// Check if pages has the tags field
if (!self::enabled()) {
return;
}
// Set column
$field = Config::inst()->get(__CLASS__, 'tag_field');
$columns['Terms'] = array(
'printonly' => true, // Hide on page report
'title' => _t('SitewideContentReport.Tags', 'Tags'),
'datasource' => function ($record) use ($field) {
$tags = $record->$field()->column('Name');
return implode(', ', $tags);
},
);
}
/**
* Check if this field is enabled.
*
* @return bool
*/
public static function enabled()
{
if (!class_exists('TaxonomyTerm')) {
return false;
}
// Check if pages has the tags field
$field = Config::inst()->get(__CLASS__, 'tag_field');
return singleton('Page')->hasMethod($field);
}
}