You are here

protected function SvgImageFormatter::fileGetContents in Svg Image 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/SvgImageFormatter.php \Drupal\svg_image\Plugin\Field\FieldFormatter\SvgImageFormatter::fileGetContents()
  2. 1.x src/Plugin/Field/FieldFormatter/SvgImageFormatter.php \Drupal\svg_image\Plugin\Field\FieldFormatter\SvgImageFormatter::fileGetContents()

Provides content of the file.

Parameters

\Drupal\file\Entity\File $file: File to handle.

Return value

string File content.

1 call to SvgImageFormatter::fileGetContents()
SvgImageFormatter::viewElements in src/Plugin/Field/FieldFormatter/SvgImageFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/SvgImageFormatter.php, line 239

Class

SvgImageFormatter
Plugin implementation of the 'image' formatter.

Namespace

Drupal\svg_image\Plugin\Field\FieldFormatter

Code

protected function fileGetContents(File $file) {
  $fileUri = $file
    ->getFileUri();
  if (file_exists($fileUri)) {

    // Make sure that SVG is safe
    $rawSvg = file_get_contents($fileUri);
    $svgSanitizer = new Sanitizer();
    return $svgSanitizer
      ->sanitize($rawSvg);
  }
  $this->logger
    ->error('File @file_uri (ID: @file_id) does not exists in filesystem.', [
    '@file_id' => $file
      ->id(),
    '@file_uri' => $fileUri,
  ]);
  return FALSE;
}