Issue when the field is empty

Created on 18 July 2017, over 7 years ago
Updated 9 October 2023, about 1 year ago

When my field is empty, I've got this errors :

Notice : Undefined offset: 0 dans Drupal\image_popup\Plugin\Field\FieldFormatter\ImagePopupFieldFormatter->viewElements() (ligne 182 de /var/www/html/web/modules/contrib/image_popup/src/Plugin/Field/FieldFormatter/ImagePopupFieldFormatter.php).

Error: Call to a member function get() on null in Drupal\image_popup\Plugin\Field\FieldFormatter\ImagePopupFieldFormatter->viewElements() (line 182 of modules/contrib/image_popup/src/Plugin/Field/FieldFormatter/ImagePopupFieldFormatter.php).

And, of course, there are no errors when I upload one file or more.

So, when I checked the code, I saw there is no test at the line :

public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$files = $this->getEntitiesToView($items, $langcode);

$image_style_popup = $this->getSetting('image_style_popup');
$image_style_name = $this->getSetting('image_style');
$image_fid = $files[0]->get('fid')->getValue()[0]['value'];
[...]
}

We just need to check if the size of the $items array parameters is not equal to zero and return an empty array.
Thank you for testing my patch.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024