diff --git a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php index 3dd7983..5dbf55b 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php @@ -174,66 +174,7 @@ public function invokeFieldMethod($method, EntityInterface $entity) { } } - /** - * {@inheritdoc} - */ - public function invokeFieldMethodMultiple($method, array $entities, $langcode) { - $entities_by_bundle = array(); - $typed_data = \Drupal::typedData(); - foreach ($entities as $id => $entity) { - $entities_by_bundle[$entity->bundle()][$id] = $entity; - } - - foreach ($entities_by_bundle as $bundle => $bundle_entities) { - $definitions = \Drupal::entityManager()->getFieldDefinitions($this->entityType, $bundle); - - foreach ($definitions as $field_name => $definition) { - $entities_items = array(); - - foreach ($bundle_entities as $id => $entity) { - // @todo Remove the condition and the second code branch when all core - // entity types are converted. - if ($entity instanceof EntityNG || $entity instanceof EntityBCDecorator) { - // @todo $entity->getTranslation()->get($name) sometimes fails, - // because Entity\Translation::getPropertyDefinitions() is empty() ?? - try { - $entities_items[$id] = $entity->getTranslation($langcode)->get($field_name); - } - catch (\InvalidArgumentException $e) { - break; - } - } - else { - $BC_mode = TRUE; - // Support BC entities. - if (!empty($definition['configurable'])) { - $itemsBC = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array(); - // @todo Exception : this calls setValue(), tries to set the - // 'formatted' property. For now, this is worked around by - // commenting out the Exception in TextProcessed::setValue(). - $entities_items[$id] = $typed_data->create($definition, $itemsBC, $field_name, $entity); - } - } - } - - $type_definition = $typed_data->getDefinition($definition['type']); - $type_definition['class']::$method($entities_items); - - // @todo Remove when all core entity types are converted. - if (!empty($BC_mode)) { - // Put back the items values in the entity. - foreach ($bundle_entities as $id => $entity) { - $itemsBC = $entities_items[$id]->getValue(TRUE); - if ($itemsBC !== array() || isset($entity->{$field_name}[$langcode])) { - $entity->{$field_name}[$langcode] = $itemsBC; - } - } - } - } - } - } - - /** + /** * {@inheritdoc} */ public function invokeFieldItemPrepareCache(EntityInterface $entity) { diff --git a/core/lib/Drupal/Core/Entity/EntityStorageControllerInterface.php b/core/lib/Drupal/Core/Entity/EntityStorageControllerInterface.php index 422d7cf..5ae3076 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageControllerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageControllerInterface.php @@ -155,21 +155,6 @@ public function getQueryServicename(); public function invokeFieldMethod($method, EntityInterface $entity); /** - * Invokes a method on the Field objects within a set of entities. - * - * This only iterates on the field values in a given language. - * - * @param string $method - * The method name. - * @param array $entities - * An array of \Drupal\Core\Entity\EntityInterface entities, keyed by entity - * id. - * @param string $langcode - * The langcode. - */ - public function invokeFieldMethodMultiple($method, array $entities, $langcode); - - /** * Invokes the prepareCache() method on all the relevant FieldItem objects. * * @param \Drupal\Core\Entity\EntityInterface $entity diff --git a/core/modules/field/field.attach.inc b/core/modules/field/field.attach.inc index fc101e0..f9c585e 100644 --- a/core/modules/field/field.attach.inc +++ b/core/modules/field/field.attach.inc @@ -1030,11 +1030,6 @@ function field_attach_prepare_view($entity_type, array $entities, array $display } } - // First let the field types do their preparation. - Drupal::entityManager() - ->getStorageController($entity_type) - ->invokeFieldMethodMultiple('prepareView', $entities, $langcode); - // Then let the formatters do their own specific massaging. For each // instance, call the prepareView() method on the formatter object handed by // the entity display. diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php index 1a8a8e0..394ec01 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php @@ -89,19 +89,4 @@ public function settingsForm(array $form, array &$form_state); */ public function instanceSettingsForm(array $form, array &$form_state); - /** - * Prepares field values prior to display. - * - * This method is invoked before the field values are handed to formatters - * for display. - * - * This method operates on multiple entities. The $entities_items parameter is - * arrays keyed by entity ID. For performance reasons, information for all - * entities should be loaded in a single query where possible. - * - * @param array $entities_items - * Array of field items, keyed by entity ID. - */ - public static function prepareView(array $entities_items); - } diff --git a/core/modules/file/file.field.inc b/core/modules/file/file.field.inc index cc2bf97..289c67f 100644 --- a/core/modules/file/file.field.inc +++ b/core/modules/file/file.field.inc @@ -184,35 +184,6 @@ function _file_generic_settings_file_directory_validate($element, &$form_state) } /** - * Implements hook_field_prepare_view(). - */ -function file_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { - // Remove files specified to not be displayed. - $fids = array(); - foreach ($entities as $id => $entity) { - foreach ($items[$id] as $delta => $item) { - if (file_field_displayed($item, $field) && !empty($item['fid'])) { - // Load the files from the files table. - $fids[] = $item['fid']; - } - } - } - - if ($fids) { - $files = file_load_multiple($fids); - - foreach ($entities as $id => $entity) { - foreach ($items[$id] as $delta => $item) { - // If the file does not exist, mark the entire item as empty. - if (!empty($item['fid'])) { - $items[$id][$delta]['entity'] = isset($files[$item['fid']]) ? $files[$item['fid']] : NULL; - } - } - } - } -} - -/** * Implements hook_field_insert(). */ function file_field_insert(EntityInterface $entity, $field, $instance, $langcode, &$items) { @@ -292,24 +263,6 @@ function file_field_is_empty($item, $field_type) { } /** - * Determines whether a file should be displayed when outputting field content. - * - * @param $item - * A field item array. - * @param $field - * A field array. - * - * @return - * Boolean TRUE if the file will be displayed, FALSE if the file is hidden. - */ -function file_field_displayed($item, $field) { - if (!empty($field['settings']['display_field'])) { - return (bool) $item['display']; - } - return TRUE; -} - -/** * Retrieves the upload validators for a file field. * * @param array $field_settings diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/FileFormatterBase.php b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/FileFormatterBase.php new file mode 100644 index 0000000..358648b --- /dev/null +++ b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/FileFormatterBase.php @@ -0,0 +1,64 @@ + $entity) { + foreach ($items[$id] as $delta => $item) { + if ($this->isDisplayed($item) && !empty($item['fid'])) { + // Load the files from the files table. + $fids[] = $item['fid']; + } + } + } + + if ($fids) { + $files = file_load_multiple($fids); + + foreach ($entities as $id => $entity) { + foreach ($items[$id] as $delta => $item) { + // If the file does not exist, mark the entire item as empty. + if (!empty($item['fid'])) { + $items[$id][$delta]['entity'] = isset($files[$item['fid']]) ? $files[$item['fid']] : NULL; + } + } + } + } + } + + /** + * Determines whether a file should be displayed when outputting field content. + * + * @param $item + * A field item array. + * @param $field + * A field array. + * + * @return + * Boolean TRUE if the file will be displayed, FALSE if the file is hidden. + */ + protected function isDisplayed($item) { + $settings = $this->getFieldSettings(); + if (!empty($settings['display_field'])) { + return (bool) $item['display']; + } + return TRUE; + } +} diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/GenericFileFormatter.php b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/GenericFileFormatter.php index bc0d16e..6eae26a 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/GenericFileFormatter.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/GenericFileFormatter.php @@ -9,7 +9,6 @@ use Drupal\field\Annotation\FieldFormatter; use Drupal\Core\Annotation\Translation; -use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\Core\Entity\EntityInterface; /** @@ -24,7 +23,7 @@ * } * ) */ -class GenericFileFormatter extends FormatterBase { +class GenericFileFormatter extends FileFormatterBase { /** * Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/RSSEnclosureFormatter.php b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/RSSEnclosureFormatter.php index ecbba24..af57980 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/RSSEnclosureFormatter.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/RSSEnclosureFormatter.php @@ -9,7 +9,6 @@ use Drupal\field\Annotation\FieldFormatter; use Drupal\Core\Annotation\Translation; -use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\Core\Entity\EntityInterface; /** @@ -24,7 +23,7 @@ * } * ) */ -class RSSEnclosureFormatter extends FormatterBase { +class RSSEnclosureFormatter extends FileFormatterBase { /** * Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/TableFormatter.php b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/TableFormatter.php index cc7a57d..95920f3 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/TableFormatter.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/TableFormatter.php @@ -9,7 +9,6 @@ use Drupal\field\Annotation\FieldFormatter; use Drupal\Core\Annotation\Translation; -use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\Core\Entity\EntityInterface; /** @@ -24,7 +23,7 @@ * } * ) */ -class TableFormatter extends FormatterBase { +class TableFormatter extends FileFormatterBase { /** * Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/UrlPlainFormatter.php b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/UrlPlainFormatter.php index 507d86f..090bd37 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/UrlPlainFormatter.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/UrlPlainFormatter.php @@ -9,7 +9,6 @@ use Drupal\field\Annotation\FieldFormatter; use Drupal\Core\Annotation\Translation; -use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\Core\Entity\EntityInterface; /** @@ -24,7 +23,7 @@ * } * ) */ -class UrlPlainFormatter extends FormatterBase { +class UrlPlainFormatter extends FileFormatterBase { /** * Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). diff --git a/core/modules/image/image.field.inc b/core/modules/image/image.field.inc index a122ba6..7dfb6d6 100644 --- a/core/modules/image/image.field.inc +++ b/core/modules/image/image.field.inc @@ -223,39 +223,6 @@ function _image_field_resolution_validate($element, &$form_state) { } /** - * Implements hook_field_prepare_view(). - */ -function image_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { - // Load the images. - file_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items); - // If there are no files specified at all, use the default. - foreach ($entities as $id => $entity) { - if (empty($items[$id])) { - $fid = array(); - // Use the default for the instance if one is available. - if (!empty($instances[$id]['settings']['default_image'])) { - $fid = array($instances[$id]['settings']['default_image']); - } - // Otherwise, use the default for the field. - elseif (!empty($field['settings']['default_image'])) { - $fid = array($field['settings']['default_image']); - } - - // Add the default image if one is found. - if ($fid && ($file = file_load($fid[0]))) { - $items[$id][0] = array( - 'is_default' => TRUE, - 'alt' => '', - 'title' => '', - 'entity' => $file, - 'fid' => $file->id(), - ); - } - } - } -} - -/** * Implements hook_field_presave(). */ function image_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) { diff --git a/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatter.php b/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatter.php index a5078a2..84b73ba 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatter.php +++ b/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatter.php @@ -9,7 +9,6 @@ use Drupal\field\Annotation\FieldFormatter; use Drupal\Core\Annotation\Translation; -use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\Core\Entity\EntityInterface; /** @@ -28,7 +27,7 @@ * } * ) */ -class ImageFormatter extends FormatterBase { +class ImageFormatter extends ImageFormatterBase { /** * {@inheritdoc} diff --git a/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatterBase.php b/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatterBase.php new file mode 100644 index 0000000..06374b6 --- /dev/null +++ b/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatterBase.php @@ -0,0 +1,53 @@ + $entity) { + if (empty($items[$id])) { + $fid = array(); + $instance = field_info_instance($entity->entityType(), $this->fieldDefinition->getFieldName(), $entity->bundle()); + // Use the default for the instance if one is available. + if (!empty($instance['settings']['default_image'])) { + $fid = array($instance['settings']['default_image']); + } + // Otherwise, use the default for the field. + // Note, that we have to bypass getFieldSetting() as this returns the + // instance-setting default. + elseif (($field = $this->fieldDefinition->getField()) && !empty($field->settings['default_image'])) { + $fid = array($field->settings['default_image']); + } + + // Add the default image if one is found. + if ($fid && ($file = file_load($fid[0]))) { + $items[$id][0] = array( + 'is_default' => TRUE, + 'alt' => '', + 'title' => '', + 'entity' => $file, + 'fid' => $file->id(), + ); + } + } + } + } + +}