1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\entity_browser_entity_form\Plugin\EntityBrowser\Widget; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Entity\EntityTypeBundleInfoInterface; |
6
|
|
|
use Drupal\Core\Entity\EntityTypeInterface; |
7
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
8
|
|
|
use Drupal\Core\Form\FormStateInterface; |
9
|
|
|
use Drupal\entity_browser\WidgetBase; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Uses a view to provide entity listing in a browser's widget. |
15
|
|
|
* |
16
|
|
|
* @EntityBrowserWidget( |
17
|
|
|
* id = "entity_form", |
18
|
|
|
* label = @Translation("Entity form"), |
19
|
|
|
* description = @Translation("Provides entity form widget.") |
20
|
|
|
* ) |
21
|
|
|
*/ |
22
|
|
|
class EntityForm extends WidgetBase { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The entity type bundle info service. |
26
|
|
|
* |
27
|
|
|
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface |
28
|
|
|
*/ |
29
|
|
|
protected $entityTypeBundleInfo; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructs widget plugin. |
33
|
|
|
* |
34
|
|
|
* @param array $configuration |
35
|
|
|
* A configuration array containing information about the plugin instance. |
36
|
|
|
* @param string $plugin_id |
37
|
|
|
* The plugin_id for the plugin instance. |
38
|
|
|
* @param mixed $plugin_definition |
39
|
|
|
* The plugin implementation definition. |
40
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
41
|
|
|
* Event dispatcher service. |
42
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
43
|
|
|
* The entity type manager service. |
44
|
|
|
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info |
45
|
|
|
* The entity type bundle info service. |
46
|
|
|
*/ |
47
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) { |
48
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager); |
49
|
|
|
$this->entityTypeBundleInfo = $entity_type_bundle_info; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
|
|
|
56
|
|
|
return new static( |
57
|
|
|
$configuration, |
58
|
|
|
$plugin_id, |
59
|
|
|
$plugin_definition, |
60
|
|
|
$container->get('event_dispatcher'), |
61
|
|
|
$container->get('entity_type.manager'), |
62
|
|
|
$container->get('entity_type.bundle.info') |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function defaultConfiguration() { |
70
|
|
|
return array( |
71
|
|
|
'entity_type' => NULL, |
72
|
|
|
'bundle' => NULL, |
73
|
|
|
) + parent::defaultConfiguration(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) { |
80
|
|
|
if (empty($this->configuration['entity_type']) || empty($this->configuration['bundle'])) { |
81
|
|
|
return [ |
82
|
|
|
'#markup' => t('Entity type or bundle are no configured correctly.'), |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return [ |
87
|
|
|
'inline_entity_form' => [ |
88
|
|
|
'#type' => 'inline_entity_form', |
89
|
|
|
'#op' => 'add', |
90
|
|
|
'#entity_type' => $this->configuration['entity_type'], |
91
|
|
|
'#bundle' => $this->configuration['bundle'], |
92
|
|
|
], |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function submit(array &$element, array &$form, FormStateInterface $form_state) { |
100
|
|
|
$this->selectEntities([$element['inline_entity_form']['#entity']], $form_state); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
107
|
|
|
$parents = ['table', $this->uuid(), 'form']; |
108
|
|
|
$entity_type = $form_state->hasValue(array_merge($parents, ['entity_type'])) ? $form_state->getValue(array_merge($parents, ['entity_type'])) : $this->configuration['entity_type']; |
109
|
|
|
$bundle = $form_state->hasValue(array_merge($parents, ['bundle', 'select'])) ? $form_state->getValue(array_merge($parents, ['bundle', 'select'])) : $this->configuration['bundle']; |
110
|
|
|
|
111
|
|
|
$definitions = $this->entityTypeManager->getDefinitions(); |
112
|
|
|
$entity_types = array_combine( |
113
|
|
|
array_keys($definitions), |
114
|
|
|
array_map(function (EntityTypeInterface $item) { |
115
|
|
|
return $item->getLabel(); |
116
|
|
|
}, $definitions) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$form['entity_type'] = [ |
120
|
|
|
'#type' => 'select', |
121
|
|
|
'#title' => $this->t('Entity type'), |
122
|
|
|
'#options' => $entity_types, |
123
|
|
|
'#default_value' => $entity_type, |
124
|
|
|
'#ajax' => [ |
125
|
|
|
'wrapper' => 'bundle-wrapper', |
126
|
|
|
'callback' => [$this, 'updateBundle'], |
127
|
|
|
], |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
$bundles = []; |
131
|
|
|
if ($entity_type) { |
132
|
|
|
$definitions = $this->entityTypeBundleInfo->getBundleInfo($entity_type); |
133
|
|
|
$bundles = array_map(function ($item) { |
134
|
|
|
return $item['label']; |
135
|
|
|
}, $definitions); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$form['bundle'] = [ |
139
|
|
|
'#type' => 'container', |
140
|
|
|
'select' => [ |
141
|
|
|
'#type' => 'select', |
142
|
|
|
'#title' => $this->t('Bundle'), |
143
|
|
|
'#options' => $bundles, |
144
|
|
|
'#default_value' => $bundle, |
145
|
|
|
], |
146
|
|
|
'#attributes' => ['id' => 'bundle-wrapper'], |
147
|
|
|
]; |
148
|
|
|
|
149
|
|
|
return $form; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* AJAX callback for bundle dropdown update. |
154
|
|
|
*/ |
155
|
|
|
public function updateBundle($form, FormStateInterface $form_state) { |
156
|
|
|
return $form['widgets']['table'][$this->uuid()]['form']['bundle']; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
163
|
|
|
parent::submitConfigurationForm($form, $form_state); |
164
|
|
|
$this->configuration['bundle'] = $this->configuration['bundle']['select']; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.