Skip to content
Snippets Groups Projects
Commit 2635b665 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2020677 by yched: file_field_prepare_view() should not delete items.

parent 0e8571d1
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -191,27 +191,22 @@ function file_field_prepare_view($entity_type, $entities, $field, $instances, $l ...@@ -191,27 +191,22 @@ function file_field_prepare_view($entity_type, $entities, $field, $instances, $l
$fids = array(); $fids = array();
foreach ($entities as $id => $entity) { foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) { foreach ($items[$id] as $delta => $item) {
if (!file_field_displayed($item, $field)) { if (file_field_displayed($item, $field) && !empty($item['fid'])) {
unset($items[$id][$delta]);
}
elseif (!empty($item['fid'])) {
// Load the files from the files table. // Load the files from the files table.
$fids[] = $item['fid']; $fids[] = $item['fid'];
} }
} }
// Ensure consecutive deltas.
$items[$id] = array_values($items[$id]);
} }
if ($fids) {
$files = file_load_multiple($fids); $files = file_load_multiple($fids);
foreach ($entities as $id => $entity) { foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) { foreach ($items[$id] as $delta => $item) {
// If the file does not exist, mark the entire item as empty. // If the file does not exist, mark the entire item as empty.
if (empty($item['fid']) || !isset($files[$item['fid']])) { if (!empty($item['fid'])) {
$items[$id][$delta] = NULL; $items[$id][$delta]['entity'] = isset($files[$item['fid']]) ? $files[$item['fid']] : NULL;
} }
else {
$items[$id][$delta]['entity'] = $files[$item['fid']];
} }
} }
} }
...@@ -854,11 +849,13 @@ function theme_file_formatter_table($variables) { ...@@ -854,11 +849,13 @@ function theme_file_formatter_table($variables) {
$header = array(t('Attachment'), t('Size')); $header = array(t('Attachment'), t('Size'));
$rows = array(); $rows = array();
foreach ($variables['items'] as $delta => $item) { foreach ($variables['items'] as $delta => $item) {
if ($item['display'] && $item['entity']) {
$rows[] = array( $rows[] = array(
theme('file_link', array('file' => $item['entity'])), theme('file_link', array('file' => $item['entity'])),
format_size($item['entity']->getSize()), format_size($item['entity']->getSize()),
); );
} }
}
return empty($rows) ? '' : theme('table', array('header' => $header, 'rows' => $rows)); return empty($rows) ? '' : theme('table', array('header' => $header, 'rows' => $rows));
} }
...@@ -33,12 +33,14 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { ...@@ -33,12 +33,14 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array(); $elements = array();
foreach ($items as $delta => $item) { foreach ($items as $delta => $item) {
if ($item['display'] && $item['entity']) {
$elements[$delta] = array( $elements[$delta] = array(
'#theme' => 'file_link', '#theme' => 'file_link',
'#file' => $item['entity'], '#file' => $item['entity'],
'#description' => $item['description'], '#description' => $item['description'],
); );
} }
}
return $elements; return $elements;
} }
......
...@@ -34,7 +34,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { ...@@ -34,7 +34,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
// Add the first file as an enclosure to the RSS item. RSS allows only one // Add the first file as an enclosure to the RSS item. RSS allows only one
// enclosure per item. See: http://en.wikipedia.org/wiki/RSS_enclosure // enclosure per item. See: http://en.wikipedia.org/wiki/RSS_enclosure
foreach ($items as $item) { foreach ($items as $item) {
if ($item['display']) { if ($item['display'] && $item['entity']) {
$file = $item['entity']; $file = $item['entity'];
$entity->rss_elements[] = array( $entity->rss_elements[] = array(
'key' => 'enclosure', 'key' => 'enclosure',
......
...@@ -33,8 +33,10 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { ...@@ -33,8 +33,10 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array(); $elements = array();
foreach ($items as $delta => $item) { foreach ($items as $delta => $item) {
if ($item['display'] && $item['entity']) {
$elements[$delta] = array('#markup' => empty($item['entity']) ? '' : file_create_url($item['entity']->getFileUri())); $elements[$delta] = array('#markup' => empty($item['entity']) ? '' : file_create_url($item['entity']->getFileUri()));
} }
}
return $elements; return $elements;
} }
......
...@@ -107,6 +107,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { ...@@ -107,6 +107,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
$image_style_setting = $this->getSetting('image_style'); $image_style_setting = $this->getSetting('image_style');
foreach ($items as $delta => $item) { foreach ($items as $delta => $item) {
if ($item['entity']) {
if (isset($link_file)) { if (isset($link_file)) {
$image_uri = $item['entity']->getFileUri(); $image_uri = $item['entity']->getFileUri();
$uri = array( $uri = array(
...@@ -121,6 +122,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { ...@@ -121,6 +122,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
'#path' => isset($uri) ? $uri : '', '#path' => isset($uri) ? $uri : '',
); );
} }
}
return $elements; return $elements;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment