diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php index af3234c..111e835 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php @@ -282,11 +282,6 @@ public function viewField(FieldItemListInterface $items, $display_options = arra $entity = $items->getEntity(); $field_name = $items->getFieldDefinition()->getName(); - // Return nothing if the field doesn't exist. - if (!$entity->hasField($field_name)) { - return $output; - } - // Get the display object. if (is_string($display_options)) { $view_mode = $display_options; @@ -320,24 +315,21 @@ public function viewField(FieldItemListInterface $items, $display_options = arra * {@inheritdoc} */ public function viewFieldItem(FieldItemInterface $item, $display = array()) { - $output = array(); $entity = $item->getEntity(); $field_name = $item->getFieldDefinition()->getName(); - if ($entity->hasField($field_name)) { - // Clone the entity since we are going to modify field values. - $clone = clone $entity; + // Clone the entity since we are going to modify field values. + $clone = clone $entity; - // Push the item as the single value for the field, and defer to - // viewFieldItemList() to build the render array for the whole list. - $clone->{$field_name}->setValue(array($item->getValue())); - $elements = $this->viewField($clone->{$field_name}, $display); + // Push the item as the single value for the field, and defer to + // viewFieldItemList() to build the render array for the whole list. + $clone->{$field_name}->setValue(array($item->getValue())); + $elements = $this->viewField($clone->{$field_name}, $display); - // Extract the part of the render array we need. - $output = isset($elements[0]) ? $elements[0] : array(); - if (isset($elements['#access'])) { - $output['#access'] = $elements['#access']; - } + // Extract the part of the render array we need. + $output = isset($elements[0]) ? $elements[0] : array(); + if (isset($elements['#access'])) { + $output['#access'] = $elements['#access']; } return $output; diff --git a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php index 570770f..a076dc9 100644 --- a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php +++ b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php @@ -11,9 +11,6 @@ use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Core\Entity\EntityViewBuilderInterface; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Field\FieldItemInterface; -use Drupal\Core\Field\FieldItemListInterface; -use Drupal\entity\Entity\EntityViewDisplay; /** * Provides a Block view builder. @@ -103,75 +100,4 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la */ public function resetCache(array $ids = NULL) { } - /** - * {@inheritdoc} - */ - public function viewField(FieldItemListInterface $items, $display_options = array()) { - $output = array(); - $entity = $items->getEntity(); - - $field_definition = $items->getFieldDefinition(); - $field_name = $field_definition->getName(); - - // Get the formatter object. - if (is_string($display_options)) { - $view_mode = $display_options; - $formatter = EntityViewDisplay::collectRenderDisplay($entity, $view_mode)->getRenderer($field_name); - } - else { - $view_mode = '_custom'; - // hook_field_attach_display_alter() needs to receive the 'prepared' - // $display_options, so we cannot let preparation happen internally. - $formatter_manager = \Drupal::service('plugin.manager.field.formatter'); - $display_options = $formatter_manager->prepareConfiguration($field_definition->getType(), $display_options); - $formatter = $formatter_manager->getInstance(array( - 'field_definition' => $field_definition, - 'view_mode' => $view_mode, - 'prepare' => FALSE, - 'configuration' => $display_options, - )); - } - - if ($formatter) { - // Invoke the formatter's prepareView() and view() methods. - $formatter->prepareView(array($entity->id() => $items)); - $result = $formatter->view($items); - - // Invoke hook_field_attach_view_alter() to let other modules alter the - // renderable array, as in a full field_attach_view() execution. - $context = array( - 'entity' => $entity, - 'view_mode' => $view_mode, - 'display_options' => $display_options, - 'langcode' => $items->getLangcode(), - ); - \Drupal::moduleHandler()->alter('field_attach_view', $result, $context); - - if (isset($result[$field_name])) { - $output = $result[$field_name]; - } - } - - return $output; - } - - /** - * {@inheritdoc} - */ - public function viewFieldItem(FieldItemInterface $item, $display_options = array()) { - // Push the item as the single value for the field, and defer to - // viewFieldItemList() to build the render array for the whole list. - $clone_items = clone $item->getParent(); - $clone_items->setValue(array($item->getValue())); - $elements = $this->viewField($clone_items, $display_options); - - // Extract the part of the render array we need. - $output = isset($elements[0]) ? $elements[0] : array(); - if (isset($elements['#access'])) { - $output['#access'] = $elements['#access']; - } - - return $output; - } - } diff --git a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php index 761b61d..2185fdc 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php +++ b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php @@ -682,9 +682,6 @@ public function getItems($values) { 'views_field' => $this, 'views_row_id' => $this->view->row_index, ); - - $langcode = $this->field_langcode($entity); - $entity = \Drupal::entityManager()->getTranslationFromContext($entity, $langcode); $render_array = $entity->get($this->definition['field_name'])->view($display); $items = array(); @@ -725,7 +722,10 @@ public function getItems($values) { */ function process_entity(EntityInterface $entity) { $processed_entity = clone $entity; + $langcode = $this->field_langcode($processed_entity); + $processed_entity = $processed_entity->getTranslation($langcode); + // If we are grouping, copy our group fields into the cloned entity. // It's possible this will cause some weirdness, but there's only // so much we can hope to do. @@ -750,10 +750,10 @@ function process_entity(EntityInterface $entity) { if ($data) { // Now, overwrite the original value with our aggregated value. // This overwrites it so there is always just one entry. - $processed_entity->getTranslation($langcode)->{$this->definition['field_name']} = array($base_value); + $processed_entity->{$this->definition['field_name']} = array($base_value); } else { - $processed_entity->getTranslation($langcode)->{$this->definition['field_name']} = array(); + $processed_entity->{$this->definition['field_name']} = array(); } } @@ -764,7 +764,7 @@ function process_entity(EntityInterface $entity) { // We are supposed to show only certain deltas. if ($this->limit_values && !empty($processed_entity->{$this->definition['field_name']})) { - $all_values = !empty($processed_entity->getTranslation($langcode)->{$this->definition['field_name']}) ? $processed_entity->getTranslation($langcode)->{$this->definition['field_name']}->getValue() : array(); + $all_values = !empty($processed_entity->{$this->definition['field_name']}) ? $processed_entity->{$this->definition['field_name']}->getValue() : array(); if ($this->options['delta_reversed']) { $all_values = array_reverse($all_values); } @@ -810,7 +810,7 @@ function process_entity(EntityInterface $entity) { } } } - $processed_entity->getTranslation($langcode)->{$this->definition['field_name']} = $new_values; + $processed_entity->{$this->definition['field_name']} = $new_values; } return $processed_entity; @@ -867,7 +867,6 @@ function field_langcode(EntityInterface $entity) { // for the selected language. FieldItemListInterface::view() does this as // well, but since the returned language code is used before calling it, // the fallback needs to happen explicitly. - // @todo This might not be needed anymore. Check with plach. $langcode = $this->entityManager->getTranslationFromContext($entity, $langcode)->language()->id; return $langcode; diff --git a/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php b/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php index f5509ed..90c8e56 100644 --- a/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php @@ -202,8 +202,6 @@ function testFieldItemView() { $item = $this->entity->{$this->field_name}[$delta]; $output = $item->view(); $this->content = drupal_render($output); - debug($this->content); - debug($setting . '|' . $value['value']); $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta))); }