-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAssociationField.php
More file actions
164 lines (134 loc) · 6.66 KB
/
Copy pathAssociationField.php
File metadata and controls
164 lines (134 loc) · 6.66 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Field;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Contracts\Translation\TranslatableInterface;
/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
final class AssociationField implements FieldInterface
{
use FieldTrait;
public const OPTION_AUTOCOMPLETE = 'autocomplete';
public const OPTION_AUTOCOMPLETE_CALLBACK = 'autocompleteCallback';
public const OPTION_AUTOCOMPLETE_TEMPLATE = 'autocompleteTemplate';
public const OPTION_AUTOCOMPLETE_DEPENDS_ON = 'autocompleteDependsOn';
public const OPTION_EMBEDDED_CRUD_FORM_CONTROLLER = 'crudControllerFqcn';
public const OPTION_WIDGET = 'widget';
public const OPTION_QUERY_BUILDER_CALLABLE = 'queryBuilderCallable';
/** @internal this option is intended for internal use only */
public const OPTION_RELATED_URL = 'relatedUrl';
/** @internal this option is intended for internal use only */
public const OPTION_DOCTRINE_ASSOCIATION_TYPE = 'associationType';
public const WIDGET_AUTOCOMPLETE = 'autocomplete';
public const WIDGET_NATIVE = 'native';
/** @internal this option is intended for internal use only */
public const PARAM_AUTOCOMPLETE_CONTEXT = 'autocompleteContext';
/** @internal this option is intended for internal use only */
public const OPTION_RENDER_AS_EMBEDDED_FORM = 'renderAsEmbeddedForm';
public const OPTION_EMBEDDED_CRUD_FORM_NEW_PAGE_NAME = 'crudNewPageName';
public const OPTION_EMBEDDED_CRUD_FORM_EDIT_PAGE_NAME = 'crudEditPageName';
// the name of the property in the associated entity used to sort the results (only for *-To-One associations)
public const OPTION_SORT_PROPERTY = 'sortProperty';
public const OPTION_ESCAPE_HTML_CONTENTS = 'escapeHtml';
public const OPTION_PREFERRED_CHOICES = 'preferredChoices';
public static function new(string $propertyName, TranslatableInterface|string|bool|null $label = null): self
{
return (new self())
->setProperty($propertyName)
->setLabel($label)
->setTemplateName('crud/field/association')
->setFormType(EntityType::class)
->addCssClass('field-association')
->setDefaultColumns('col-md-7 col-xxl-6')
->setCustomOption(self::OPTION_AUTOCOMPLETE, false)
->setCustomOption(self::OPTION_AUTOCOMPLETE_CALLBACK, null)
->setCustomOption(self::OPTION_AUTOCOMPLETE_TEMPLATE, null)
->setCustomOption(self::OPTION_AUTOCOMPLETE_DEPENDS_ON, null)
->setCustomOption(self::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER, null)
->setCustomOption(self::OPTION_WIDGET, self::WIDGET_AUTOCOMPLETE)
->setCustomOption(self::OPTION_QUERY_BUILDER_CALLABLE, null)
->setCustomOption(self::OPTION_RELATED_URL, null)
->setCustomOption(self::OPTION_DOCTRINE_ASSOCIATION_TYPE, null)
->setCustomOption(self::OPTION_RENDER_AS_EMBEDDED_FORM, false)
->setCustomOption(self::OPTION_EMBEDDED_CRUD_FORM_NEW_PAGE_NAME, null)
->setCustomOption(self::OPTION_EMBEDDED_CRUD_FORM_EDIT_PAGE_NAME, null)
->setCustomOption(self::OPTION_ESCAPE_HTML_CONTENTS, true)
->setCustomOption(self::OPTION_PREFERRED_CHOICES, null);
}
/**
* @param list<string>|string|null $dependsOn
*/
public function autocomplete(bool $enable = true, ?callable $callback = null, ?string $template = null, bool $renderAsHtml = false, array|string|null $dependsOn = null): self
{
if (!$enable) {
return $this;
}
$this->setCustomOption(self::OPTION_AUTOCOMPLETE, true);
if (null !== $callback) {
$this->setCustomOption(self::OPTION_AUTOCOMPLETE_CALLBACK, $callback);
}
if (null !== $template) {
$this->setCustomOption(self::OPTION_AUTOCOMPLETE_TEMPLATE, $template);
}
// the renderAsHtml parameter controls the same option as renderAsHtml() method
$this->setCustomOption(self::OPTION_ESCAPE_HTML_CONTENTS, !$renderAsHtml);
if (null !== $dependsOn) {
$this->setCustomOption(self::OPTION_AUTOCOMPLETE_DEPENDS_ON, \is_string($dependsOn) ? [$dependsOn] : $dependsOn);
}
return $this;
}
public function renderAsNativeWidget(bool $asNative = true): self
{
$this->setCustomOption(self::OPTION_WIDGET, $asNative ? self::WIDGET_NATIVE : self::WIDGET_AUTOCOMPLETE);
return $this;
}
public function setCrudController(string $crudControllerFqcn): self
{
$this->setCustomOption(self::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER, $crudControllerFqcn);
return $this;
}
public function setQueryBuilder(\Closure $queryBuilderCallable): self
{
$this->setCustomOption(self::OPTION_QUERY_BUILDER_CALLABLE, $queryBuilderCallable);
return $this;
}
public function renderAsEmbeddedForm(?string $crudControllerFqcn = null, ?string $crudNewPageName = null, ?string $crudEditPageName = null): self
{
$this->setCustomOption(self::OPTION_RENDER_AS_EMBEDDED_FORM, true);
$this->setCustomOption(self::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER, $crudControllerFqcn);
$this->setCustomOption(self::OPTION_EMBEDDED_CRUD_FORM_NEW_PAGE_NAME, $crudNewPageName);
$this->setCustomOption(self::OPTION_EMBEDDED_CRUD_FORM_EDIT_PAGE_NAME, $crudEditPageName);
return $this;
}
public function setSortProperty(string $orderProperty): self
{
$this->setCustomOption(self::OPTION_SORT_PROPERTY, $orderProperty);
return $this;
}
public function renderAsHtml(bool $asHtml = true): self
{
$this->setCustomOption(self::OPTION_ESCAPE_HTML_CONTENTS, !$asHtml);
return $this;
}
/**
* Sets the preferred entities that will be displayed at the top of the dropdown,
* visually separated from the rest of entities.
*
* You can pass an array of entity objects or their primary key values:
* ->setPreferredChoices([1, 2, 3])
* ->setPreferredChoices([$featuredCategory1, $featuredCategory2])
*
* Or a callable that receives an entity and returns true for preferred choices:
* ->setPreferredChoices(fn (Category $category) => $category->isFeatured())
*
* Note: This option is not compatible with remote autocomplete (->autocomplete()).
*
* @param array<mixed>|callable $preferredChoices
*/
public function setPreferredChoices(array|callable $preferredChoices): self
{
$this->setCustomOption(self::OPTION_PREFERRED_CHOICES, $preferredChoices);
return $this;
}
}