Skip to content

Creating data table type extension

Data table type extensions are incredibly powerful: they allow you to modify any existing data table types across the entire system.

They have 2 main use-cases:

  1. You want to add a specific feature to a single data table type;
  2. You want to add a generic feature to several types;

Defining the data table type extension

First, create the data table type extension class extending from AbstractTypeExtension (you can implement DataTableTypeExtensionInterface instead if you prefer):

// src/DataTable/Extension/LoggedUserDataTableTypeExtension.php
namespace App\DataTable\Extension;

use App\DataTable\Type\ProductDataTableType;
use Kreyu\Bundle\DataTableBundle\Extension\AbstractDataTableTypeExtension;
use Kreyu\Bundle\DataTableBundle\Type\DataTableType;

class LoggedUserDataTableTypeExtension extends AbstractDataTableTypeExtension
{
    public static function getExtendedTypes(): iterable
    {
        // return [DataTableType::class] to modify (nearly) every data table in the system
        return [ProductDataTableType::class];
    }
}

The only method you must implement is getExtendedTypes(), which is used to configure which types you want to modify.

Depending on your use case, you may need to override some of the following methods:

  • buildDataTable()
  • buildView()
  • configureOptions()

Registering the extension as a service

Data table type extensions must be registered as services and tagged with the kreyu_data_table.type_extension tag. If you're using the default services.yaml configuration, this is already done for you, thanks to autoconfiguration.

Once the extension is registered, any method that you've overridden (e.g. buildDataTable()) will be called whenever any data table of the given type is built.