Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services/ui_config/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ services:

Ibexa\Bundle\AdminUi\Templating\Twig\EmbeddedItemEditFormExtension: ~

Ibexa\Bundle\AdminUi\Templating\Twig\TableExtension: ~

Ibexa\AdminUi\UI\Config\Provider\UserContentTypes:
tags:
- { name: ibexa.admin_ui.config.provider, key: 'userContentTypes' }
Expand Down
60 changes: 60 additions & 0 deletions src/bundle/Templating/Twig/TableExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\AdminUi\Templating\Twig;

use Ibexa\AdminUi\Tab\Event\CollectCustomTableColumnsEvent;
use Ibexa\AdminUi\Tab\Event\CollectCustomTableHeadersEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class TableExtension extends AbstractExtension
{
private EventDispatcherInterface $eventDispatcher;

public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

public function getFunctions(): array
{
return [
new TwigFunction('ibexa_add_custom_table_headers', [$this, 'addCustomHeaders']),
new TwigFunction('ibexa_add_custom_table_columns', [$this, 'addCustomColumns']),
];
}

/**
* @param array<int, array<string, string|bool>> $existingHeaders
*
* @return array<int, array<string, string|bool>>
*/
public function addCustomHeaders(string $tableIdentifier, array $existingHeaders): array
{
$event = new CollectCustomTableHeadersEvent($tableIdentifier, $existingHeaders);
$this->eventDispatcher->dispatch($event);

return $event->getHeaders();
}

/**
* @param array<int, array<string, string|bool>> $existingColumns
* @param mixed $row
*
* @return array<int, array<string, string|bool>>
*/
public function addCustomColumns(string $tableIdentifier, array $existingColumns, $row): array
{
$event = new CollectCustomTableColumnsEvent($tableIdentifier, $existingColumns, $row);
$this->eventDispatcher->dispatch($event);

return $event->getColumns();
}
}
68 changes: 68 additions & 0 deletions src/lib/Tab/Event/CollectCustomTableColumnsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Tab\Event;

class CollectCustomTableColumnsEvent
{
private string $tableIdentifier;

/** @var array<int, array<string, string|bool>> */
private array $columns;

/** @var mixed */
private $row;

/**
* @param array<int, array<string, string|bool>> $columns
* @param mixed $row
*/
public function __construct(string $tableIdentifier, array $columns, $row)
{
$this->tableIdentifier = $tableIdentifier;
$this->columns = $columns;
$this->row = $row;
}

public function getTableIdentifier(): string
{
return $this->tableIdentifier;
}

/**
* @return mixed
*/
public function getRow()
{
return $this->row;
}

/**
* @return array<int, array<string, string|bool>>
*/
public function getColumns(): array
{
return $this->columns;
}

/**
* @param array<string, string|bool> $column
*/
public function addColumn(array $column): void
{
$this->columns[] = $column;
}

/**
* @param array<string, string|bool> $column
*/
public function addColumnAt(int $index, array $column): void
{
array_splice($this->columns, $index, 0, [$column]);
}
}
57 changes: 57 additions & 0 deletions src/lib/Tab/Event/CollectCustomTableHeadersEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Tab\Event;

use Symfony\Contracts\EventDispatcher\Event;

class CollectCustomTableHeadersEvent extends Event
{
private string $tableIdentifier;

/** @var array<int, array<string, string|bool>> */
private array $headers;

/**
* @param array<int, array<string, string|bool>> $headers
*/
public function __construct(string $tableIdentifier, array $headers)
{
$this->tableIdentifier = $tableIdentifier;
$this->headers = $headers;
}

public function getTableIdentifier(): string
{
return $this->tableIdentifier;
}

/**
* @return array<int, array<string, string|bool>>
*/
public function getHeaders(): array
{
return $this->headers;
}

/**
* @param array<string, string|bool> $column
*/
public function addHeader(array $column): void
{
$this->headers[] = $column;
}

/**
* @param array<string, string|bool> $header
*/
public function addHeaderAt(int $index, array $header): void
{
array_splice($this->headers, $index, 0, [$header]);
}
}
Loading