Unknown uniqid function

I am trying to create a gallery-carousel areabricks and i have added all the steps the problem is that it does not recognized the uniqid function in this line {% set id = ‘gallery-carousel-’ ~ uniqid() %}
Below is the slider view:

{% set selectStore = [] %} {% for i in 2..29 %} {% set selectStore = selectStore|merge([i]) %} {% endfor %}
{% set slidesSelect = pimcore_select('slides', {
    store: selectStore,
    reload: true,
    width: 70
}) %}

{% set showPreviewsCheckbox = pimcore_checkbox('showPreviews') %}
{% set showPreviews = showPreviewsCheckbox.isChecked and not editmode %}

{%  set id = 'gallery-carousel-' ~  uniqid() %} 

{% set slides = slidesSelect.getData()|default(2) %}

{% if editmode %}

    <div class="alert alert-info" style="height: 75px">
        <div class="col-xs-6">
            How many images you want to show?
            {{ slidesSelect|raw }}
        </div>
        <div class="col-xs-6">
            Show Previews
            {{ showPreviewsCheckbox|raw }}
        </div>
    </div>

    <style type="text/css">
        .gallery .item {
            min-height: 200px;
        }
    </style>

{% endif %}

<div id="{{ id }}" class="gallery carousel slide">
    <!-- Indicators -->
    <ol class="carousel-indicators {{ showPreviews ? 'preview visible-lg' }}">

        {% for i in 0..(slides-1) %}

            <li data-target="#{{ id }}" data-slide-to="{{ i }}" class="{{ loop.first ? 'active' }}">
                {% if showPreviews %}
                    {{ pimcore_image('image_' ~ i, {
                        thumbnail: 'galleryCarouselPreview',
                    }) }}
                {% endif %}
            </li>

        {% endfor %}
    </ol>

    <div class="carousel-inner">
        {% for i in 0..(slides-1) %}
            <div class="item {{ id ~ '-' ~ i }} {{ loop.first ? 'active' }}">
                {{ pimcore_image('image_' ~ i, {
                    thumbnail: 'galleryCarousel',
                    dropClass: id ~ '-' ~ i,
                    defaultHeight: 200
                }) }}

                <div class="carousel-caption">
                    {% set captionTitle = pimcore_input('caption-title-' ~ i, { width: 400 }) %}
                    {% set captionText = pimcore_textarea('caption-text-' ~ i, { width: 400 }) %}

                    {% if editmode or not captionTitle.isEmpty() %}
                        <h3>{{ captionTitle|raw }}</h3>
                    {% endif %}

                    {% if editmode or not captionText.isEmpty() %}
                        <p>{{ captionText|raw }}</p>
                    {% endif %}
                </div>
            </div>
        {% endfor %}
    </div>

    <a class="left carousel-control" href="#{{ id }}" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left"></span>
    </a>
    <a class="right carousel-control" href="#{{ id }}" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right"></span>
    </a>
</div>
Should i install twig extensions and how??

Twig does not have the PHP uniqid() function enabled by default.

You can, however, create a Twig function which will act as the uniqid().

First, create a PHP class which will represent your function, like so:

<?php
namespace My\Namespace\Twig\Function;

use use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class MyTwigFunctions extends AbstractExtension
{
    public function getFunctions()
    {
        return [
            new TwigFunction('uniqid', [$this, 'uniqueId'])
        ];
    }

    // See https://www.php.net/manual/en/function.uniqid.php
    public function uniqueId($prefix = "", $moreEntropy = false)
    {
        return uniqid($prefix, $moreEntropy);
    }
}

Then, you need to register your newly created class in the services.yml of your bundle:

services:
    my.twig_functions:
        class: 'My\Namespace\Twig\Function\MyTwigFunctions'
        public: true
        tags:
            - { name: twig.extension }

After that, you can use it in your Twig:

{% set id = 'gallery-carousel-' ~ uniqid() %}

That’s if you need to create individual PHP functions. If you’d like access to all PHP functions in Twig, you could use this Twig extension: https://github.com/ravenflux/php-functions.

It basically adds a function and a filter for Twig into which you can pass the name of a PHP function, or a filter of course, and get a desired outcome.

So, the usage with that bundle would be:

{% set id = 'gallery-carousel-' ~ raven_function('uniqId') %}

Hope this helps.

1 Like

I did the above and now the error is gone, thank you but, I get the result as in the picure: