From 5b01f9a137cd3810c95e0bed9faab8b177e98c1d Mon Sep 17 00:00:00 2001
From: Sidharth Soman <60124-sidharth_soman@users.noreply.drupalcode.org>
Date: Thu, 23 May 2024 20:50:12 +0000
Subject: [PATCH] Fixed phpcs issues.

---
 .../FieldFormatter/BgImgFieldFormatter.php    | 47 +++++++++++++++----
 src/Plugin/Field/FieldType/BgImgItem.php      |  1 -
 .../Field/FieldWidget/BgImageFieldWidget.php  | 20 +++++---
 .../media/Source/BackgroundImageMedia.php     |  3 +-
 templates/background-style.html.twig          |  2 +-
 5 files changed, 54 insertions(+), 19 deletions(-)

diff --git a/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php b/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
index 8c118ce..37e3d76 100644
--- a/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
+++ b/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
@@ -6,8 +6,6 @@ use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Cache\Cache;
 use Drupal\Core\Field\FieldItemListInterface;
 use Drupal\Core\Form\FormStateInterface;
-use Drupal\file\Entity\File;
-use Drupal\image\Entity\ImageStyle;
 use Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\bg_img_field\Component\Render\CSSSnippet;
@@ -18,6 +16,8 @@ use Drupal\Core\Utility\LinkGeneratorInterface;
 use Drupal\Core\Session\AccountInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Drupal\Core\Url;
