Issue #2083835 by yched: Fixed theme('field') displays no value on empty fields even if the formatter returned an output.

8.0.x
webchick 2013-09-11 13:29:55 -07:00
parent d009cac09c
commit 9bb5d68aef
2 changed files with 13 additions and 20 deletions

View File

@ -817,14 +817,15 @@ function template_preprocess_field(&$variables, $hook) {
// We want other preprocess functions and the theme implementation to have
// fast access to the field item render arrays. The item render array keys
// (deltas) should always be a subset of the keys in #items, and looping on
// those keys is faster than calling element_children() or looping on all keys
// within $element, since that requires traversal of all element properties.
// (deltas) should always be numerically indexed starting from 0, and looping
// on those keys is faster than calling element_children() or looping on all
// keys within $element, since that requires traversal of all element
// properties.
$variables['items'] = array();
foreach ($element['#items'] as $delta => $item) {
if (!empty($element[$delta])) {
$variables['items'][$delta] = $element[$delta];
}
$delta = 0;
while (!empty($element[$delta])) {
$variables['items'][$delta] = $element[$delta];
$delta++;
}
// Add default CSS classes. Since there can be many fields rendered on a page,

View File

@ -29,25 +29,17 @@ use Drupal\field\Plugin\Type\Formatter\FormatterBase;
*/
class TestFieldEmptyFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function prepareView(array $entities, $langcode, array $items) {
foreach ($entities as $id => $entity) {
if ($items[$id]->isEmpty()) {
// For fields with no value, just add the configured "empty" value.
$items[$id][0]->value = $this->getSetting('test_empty_string');
}
}
}
/**
* {@inheritdoc}
*/
public function viewElements(EntityInterface $entity, $langcode, FieldInterface $items) {
$elements = array();
if (!empty($items)) {
if ($items->isEmpty()) {
// For fields with no value, just add the configured "empty" value.
$elements[0] = array('#markup' => $this->getSetting('test_empty_string'));
}
else {
foreach ($items as $delta => $item) {
// This formatter only needs to output raw for testing.
$elements[$delta] = array('#markup' => $item->value);