You are here

class HelperExtension in Helper 8

Twig extension with some useful functions and filters.

Hierarchy

  • class \Drupal\helper\Twig\HelperExtension extends \Drupal\helper\Twig\Twig_Extension

Expanded class hierarchy of HelperExtension

1 string reference to 'HelperExtension'
helper.services.yml in ./helper.services.yml
helper.services.yml
1 service uses HelperExtension
twig.extension.helper in ./helper.services.yml
Drupal\helper\Twig\HelperExtension

File

src/Twig/HelperExtension.php, line 10

Namespace

Drupal\helper\Twig
View source
class HelperExtension extends \Twig_Extension {

  /**
   * The file helper service.
   *
   * @var \Drupal\helper\File
   */
  protected $fileHelper;

  /**
   * Constructs \Drupal\helper\Twig\HelperExtension.
   *
   * @param \Drupal\helper\File $file_helper
   *   The file helper.
   */
  public function __construct(File $file_helper) {
    $this->fileHelper = $file_helper;
  }

  /**
   * {@inheritdoc}
   */
  public function getFunctions() {
    return [
      new \Twig_SimpleFunction('file_data_uri', [
        $this,
        'fileDataUri',
      ]),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'drupal_helper';
  }

  /**
   * Converts a file URL into a data URI.
   *
   * @param string $uri
   *   The file URI.
   * @param bool $base_64_encode
   *   TRUE to return the data URI as base-64 encoded content.
   * @param string|null $mimetype
   *   The optional mime type to provide for the data URI. If not provided
   *   the mime type guesser service will be used.
   *
   * @return string
   *   The image data URI for use in a src attribute.
   */
  public function fileDataUri($uri, $base_64_encode = TRUE, $mimetype = NULL) {
    return $this->fileHelper
      ->getDataUri($uri, $base_64_encode, $mimetype);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HelperExtension::$fileHelper protected property The file helper service.
HelperExtension::fileDataUri public function Converts a file URL into a data URI.
HelperExtension::getFunctions public function
HelperExtension::getName public function
HelperExtension::__construct public function Constructs \Drupal\helper\Twig\HelperExtension.