Problem/Motivation

Error: Class "Drupal\bg_img_field\Plugin\Field\FieldFormatter\File" not found in Drupal\bg_img_field\Plugin\Field\FieldFormatter\BgImgFieldFormatter->viewElements() on line 188

/modules/contrib/bg_img_field/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php).

Steps to reproduce

I upgraded this module from version 1.7 that i had patched to use in Drupal 10, but the new code comes with a break in the code and unexpected error pages

Proposed resolution

I changed line 189 like it was before in version 1.7 'file' => File::load($item['target_id']),

and
use Drupal\file\Entity\File; was included back again like it was in version 1.7 and everything worked

See screenshots attached

Comments

Thomas Kaisuka created an issue. See original summary.

sarwan_verma’s picture

Status: Active » Needs review
StatusFileSize
new894 bytes

Hi @Thomas Kaisuka,

I have resolved this issue "Error: Class "Drupal\bg_img_field\Plugin\Field\FieldFormatter\File" not found in Drupal\bg_img_field\Plugin\Field\FieldFormatter\BgImgFieldFormatter->viewElements()" and also attached the patch,
please review and verify.

sandeep_k’s picture

StatusFileSize
new411.18 KB
new745.13 KB

Hi @sarwan_verma, I've tested shared Patch- bg_img_field-3452677 on Drupal version- 10.3-dev. The patch was applied successfully but the error is still does not fixed for me, Added before and after patch results.

nicholass’s picture

10.3 I get Error: Call to a member function load() on null in Drupal\bg_img_field\Plugin\Field\FieldFormatter\BgImgFieldFormatter->viewElements() (line 188 of modules/contrib/bg_img_field/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php). which is the same line this issue has a problem with.

nicholass’s picture

Plus I get

Warning: Undefined variable $fileSystem in Drupal\bg_img_field\Plugin\Field\FieldFormatter\BgImgFieldFormatter->__construct() (line 117 of modules/contrib/bg_img_field/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php).

and

Warning: Undefined variable $imageFactory in Drupal\bg_img_field\Plugin\Field\FieldFormatter\BgImgFieldFormatter->__construct() (line 118 of modules/contrib/bg_img_field/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php).

So not sure how 2.1 release is working for anyone! But the undefined vars probably explains your original issue.

nicholass’s picture

I started a new issue but check out https://www.drupal.org/project/bg_img_field/issues/3484950 and see if it solves your orginal error, I never got your exact error, but I assume it stems from the same bad code.

john franklin’s picture

Status: Needs review » Closed (duplicate)