Skip to content

Helper Methods

Javier Villanueva edited this page Feb 27, 2021 · 1 revision

The template block uses the MediaLounge\Storyblok\Block\Container\Element class which also extends Magento\Framework\View\Element\Template so we have access to all the Magento block methods that we are used to.

On top of this we expose a few helper methods to make working with Storyblok content a bit easier:

$block->renderWysiwyg(array $arrContent)

Arguments Description
$arrContent Content of "Richtext" field

When using "Richtext" fields this method will ensure that HTML elements are rendered.

Usage:

<?php /** @var MediaLounge\Storyblok\Block\Container\Element $block */ ?>

<div>
    <?= $block->renderWysiwyg($block->getDescription()) ?>
</div>

$block->transformImage(string $image, string $param = '')

Arguments Description
$image Image URL
$param Transformation parameters for Storyblok Image Service

Storyblok offers an Image Service that allows you to transform image's size, format, quality amongst other things. This method provides a convenient way of interacting with it so we are able to modify images on the fly.

Usage:

<?php /** @var MediaLounge\Storyblok\Block\Container\Element $block */ ?>

<div>
    <img width="1280" height="460" src="<?= $block->transformImage($block->getImage()['filename'], '1280x460/filters:quality(80):format(webp)') ?>" alt="<?= __('New Luma Collection') ?>">
</div>

Hint: This can be really powerful when combined with the srcset attribute or <picture> element!