1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\entity_browser_entity_form\Plugin\EntityBrowser\Widget; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Entity\EntityTypeInterface; |
6
|
|
|
use Drupal\Core\Form\FormStateInterface; |
7
|
|
|
use Drupal\entity_browser\WidgetBase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Uses a view to provide entity listing in a browser's widget. |
11
|
|
|
* |
12
|
|
|
* @EntityBrowserWidget( |
13
|
|
|
* id = "entity_form", |
14
|
|
|
* label = @Translation("Entity form"), |
15
|
|
|
* description = @Translation("Provides entity form widget.") |
16
|
|
|
* ) |
17
|
|
|
*/ |
18
|
|
|
class EntityForm extends WidgetBase { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function defaultConfiguration() { |
24
|
|
|
return array( |
25
|
|
|
'entity_type' => NULL, |
26
|
|
|
'bundle' => NULL, |
27
|
|
|
) + parent::defaultConfiguration(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) { |
34
|
|
|
if (empty($this->configuration['entity_type']) || empty($this->configuration['bundle'])) { |
35
|
|
|
return [ |
36
|
|
|
'#markup' => t('Entity type or bundle are no configured correctly.'), |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return [ |
41
|
|
|
'inline_entity_form' => [ |
42
|
|
|
'#type' => 'inline_entity_form', |
43
|
|
|
'#op' => 'add', |
44
|
|
|
'#entity_type' => $this->configuration['entity_type'], |
45
|
|
|
'#bundle' => $this->configuration['bundle'], |
46
|
|
|
], |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function submit(array &$element, array &$form, FormStateInterface $form_state) { |
54
|
|
|
$this->selectEntities([$element['inline_entity_form']['#entity']], $form_state); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
61
|
|
|
$parents = ['table', $this->uuid(), 'form']; |
62
|
|
|
$entity_type = $form_state->hasValue(array_merge($parents, ['entity_type'])) ? $form_state->getValue(array_merge($parents, ['entity_type'])) : $this->configuration['entity_type']; |
63
|
|
|
$bundle = $form_state->hasValue(array_merge($parents, ['bundle', 'select'])) ? $form_state->getValue(array_merge($parents, ['bundle', 'select'])) : $this->configuration['bundle']; |
64
|
|
|
|
65
|
|
|
$definitions = $this->entityManager->getDefinitions(); |
66
|
|
|
$entity_types = array_combine( |
67
|
|
|
array_keys($definitions), |
68
|
|
|
array_map(function (EntityTypeInterface $item) { return $item->getLabel(); }, $definitions) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$form['entity_type'] = [ |
72
|
|
|
'#type' => 'select', |
73
|
|
|
'#title' => $this->t('Entity type'), |
74
|
|
|
'#options' => $entity_types, |
75
|
|
|
'#default_value' => $entity_type, |
76
|
|
|
'#ajax' => [ |
77
|
|
|
'wrapper' => 'bundle-wrapper', |
78
|
|
|
'callback' => [$this, 'updateBundle'], |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$bundles = []; |
83
|
|
|
if ($entity_type) { |
84
|
|
|
$definitions = $this->entityManager->getBundleInfo($entity_type); |
85
|
|
|
$bundles = array_map(function ($item) { return $item['label']; }, $definitions); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$form['bundle'] = [ |
89
|
|
|
'#type' => 'container', |
90
|
|
|
'select' => [ |
91
|
|
|
'#type' => 'select', |
92
|
|
|
'#title' => $this->t('Bundle'), |
93
|
|
|
'#options' => $bundles, |
94
|
|
|
'#default_value' => $bundle, |
95
|
|
|
], |
96
|
|
|
'#attributes' => ['id' => 'bundle-wrapper'], |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
return $form; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* AJAX callback for bundle dropdown update. |
104
|
|
|
*/ |
105
|
|
|
public function updateBundle($form, FormStateInterface $form_state) { |
106
|
|
|
return $form['widgets']['table'][$this->uuid()]['form']['bundle']; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
113
|
|
|
parent::submitConfigurationForm($form, $form_state); |
114
|
|
|
$this->configuration['bundle'] = $this->configuration['bundle']['select']; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|