Passed
Pull Request — master (#122)
by Robbie
01:58
created

applyBlockTypeTitles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace DNADesign\Elemental\Forms;
4
5
use DNA\AdvancedDropdowns\AdvancedDropdownField;
0 ignored issues
show
Bug introduced by
The type DNA\AdvancedDropdowns\AdvancedDropdownField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\View\ArrayData;
10
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;
11
use Symbiote\GridFieldExtensions\GridFieldExtensions;
12
13
class ElementalGridFieldAddNewMultiClass extends GridFieldAddNewMultiClass
14
{
15
    /**
16
     * Overridden to swap out dropdown for advancedropdown, so we can define element icons
17
     *
18
     * {@inheritDoc}
19
     */
20
    public function getHTMLFragments($grid)
21
    {
22
        $classes = $this->getClasses($grid);
23
        $classes = $this->applyBlockTypeTitles($classes);
24
25
        if (!count($classes)) {
26
            return array();
27
        }
28
29
        GridFieldExtensions::include_requirements();
30
31
        $preppedClasses = [];
32
33
        foreach ($classes as $key => $value) {
34
            $preppedClasses[$key] = [
35
                'Title' => $value,
36
                'Attributes' => ['class'=>'el-icon ' . strtolower($key)]
37
            ];
38
        }
39
40
        $field = new AdvancedDropdownField(sprintf('%s[ClassName]', __CLASS__), '', $preppedClasses);
41
42
        if (Config::inst()->get(__CLASS__, 'showEmptyString')) {
43
            $field->setHasEmptyDefault(true);
44
        }
45
46
        $field->addExtraClass('no-change-track el-chosen');
47
48
        $data = new ArrayData(array(
49
            'Title'      => $this->getTitle(),
50
            'Link'       => Controller::join_links($grid->Link(), 'add-multi-class', '{class}'),
51
            'ClassField' => $field
52
        ));
53
54
        return array(
55
            $this->getFragment() => $data->renderWith(parent::class)
56
        );
57
    }
58
59
    /**
60
     * Return a list of classes for use in the "add new" block dropdown, using the block's type rather
61
     * than i18n singular name as the title
62
     *
63
     * @param  array $classes
64
     * @return array
65
     */
66
    public function applyBlockTypeTitles(array $classes)
67
    {
68
        $output = [];
69
70
        foreach ($classes as $sanitisedClassName => $originalTitle) {
71
            $className = $this->unsanitiseClassName($sanitisedClassName);
72
73
            $output[$sanitisedClassName] = singleton($className)->getType();
74
        }
75
76
        return $output;
77
    }
78
}
79