Completed
Push — master ( a705c4...a20c7b )
by Nate
10:38
created

ElementSourceList::getSearchKeywords()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://www.flipboxfactory.com/software/element-lists/license
6
 * @link       https://www.flipboxfactory.com/software/element-lists/
7
 */
8
9
namespace flipbox\element\lists\fields;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\base\ElementInterface;
14
use craft\elements\db\ElementQueryInterface;
15
use craft\fields\BaseRelationField;
16
use flipbox\craft\elements\nestedIndex\web\assets\index\NestedElementIndex;
17
use flipbox\element\lists\db\SourceElementQuery;
18
use flipbox\element\lists\ElementList;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class ElementSourceList extends BaseRelationField
25
{
26
    /**
27
     * The element class
28
     */
29
    const ELEMENT_CLASS = Element::class;
30
31
    /**
32
     * The element query class
33
     */
34
    const ELEMENT_QUERY_CLASS = SourceElementQuery::class;
35
36
    /**
37
     * @var string Template to use for field rendering
38
     */
39
    protected $inputTemplate = 'element-lists/_components/fieldtypes/ElementSource';
40
41
    /**
42
     * @var string|null The JS class that should be initialized for the input
43
     */
44
    protected $inputJsClass = 'Craft.NestedElementIndexSelectInput';
45
46
    /**
47
     * @var string|null The JS class that should be initialized for the index
48
     */
49
    protected $indexJsClass = 'Craft.NestedElementIndex';
50
51
    /**
52
     * @var bool
53
     */
54
    protected $ignoreSearchKeywords = true;
55
56
    /**
57
     * @inheritdoc
58
     */
59
    protected static function elementType(): string
60
    {
61
        return static::ELEMENT_CLASS;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public static function displayName(): string
68
    {
69
        return Craft::t('element-lists', 'Element List');
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public static function defaultSelectionLabel(): string
76
    {
77
        return '';
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function normalizeValue($value, ElementInterface $element = null)
84
    {
85
        return ElementList::getInstance()->getSourceFields()->normalizeValue(
86
            $this,
87
            $value,
88
            $element
89
        );
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function modifyElementsQuery(ElementQueryInterface $query, $value)
96
    {
97
        return ElementList::getInstance()->getSourceFields()->modifyElementsQuery(
98
            $this,
99
            $query,
100
            $value
101
        );
102
    }
103
104
    /**
105
     * @param null $value
106
     * @param ElementInterface|null $element
107
     * @return array
108
     * @throws \yii\base\InvalidConfigException
109
     */
110
    protected function inputTemplateVariables($value = null, ElementInterface $element = null): array
111
    {
112
        Craft::$app->getView()->registerAssetBundle(NestedElementIndex::class);
113
114
        $fieldService = ElementList::getInstance()->getSourceFields();
115
116
        return [
117
            'element' => $element,
118
            'container' => 'nested-index-' . $this->handle,
119
            'elementType' => static::elementType(),
120
            'inputJsClass' => $this->inputJsClass,
121
            'inputJs' => $fieldService->getInputJs($this, $element),
122
            'indexJsClass' => $this->indexJsClass,
123
            'indexJs' => $fieldService->getIndexJs($this, $element)
124
        ];
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function getSearchKeywords($value, ElementInterface $element): string
131
    {
132
        if ($this->ignoreSearchKeywords === true) {
133
            return '';
134
        }
135
136
        return parent::getSearchKeywords($value, $element);
137
    }
138
}
139