-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathFieldContainerBundle.php
More file actions
70 lines (57 loc) · 1.93 KB
/
FieldContainerBundle.php
File metadata and controls
70 lines (57 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Solspace\Freeform\Bundles\Fields;
use Solspace\Freeform\Events\Fields\CompileFieldAttributesEvent;
use Solspace\Freeform\Fields\FieldInterface;
use Solspace\Freeform\Library\Attributes\FieldAttributesCollection;
use Solspace\Freeform\Library\Bundles\FeatureBundle;
use yii\base\Event;
class FieldContainerBundle extends FeatureBundle
{
public function __construct()
{
Event::on(
FieldInterface::class,
FieldInterface::EVENT_COMPILE_ATTRIBUTES,
[$this, 'updateContainerAttributes'],
);
}
public function updateContainerAttributes(CompileFieldAttributesEvent $event): void
{
if (FieldAttributesCollection::class !== $event->getClass()) {
return;
}
$request = \Craft::$app->request;
if ($request && $request->isCpRequest) {
$isFreeform = 'freeform' === $request->getSegment(1);
$isApi = 'api' === $request->getSegment(2);
$isForms = 'forms' === $request->getSegment(3);
if ($isFreeform && $isApi && $isForms) {
return;
}
}
$field = $event->getField();
/** @var FieldAttributesCollection $attributes */
$attributes = $event->getAttributes();
$attributes
->getContainer()
->replace('data-field-container', $field->getHandle())
->replace('data-field-type', $field->getType())
;
$attributes
->getLabel()
->replace('data-field-label', $field->getHandle())
;
$attributes
->getInput()
->replace('data-field-handle', $field->getHandle())
;
$attributes
->getInstructions()
->replace('data-field-instructions', $field->getHandle())
;
$attributes
->getError()
->replace('data-field-errors', $field->getHandle())
;
}
}