I've created custom field type and widget plugins in D8.
I am presenting an element in my widget as checkboxes, but I need to transform the resulting array into text delimited by pipes. i.e. a|b|c
.
How would I approach this in the field widget?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI've created custom field type and widget plugins in D8.
I am presenting an element in my widget as checkboxes, but I need to transform the resulting array into text delimited by pipes. i.e. a|b|c
.
How would I approach this in the field widget?
messageFormValues
in the widget class did the job.
class MyDefaultWidget extends WidgetBase {
...
/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
// Change the format of $item['sections'] from array to pipe delimited string.
foreach ($values as &$item) {
$sections = [];
if (is_array($item['sections'])) {
foreach ($item['sections'] as $s) {
if ($s !== 0) {
$sections[] = $s;
}
}
}
$item['sections'] = implode('|', $sections);
}
return $values;
}
...
}