Unverified Commit f7f6d837 authored by Stefan Auditor's avatar Stefan Auditor
Browse files

Issue #3291712 by sourabhjain, Rakhi Soni, sanduhrs, dipayanpramanik5: \Drupal...

Issue #3291712 by sourabhjain, Rakhi Soni, sanduhrs, dipayanpramanik5: \Drupal calls should be avoided in classes, use dependency injection instead
parent 49906f71
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
services:
  panolens.commands:
    class: \Drupal\panolens\Commands\PanolensCommands
    arguments: ['@file_system']
    tags:
      - { name: drush.command }
+34 −5
Original line number Diff line number Diff line
@@ -2,9 +2,11 @@

namespace Drupal\panolens\Commands;

use Drupal\Core\File\FileSystemInterface;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Drush\Exec\ExecTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Panolens drush commandfile.
@@ -21,6 +23,34 @@ class PanolensCommands extends DrushCommands {

  const LIBRARY_DESTINATION = 'libraries';

  /**
   * Filesystem.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Constructs a new PanolensCommands.
   *
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   File system.
   */
  public function __construct(FileSystemInterface $file_system) {
    parent::__construct();

    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('file_system')
    );
  }

  /**
   * Download and extract Panolens library.
   *
@@ -71,12 +101,12 @@ class PanolensCommands extends DrushCommands {

    // Download the archive.
    $filename = basename($url);
    if ($filepath = $this->downloadFile($url, FALSE, FALSE, \Drupal::service('file_system')->getTempDirectory() . '/' . $filename, TRUE)) {
    if ($filepath = $this->downloadFile($url, FALSE, FALSE, $this->fileSystem->getTempDirectory() . '/' . $filename, TRUE)) {
      $filename = basename($url);

      // Remove any existing plugin directory.
      if (is_dir($destination)) {
        \Drupal::service('file_system')->deleteRecursive($destination);
        $this->fileSystem->deleteRecursive($destination);
      }

      // Decompress the archive.
@@ -87,7 +117,7 @@ class PanolensCommands extends DrushCommands {
        $zip->extractTo('.');
        $zip->close();

        \Drupal::service('file_system')->move($index, $destination);
        $this->fileSystem->move($index, $destination);
        $this->logger()->notice('The library has been downloaded to ' . $destination);
      }
      else {
@@ -178,8 +208,7 @@ class PanolensCommands extends DrushCommands {
      }
    }
    if ($destination) {
      \Drupal::service('file_system')
        ->move($destination_tmp, $destination, $overwrite);
      $this->fileSystem->move($destination_tmp, $destination, $overwrite);
      return $destination;
    }
    return $destination_tmp;
+60 −3
Original line number Diff line number Diff line
@@ -3,8 +3,13 @@
namespace Drupal\panolens\Plugin\Field\FieldFormatter;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\image\ImageStyleStorageInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin implementation of the 'Image Panorama' formatter.
@@ -19,6 +24,60 @@ use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
 */
class ImagePanoramaFormatter extends ImageFormatter {

  /**
   * The file URL generator.
   *
   * @var \Drupal\Core\File\FileUrlGeneratorInterface
   */
  protected $fileUrlGenerator;

  /**
   * Constructs a ImagePanoramaFormatter object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param array $third_party_settings
   *   Any third party settings.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\image\ImageStyleStorageInterface $image_style_storage
   *   The image style entity storage handler.
   * @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator
   *   The file URL generator.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, ImageStyleStorageInterface $image_style_storage, FileUrlGeneratorInterface $file_url_generator) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage);
    $this->fileUrlGenerator = $file_url_generator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $plugin_id,
      $plugin_definition,
      $configuration['field_definition'],
      $configuration['settings'],
      $configuration['label'],
      $configuration['view_mode'],
      $configuration['third_party_settings'],
      $container->get('current_user'),
      $container->get('entity_type.manager')->getStorage('image_style'),
      $container->get('file_url_generator')
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -43,9 +102,7 @@ class ImagePanoramaFormatter extends ImageFormatter {
    foreach ($files as $delta => $file) {
      $cache_contexts = [];

      /** @var \Drupal\Core\File\FileUrlGenerator $file_url_generator */
      $file_url_generator = \Drupal::service('file_url_generator');
      $file_uri = $file_url_generator->generateString($file->getFileUri());
      $file_uri = $this->fileUrlGenerator->generateString($file->getFileUri());

      $cache_contexts[] = 'url.site';
      $cache_tags = Cache::mergeTags($base_cache_tags, $file->getCacheTags());
+52 −3
Original line number Diff line number Diff line
@@ -2,8 +2,11 @@

namespace Drupal\panolens\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\file\Plugin\Field\FieldFormatter\FileVideoFormatter;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin implementation of the 'Video Panorama' formatter.
@@ -19,6 +22,54 @@ use Drupal\file\Plugin\Field\FieldFormatter\FileVideoFormatter;
 */
class VideoPanoramaFormatter extends FileVideoFormatter {

  /**
   * The file URL generator.
   *
   * @var \Drupal\Core\File\FileUrlGeneratorInterface
   */
  protected $fileUrlGenerator;

  /**
   * Constructs a ImagePanoramaFormatter object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param array $third_party_settings
   *   Any third party settings.
   * @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator
   *   The file URL generator.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, FileUrlGeneratorInterface $file_url_generator) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
    $this->fileUrlGenerator = $file_url_generator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $plugin_id,
      $plugin_definition,
      $configuration['field_definition'],
      $configuration['settings'],
      $configuration['label'],
      $configuration['view_mode'],
      $configuration['third_party_settings'],
      $container->get('file_url_generator')
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -35,9 +86,7 @@ class VideoPanoramaFormatter extends FileVideoFormatter {
      $file = array_pop($files)['file'];
      $cache_contexts = [];

      /** @var \Drupal\Core\File\FileUrlGenerator $file_url_generator */
      $file_url_generator = \Drupal::service('file_url_generator');
      $file_uri = $file_url_generator->generateString($file->getFileUri());
      $file_uri = $this->fileUrlGenerator->generateString($file->getFileUri());

      $cache_contexts[] = 'url.site';
      $cache_tags = $file->getCacheTags();