Commit 05c754c6 authored by git's avatar git Committed by Mark Quirvien Cristobal
Browse files

Issue #3298740 by Rakhi Soni: Drupal calls should be avoided in classes, use...

Issue #3298740 by Rakhi Soni: Drupal calls should be avoided in classes, use dependency injection instead
parent 7b80cdd1
Loading
Loading
Loading
Loading
+29 −1
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@ namespace Drupal\field_zip_file\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\field_zip_file\Plugin\Field\FieldType\ZipFileItem;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Formatter that shows the file size in a human readable way.
@@ -20,6 +22,32 @@ use Drupal\field_zip_file\Plugin\Field\FieldType\ZipFileItem;
 */
class ZipFileBase extends FormatterBase {

  /**
   * File url generator object.
   *
   * @var \Drupal\Core\File\FileUrlGeneratorInterface
   */
  protected $fileUrlGenerator;

  /**
   * Constructs new ZipFileBase object.
   *
   * @param \Drupal\Core\File\FileUrlGeneratorInterface $fileUrlGenerator
   *   File url generator object.
   */
  public function __construct(FileUrlGeneratorInterface $fileUrlGenerator) {
    $this->fileUrlGenerator = $fileUrlGenerator;
  }

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

  /**
   * {@inheritdoc}
   */
@@ -64,7 +92,7 @@ class ZipFileBase extends FormatterBase {
    $url = NULL;
    if ($file = $zip_file->get('entity')->getValue()) {
      if ($file_info = _field_zip_file($file)) {
        $url = \Drupal::service('file_url_generator')->generateAbsoluteString($file_info['folder_uri']);
        $url = $this->fileUrlGenerator->generateAbsoluteString($file_info['folder_uri']);
      }
    }
    if ($this->getSetting('default_index')) {