Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
prefixabledataprovidertrait.php
1<?php
2
4
6
16trait PrefixableDataProviderTrait
17{
28 protected function splitPrefixFilterValues(string $prefix, array $fields): array
29 {
30 $otherFields = [];
31 $prefixFields = [];
32
33 foreach ($fields as $nameWithPrefix => $value)
34 {
35 $name = $this->removePrefix($prefix, $nameWithPrefix);
36
37 if ($name === $nameWithPrefix)
38 {
39 $otherFields[$name] = $value;
40 }
41 else
42 {
43 $prefixFields[$name] = $value;
44 }
45 }
46
47 return [$prefixFields, $otherFields];
48 }
49
50 protected function removePrefix(string $prefix, string $fieldId): string
51 {
52 return str_replace($prefix, '', $fieldId);
53 }
54
55 protected function addPrefix(string $prefix, string $fieldId): string
56 {
57 return $prefix . $fieldId;
58 }
59
68 protected function appendPrefixName(string $prefixNameTemplate, string $fieldName): string
69 {
70 return str_replace('#NAME#', $fieldName, $prefixNameTemplate);
71 }
72
84 protected function prepareFieldsByPrefix(string $prefix, array $fields, ?string $prefixNameTemplate = null, ?array $iconParams = null, ?string $sectionId = null): array
85 {
86 $result = [];
87
88 foreach ($fields as $id => $field)
89 {
90 if ($field instanceof Field)
91 {
92 $newId = $this->addPrefix($prefix, $id);
93 $field->setID($newId);
94
95 $name = $field->getName();
96 if ($name)
97 {
98 if (isset($prefixNameTemplate))
99 {
100 $name = $this->appendPrefixName($prefixNameTemplate, $name);
101 }
102
103 $field->setName($name);
104 }
105
106 if (isset($iconParams))
107 {
108 $field->setIconParams($iconParams);
109 }
110
111 if (isset($sectionId))
112 {
113 $field->setSectionId($sectionId);
114 }
115
116 $result[$newId] = $field;
117 }
118 }
119
120 return $result;
121 }
122}