+use Drupal\Core\File\FileSystemInterface;
+use Drupal\Core\Image\ImageFactory;
 
 /**
  * Plugin implementation of the 'image' formatter.
@@ -35,9 +35,26 @@ use Drupal\Core\Url;
  */
 class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerFactoryPluginInterface {
 
-  // @var Drupal\Core\Logger\LoggerChannelTrait
+  /**
+   * Logger channel trait.
+   *
+ * @var Drupal\Core\Logger\LoggerChannelTrait */
   use LoggerChannelTrait;
 
+  /**
+   * The file system service.
+   *
+   * @var \Drupal\Core\File\FileSystemInterface
+   */
+  protected $fileSystem;
+
+  /**
+   * The image factory service.
+   *
+   * @var \Drupal\Core\Image\ImageFactory
+   */
+  protected $imageFactory;
+
   /**
    * Constructor for the Background Image Formatter.
    *
@@ -64,6 +81,10 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
    *   Help generate links.
    * @param \Drupal\Core\Session\AccountInterface $current_user
    *   The current user.
+   * @param \Drupal\Core\File\FileSystemInterface $fileSystem
+   *   The file system.
+   * @param \Drupal\Core\Image\ImageFactory $imageFactory
+   *   The image factory.
    */
   public function __construct(
     $plugin_id,
@@ -76,7 +97,9 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
     EntityStorageInterface $responsive_image_style_storage,
     EntityStorageInterface $image_style_storage,
     LinkGeneratorInterface $link_generator,
-    AccountInterface $current_user
+    AccountInterface $current_user,
+    FileSystemInterface $fileSystem,
+    ImageFactory $imageFactory,
   ) {
     parent::__construct(
       $plugin_id,
@@ -93,6 +116,8 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
     );
 
     $this->logger = $this->getLogger('bg_img_field');
+    $this->fileSystem = $fileSystem;
+    $this->imageFactory = $imageFactory;
   }
 
   /**
@@ -162,7 +187,7 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
     $files = [];
     foreach ($items->getValue() as $item) {
       $files[] = [
-        'file' => File::load($item['target_id']),
+        'file' => $this->fileSystem->load($item['target_id']),
         'item' => $item,
       ];
     }
@@ -226,8 +251,8 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
 
       // Attach to head on element to create style tag in the html head.
       if (!empty($css)) {
-        $current_path =  \Drupal::request()->getRequestUri();
-        if(preg_match('/node\/(\d+)\/layout/', $current_path, $matches)) {
+        $current_path = \Drupal::request()->getRequestUri();
+        if (preg_match('/node\/(\d+)\/layout/', $current_path, $matches)) {
           $elements = [
             '#theme' => 'background_style',
             '#css' => $css,
@@ -236,7 +261,8 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
               'contexts' => $cache_contexts,
             ],
           ];
-        } else {
+        }
+        else {
           // Use the selector in the id to avoid collisions with multiple
           // background formatters on the same page.
           $id = 'picture-background-formatter-' . $selector;
@@ -247,7 +273,8 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
               'tags' => $cache_tags,
               'contexts' => $cache_contexts,
             ],
-          ], $id];
+          ], $id,
+          ];
         }
       }
     }
@@ -318,7 +345,7 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
               $url = \Drupal::service('file_url_generator')->generateAbsoluteString($image->getFileUri());
             }
             else {
-              $url = ImageStyle::load($mapping['image_mapping'])->buildUrl($image->getFileUri());
+              $url = $this->imageFactory->load($mapping['image_mapping'])->buildUrl($image->getFileUri());
             }
 
             if ($multiplier != 1) {
diff --git a/src/Plugin/Field/FieldType/BgImgItem.php b/src/Plugin/Field/FieldType/BgImgItem.php
index b2732e9..96b66e3 100644
--- a/src/Plugin/Field/FieldType/BgImgItem.php
+++ b/src/Plugin/Field/FieldType/BgImgItem.php
@@ -6,7 +6,6 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\TypedData\DataDefinition;
 use Drupal\image\Plugin\Field\FieldType\ImageItem;
-use Robo\State\Data;
 
 /**
  * Plugin implementation of the 'bg_img_field' field type.
diff --git a/src/Plugin/Field/FieldWidget/BgImageFieldWidget.php b/src/Plugin/Field/FieldWidget/BgImageFieldWidget.php
index 7d2cdfa..b95b495 100644
--- a/src/Plugin/Field/FieldWidget/BgImageFieldWidget.php
+++ b/src/Plugin/Field/FieldWidget/BgImageFieldWidget.php
@@ -4,6 +4,7 @@ namespace Drupal\bg_img_field\Plugin\Field\FieldWidget;
 
 use Drupal\Core\Field\FieldItemListInterface;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
 use Drupal\image\Plugin\Field\FieldWidget\ImageWidget;
 
 /**
@@ -19,6 +20,8 @@ use Drupal\image\Plugin\Field\FieldWidget\ImageWidget;
  */
 class BgImageFieldWidget extends ImageWidget {
 
+  use StringTranslationTrait;
+
   /**
    * {@inheritdoc}
    */
@@ -42,9 +45,9 @@ class BgImageFieldWidget extends ImageWidget {
 
     $item = $items[$delta]->getFieldDefinition()->getSettings();
 
-    // set the form state with values of css_settings and hide_css_settings
+    // Set the form state with values of css_settings and hide_css_settings
     // to be used by the process method.
-    $form_state->set('css_values',$item['css_settings'] );
+    $form_state->set('css_values', $item['css_settings']);
     $form_state->set('hide_css_settings', $this->getSetting('hide_css_settings'));
 
     $elements['#upload_validators']['file_validate_extensions'][0] =
@@ -83,8 +86,10 @@ class BgImageFieldWidget extends ImageWidget {
         '#title' => t('CSS Settings'),
         '#description' => t('Set default CSS properties for the background image.'),
         '#open' => FALSE,
-        '#attributes' => $form_state->get('hide_css_settings') ? ['class' =>
-          ['visually-hidden']] : ['class' => ['visually-shown']],
+        '#attributes' => $form_state->get('hide_css_settings') ? [
+          'class' =>
+          ['visually-hidden'],
+        ] : ['class' => ['visually-shown']],
       ];
 
       $token_types = [];
@@ -176,13 +181,16 @@ class BgImageFieldWidget extends ImageWidget {
     return $elements;
   }
 
+  /**
+   * The settings form.
+   */
   public function settingsForm(array $form, FormStateInterface $form_state) {
     $element = parent::settingsForm($form, $form_state);
 
     $element['hide_css_settings'] = [
       '#type' => 'checkbox',
-      '#title' => t("Hide CSS Settings"),
-      '#description' => t("Do not show CSS settings when creating content 
+      '#title' => $this->t("Hide CSS Settings"),
+      '#description' => $this->t("Do not show CSS settings when creating content 
       on nodes, blocks, custom entities, media,  and paragraph items"),
       '#default_value' => $this->getSetting('hide_css_settings'),
     ];
diff --git a/src/Plugin/media/Source/BackgroundImageMedia.php b/src/Plugin/media/Source/BackgroundImageMedia.php
index cf6a952..90cda65 100644
--- a/src/Plugin/media/Source/BackgroundImageMedia.php
+++ b/src/Plugin/media/Source/BackgroundImageMedia.php
@@ -1,4 +1,5 @@
 <?php
+
 namespace Drupal\bg_img_field\Plugin\media\Source;
 
 use Drupal\media\Plugin\media\Source\Image;
@@ -19,4 +20,4 @@ use Drupal\media\Plugin\media\Source\Image;
  */
 class BackgroundImageMedia extends Image {
 
-}
\ No newline at end of file
+}
diff --git a/templates/background-style.html.twig b/templates/background-style.html.twig
index 2c28584..7b76b5f 100644
--- a/templates/background-style.html.twig
+++ b/templates/background-style.html.twig
@@ -1,3 +1,3 @@
 <style>
   {{ css }}
-</style>
\ No newline at end of file
+</style>
-- 
GitLab