Skip to content

Commit 0f52e49

Browse files
committed
Fixed widget
1 parent 1732176 commit 0f52e49

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Plugin/Field/FieldWidget/LayoutLibraryIconsWidget.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
id: 'layout_library_icons',
2323
label: new TranslatableMarkup('Layout Library Icons'),
2424
field_types: ['entity_reference'],
25+
multiple_values: TRUE,
2526
)]
2627
class LayoutLibraryIconsWidget extends OptionsWidgetBase {
2728

@@ -57,11 +58,19 @@ public static function create(ContainerInterface $container, array $configuratio
5758
);
5859
}
5960

61+
/**
62+
* {@inheritdoc}
63+
*/
64+
public static function isApplicable(FieldDefinitionInterface $field_definition) {
65+
return $field_definition->getName() == 'layout_selection';
66+
}
67+
6068
/**
6169
* {@inheritdoc}
6270
*/
6371
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
6472
$element = parent::formElement($items, $delta, $element, $form, $form_state);
73+
6574
$options = $this->getOptions($items->getEntity());
6675
/** @var \Drupal\layout_library\Entity\Layout[] $layouts */
6776
$layouts = $this->entityTypeManager->getStorage('layout')
@@ -93,10 +102,11 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
93102
$options['_none'] .= $this->renderer->render($image);
94103
}
95104

105+
$selectedOption = $this->getSelectedOptions($items);
96106
$element += [
97107
'#type' => 'radios',
98108
'#options' => $options,
99-
'#default_value' => $this->getSelectedOptions($items) ?: '_none',
109+
'#default_value' => $selectedOption[0] ?? '_none',
100110
'#attached' => ['library' => ['stanford_profile_helper/layout_library_icon_widget']],
101111
'#attributes' => ['class' => ['layout-library-icons']],
102112
];
@@ -110,5 +120,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
110120
protected function getEmptyLabel() {
111121
return $this->t('- Default -');
112122
}
123+
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
124+
dpm($values);
125+
return parent::massageFormValues($values, $form, $form_state); // TODO: Change the autogenerated stub
126+
}
113127

114128
}

tests/src/Unit/Plugin/Field/FieldWidget/LayoutLibraryIconsWidgetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function getOptions($entity = NULL) {
158158
];
159159
}
160160
public function getSelectedOptions(FieldItemListInterface $items, $delta = 0) {
161-
return 'layout_1';
161+
return ['layout_1'];
162162
}
163163
};
164164
$widget->setStringTranslation($this->translation);
@@ -189,7 +189,7 @@ public function testGetEmptyLabel() {
189189
$reflection = new \ReflectionClass($this->widget);
190190
$method = $reflection->getMethod('getEmptyLabel');
191191
$method->setAccessible(TRUE);
192-
192+
193193
$label = $method->invoke($this->widget);
194194
$this->assertInstanceOf(TranslatableMarkup::class, $label);
195195
// Check the untranslated string.

0 commit comments

Comments
 (0)