Completed
Pull Request — master (#62)
by Jason
12:32
created

PromoBlock::getCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 17
cts 17
cp 1
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Dynamic\DynamicBlocks\Block;
4
5
use Dynamic\DynamicBlocks\Model\PromoObject;
6
use SheaDawson\Blocks\Model\Block;
7
use SilverStripe\Forms\GridField\GridField;
8 12
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
9
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
10 12
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
11
12
class PromoBlock extends Block
13
{
14
    /**
15
     * @return string
16 1
     */
17
    public function singular_name()
18 1
    {
19
        return _t('PromoBlock.SINGULARNAME', 'Promos Block');
20
    }
21
22
    /**
23
     * @return string
24
     */
25
    public function plural_name()
26
    {
27
        return _t('PromoBlock.PLURALNAME', 'Promos Blocks');
28
    }
29
30
    /**
31
     * @var array
32
     */
33
    private static $many_many = array(
34
        'Promos' => PromoObject::class,
35
    );
36
37
    /**
38
     * @var array
39
     */
40 1
    private static $many_many_extraFields = array(
41
        'Promos' => array(
42 1
            'SortOrder' => 'Int',
43
        ),
44 1
    );
45 1
46 1
    /**
47
     * @return FieldList
48 1
     */
49 1
    public function getCMSFields()
50 1
    {
51 1
        $fields = parent::getCMSFields();
52 1
53 1
        $fields->removeByName(array(
54 1
            'Promos',
55
        ));
56 1
57 1
        if ($this->ID) {
58 1
            $config = GridFieldConfig_RelationEditor::create();
59 1
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
60
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
61 1
            $config->addComponent(new GridFieldAddExistingSearchButton());
62
            $promos = $this->Promos()->sort('SortOrder');
0 ignored issues
show
Documentation Bug introduced by
The method Promos does not exist on object<Dynamic\DynamicBlocks\Block\PromoBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
            $promoField = GridField::create('Promos', 'Promos', $promos, $config);
64
65
            $fields->addFieldsToTab('Root.Promos', array(
66
                $promoField,
67 1
            ));
68
        }
69 1
70
        return $fields;
71
    }
72
73
    /**
74
     * @return mixed
75
     */
76
    public function getPromoList()
77
    {
78
        return $this->Promos()->sort('SortOrder');
0 ignored issues
show
Documentation Bug introduced by
The method Promos does not exist on object<Dynamic\DynamicBlocks\Block\PromoBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
79
    }
80
}