diff --git a/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php b/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php index 61a83ca..0754c53 100644 --- a/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php +++ b/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php @@ -35,23 +35,21 @@ class OptionsDefaultFormatter extends FormatterBase { public function viewElements(FieldItemListInterface $items) { $elements = array(); - $provider = $items->getFieldDefinition() - ->getFieldStorageDefinition() - ->getOptionsProvider('value', $items->getEntity()); - // Flatten the possible options, to support opt groups. - $options = $this->flattenOptions($provider->getPossibleOptions()); - - foreach ($items as $delta => $item) { - $value = $item->value; - - // If there's a single string label for the value, output that. - if (isset($options[$value]) && is_string($options[$value])) { - $output = $this->fieldFilterXss($options[$value]); + // Only collect allowed options if there are actually items to display. + if ($items->count()) { + $provider = $items->getFieldDefinition() + ->getFieldStorageDefinition() + ->getOptionsProvider('value', $items->getEntity()); + // Flatten the possible options, to support opt groups. + $options = $this->flattenOptions($provider->getPossibleOptions()); + + foreach ($items as $delta => $item) { + $value = $item->value; + // If the stored value is in the current set of allowed values, display + // the associated label, otherwise just display the raw value. + $output = isset($options[$value]) ? $options[$value] : $value; + $elements[$delta] = array('#markup' => $this->fieldFilterXss($output)); } - else { - $output = $this->fieldFilterXss($value); - } - $elements[$delta] = array('#markup' => $output); } return $elements;