Passed
Pull Request — master (#28)
by
unknown
03:15
created

SitewideContentTaxonomy::updateColumns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\SiteWideContentReport\Model;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Core\Extension;
7
8
/**
9
 * Provides taxonomy integration for sitewide content report.
10
 *
11
 * Requires https://github.com/silverstripe-labs/silverstripe-taxonomy
12
 *
13
 * Class SitewideContentTaxonomy
14
 * @package SilverStripe\SiteWideContentReport\Model
15
 */
16
class SitewideContentTaxonomy extends Extension
17
{
18
    /**
19
     * Name of field to get tags from.
20
     *
21
     * @config
22
     *
23
     * @var string
24
     */
25
    private static $tag_field = 'Terms';
0 ignored issues
show
introduced by
The private property $tag_field is not used, and could be removed.
Loading history...
26
27
    /**
28
     * Update columns to include taxonomy details.
29
     *
30
     * @param string $itemType (i.e 'Pages' or 'Files')
31
     * @param array  $columns  Columns
32
     */
33
    public function updateColumns($itemType, &$columns)
34
    {
35
        if ($itemType !== 'Pages') {
36
            return;
37
        }
38
39
        // Check if pages has the tags field
40
        if (!self::enabled()) {
41
            return;
42
        }
43
44
        // Set column
45
        $field = Config::inst()->get(__CLASS__, 'tag_field');
46
        $columns['Terms'] = [
47
            'printonly' => true, // Hide on page report
48
            'title' => _t('SitewideContentReport.Tags', 'Tags'),
49
            'datasource' => function ($record) use ($field) {
50
                $tags = $record->$field()->column('Name');
51
52
                return implode(', ', $tags);
53
            },
54
        ];
55
    }
56
57
    /**
58
     * Check if this field is enabled.
59
     *
60
     * @return bool
61
     */
62
    public static function enabled()
63
    {
64
        if (!class_exists('TaxonomyTerm')) {
65
            return false;
66
        }
67
68
        // Check if pages has the tags field
69
        $field = Config::inst()->get(__CLASS__, 'tag_field');
70
71
        return singleton('Page')->hasMethod($field);
72
    }
73
}
74