Enable Twig Components in Pimcore

I installed Symfony’s Twig Components in Pimcore to test if and how to do that.

A small warning ahead: This feature is still experimental, as their documentation titles.

To install the components in Pimcore simply install the components bundle via composer

composer require symfony/ux-twig-component

Now you need to tell Pimcore about this bundle in config/bundles.php

// config/bundles.php
return [
...
Symfony\UX\TwigComponent\TwigComponentBundle::class => ['all' => true],
];

It is installed and ready to be setup.
Depending on the specific setup these steps might vary, adjust accordingly

  1. Register the components in the services.yaml and make them public
  2. Create the components-class
  3. Create the components-template
  4. Use the template in your code e.g. the default-template
// services.yaml
AppBundle\Components\:
    resource: '../../Components'
    public: true
// src/Components/AlertComponent.php
namespace App\Components;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent('alert')]
class AlertComponent
{
    public string $type = 'success';
    public string $message;
}
{# templates/components/alert.html.twig #}
<div class="alert alert-{{ type }}">
    {{ message }}
</div>
{{ component('alert', { message: 'Hello Twig Components!' }) }}

Find all missing entries in two files

I have two csv-Files containing 30k customer information and should only return the customers missing in file1. Usecase: accidentially deleted some data (~1800) and need to import the deleted one, instead of importing 30k data and skip if they exist.

I did this via a small shell-script. The script can be downloaded and used here:

View Gist on Github

This is the magic

grep -v -f <(tr ',' '\n' < "${patterns}") "${search}"

-v negates the search

Pimcore: Create pages via PHP-Code (incl. editables)

I was given the task to create pages of existing data. The challenge: the pages contain editables and these editables should be filled by pre-existing data.

All editables must be created prior to creating pages with the content.

A editable consists of one or multiple elements. each element must be created individually and later added to the areablock which than is saved as a page.

The areablock is created with a name and later on all content gets connected via the name. In this example it is named content for the ease of use.

This code creates a page construct, sets editables and saves it to the database

<?php

$editablesOnPage = [];

$pageInformationYouWantToCreate = [
    'page_title' => 'My first page',
    'folder_path' => '/',
    'seo_description' => 'Pages seo description',
    'seo_title' => 'Seo Title, if needed',

];

$positionOnPageFor = [
    'headline' => 1,
    'wysiwyg' => 2,
];

$foldertoCreateThePageUnder = 1; // Root folder id
$areaBlockName = 'content';

$importPage = new \Pimcore\Model\Document\Page();
$importPage->setKey($pageInformationYouWantToCreate['page_title']);
$importPage->setPath($pageInformationYouWantToCreate['folder_path']);
$importPage->setType('page');
$importPage->setTitle($pageInformationYouWantToCreate['page_title']);
$importPage->setDescription($pageInformationYouWantToCreate['seo_description']);
$importPage->setMetaData([
    'seoTitle' => $pageInformationYouWantToCreate['seo_title'],
]);
$importPage->setParentId($foldertoCreateThePageUnder);
$importPage->setController('Default');

$blockArea = new \Pimcore\Model\Document\Editable\Areablock();
$blockArea->setName($areaBlockName);

// Headline
if ($pageInformationYouWantToCreate['headline_title']) {
    $headline = new \Pimcore\Model\Document\Editable\Input();
    $headline->setName(sprintf('%s:%s.title', $areaBlockName, $positionOnPageFor['headline']));
    $headline->setDataFromEditmode($pageInformationYouWantToCreate['headline_title']);
    $editablesOnPage[] = $headline;
}

// Wysiwyg
if ($pageInformationYouWantToCreate['content_text']) {
    $wysiwygComponent = new \Pimcore\Model\Document\Editable\Wysiwyg();
    $wysiwygComponent->setName(sprintf('%s:%s.text', $areaBlockName, $positionOnPageFor['wysiwyg']));
    $wysiwygComponent->setDataFromEditmode($pageInformationYouWantToCreate['content_text']);
    $editablesOnPage[] = $wysiwygComponent;
}

$blockArea->setDataFromEditmode([
    [
        'key'    => $pageInformationYouWantToCreate['headline'],
        'type'   => 'heading',
        'hidden' => false,
    ],
    [
        'key'    => $pageInformationYouWantToCreate['wysiwyg'],
        'type'   => 'wysiwyg',
        'hidden' => false,
    ]
]);

array_unshift($editablesOnPage, $blockArea);
$importPage->setEditables($editablesOnPage);

$importPage->save();