/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) { $element = parent::formElement($items, $delta, $element, $form, $form_state); // Prepare some properties for the child methods to build the actual form element. $this->required = $element['#required']; $this->multiple = $this->fieldDefinition->getFieldStorageDefinition()->isMultiple(); $this->has_value = isset($items[0]->{$this->column}); $options = $this->getOptions($items[$delta]); $selected = $this->getSelectedOptions($items); $element += array('#type' => 'multiselect', '#size' => $this->getSetting('size'), '#options' => $options, '#multiple' => $this->multiple, '#key_column' => $this->column, '#default_value' => $selected); return $element; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) { $element = parent::formElement($items, $delta, $element, $form, $form_state); $options = $this->getOptions($items[$delta]); $selected = $this->getSelectedOptions($items); // If required and there is one single option, preselect it. if ($this->required && count($options) == 1) { reset($options); $selected = array(key($options)); } if ($this->multiple) { $element += array('#type' => 'checkboxes', '#default_value' => $selected, '#options' => $options); } else { $element += array('#type' => 'radios', '#default_value' => $selected ? reset($selected) : NULL, '#options' => $options); } return $element; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $element = parent::formElement($items, $delta, $element, $form, $form_state); $element += array('#type' => 'select', '#options' => $this->getOptions($items[$delta]), '#default_value' => $this->getSelectedOptions($items, $delta), '#multiple' => $this->multiple && count($this->options) > 1); return $element; }