1

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?

  • Wouldn't this be the job of the field formatter? – 4k4 Dec 6 '17 at 12:28
  • No, that is for display purposes. – Coder1 Dec 6 '17 at 18:36
0

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;
  }
  ...
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.