CwpSearchBoostExtension::updateCMSFields()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 22
c 2
b 0
f 1
dl 0
loc 30
rs 9.568
cc 2
nc 2
nop 1
1
<?php
2
3
namespace CWP\Search\Extensions;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\TextareaField;
7
use SilverStripe\Forms\ToggleCompositeField;
8
use SilverStripe\ORM\DataExtension;
9
10
/**
11
 * Adds field boosting capabilities to fulltext search for pages
12
 */
13
class CwpSearchBoostExtension extends DataExtension
14
{
15
    /**
16
     * Quality to boost the 'SearchBoost' field by.
17
     * Default boost is 2x
18
     *
19
     * @config
20
     * @var string
21
     */
22
    private static $search_boost = '2';
0 ignored issues
show
introduced by
The private property $search_boost is not used, and could be removed.
Loading history...
23
24
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
25
        'SearchBoost' => 'Text',
26
    ];
27
28
    /**
29
     * Adds boost fields to this page
30
     *
31
     * @param FieldList $fields
32
     */
33
    public function updateCMSFields(FieldList $fields)
34
    {
35
        $pageInfoTitle = _t(__CLASS__ . '.PAGEINFO', 'Page info and SEO');
36
        $boostTitle = _t(__CLASS__ . '.SearchBoost', 'Boost Keywords');
37
        $boostNote = _t(
38
            __CLASS__ . '.SearchBoostNote',
39
            '(Only applies to the search results on this site e.g. not on Google search)'
40
        );
41
        $boostDescription = _t(
42
            __CLASS__ . '.SearchBoostDescription',
43
            'Enter keywords separated by comma ( , ) for which to boost the ranking of this page '
44
            . 'within the search results on this site.'
45
        );
46
        $boostField = TextareaField::create('SearchBoost', $boostTitle)
47
            ->setRightTitle($boostNote)
48
            ->setDescription($boostDescription);
49
50
        if ($meta = $fields->fieldByName('Root.Main.Metadata')) {
51
            // Rename metafield if it exists
52
            $meta->setTitle($pageInfoTitle);
53
            $fields->insertBefore('MetaDescription', $boostField);
54
        } else {
55
            // Else create new field to store SEO
56
            $fields->addFieldToTab(
57
                'Root.Main',
58
                ToggleCompositeField::create(
59
                    'Metadata',
60
                    $pageInfoTitle,
61
                    [
62
                        $boostField,
63
                    ]
64
                )
65
            );
66
        }
67
    }
68
}
69