1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-element-lists/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-element-lists/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\element\lists\fields; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\Element; |
13
|
|
|
use craft\base\ElementInterface; |
14
|
|
|
use craft\events\FieldElementEvent; |
15
|
|
|
use flipbox\craft\element\lists\relationships\RelationshipInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 2.0.0 |
20
|
|
|
* |
21
|
|
|
* @property string $settingsTemplate |
22
|
|
|
*/ |
23
|
|
|
trait ElementListTrait |
24
|
|
|
{ |
25
|
|
|
use ModifyElementQueryTrait, |
26
|
|
|
NormalizeValueTrait, |
27
|
|
|
InputTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $ignoreSearchKeywords = true; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function getSearchKeywords($value, ElementInterface $element): string |
38
|
|
|
{ |
39
|
|
|
if ($this->ignoreSearchKeywords === true) { |
40
|
|
|
return ''; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return parent::getSearchKeywords($value, $element); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Identify whether a sort order should be enforced. |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function ensureSortOrder(): bool |
52
|
|
|
{ |
53
|
|
|
return $this->sortable; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Allow the settings to identify whether the element should be sortable |
58
|
|
|
* |
59
|
|
|
* @param bool $sortable |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setSortable(bool $sortable = null) |
63
|
|
|
{ |
64
|
|
|
$this->sortable = $sortable === true; |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the sortable attribute value |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function getSortable(): bool |
74
|
|
|
{ |
75
|
|
|
return $this->sortable; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
* @throws \Twig\Error\LoaderError |
81
|
|
|
* @throws \Twig\Error\RuntimeError |
82
|
|
|
* @throws \Twig\Error\SyntaxError |
83
|
|
|
*/ |
84
|
|
|
public function getSettingsHtml() |
85
|
|
|
{ |
86
|
|
|
return Craft::$app->getView()->renderTemplate( |
87
|
|
|
'element-lists/_components/fieldtypes/settings', |
88
|
|
|
[ |
89
|
|
|
'settingsTemplate' => $this->settingsTemplate, |
90
|
|
|
'field' => $this, |
91
|
|
|
] |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Our value is not an ElementQueryInterface and therefore we should handle it |
97
|
|
|
* differently. |
98
|
|
|
* |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
|
|
public function afterElementSave(ElementInterface $element, bool $isNew) |
102
|
|
|
{ |
103
|
|
|
// Skip if the element is just propagating, and we're not localizing relations |
104
|
|
|
/** @var Element $element */ |
105
|
|
|
if (!$element->propagating || $this->localizeRelations) { |
|
|
|
|
106
|
|
|
/** @var RelationshipInterface $value */ |
107
|
|
|
$value = $element->getFieldValue($this->handle); |
108
|
|
|
|
109
|
|
|
if ($value->isMutated()) { |
110
|
|
|
$value->save(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Trigger an 'afterElementSave' event |
115
|
|
|
if ($this->hasEventHandlers(self::EVENT_AFTER_ELEMENT_SAVE)) { |
|
|
|
|
116
|
|
|
$this->trigger(self::EVENT_AFTER_ELEMENT_SAVE, new FieldElementEvent([ |
|
|
|
|
117
|
|
|
'element' => $element, |
118
|
|
|
'isNew' => $isNew, |
119
|
|
|
])); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: