Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various base ColumnType improvements and tests #150

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

Kreyu
Copy link
Owner

@Kreyu Kreyu commented Nov 24, 2024

What's Changed

  1. Added tests for the base ColumnType

  2. The default property path inherited from column name is now properly set to column config:

$builder->addColumn('firstName');

// Before:
$builder->getColumn('firstName')->getPropertyPath(); // null
$builder->getColumn('firstName')->getColumnConfig()->getPropertyPath(); // null

// After:
(string) $builder->getColumn('firstName')->getPropertyPath(); // string(9) "firstName"
(string) $dataTable->getColumn('firstName')->getConfig()->getPropertyPath(); // string(9) "firstName"

Caution

This may be breaking change if your application contains logic that depends on this value.

  1. The default sort property path inherited from column name is now properly set to column config:
$builder->addColumn('firstName', options: ['sort' => true]);

// Before:
$builder->getColumn('firstName')->getSortPropertyPath(); // null
$builder->getColumn('firstName')->getColumnConfig()->getSortPropertyPath(); // null

// After:
(string) $builder->getColumn('firstName')->getSortPropertyPath(); // string(9) "firstName"
(string) $dataTable->getColumn('firstName')->getConfig()->getSortPropertyPath(); // string(9) "firstName"

Caution

This may be breaking change if your application contains logic that depends on this value.

  1. The export value view now contains data variable, similar to regular value view, that represents value returned by property path or a getter option, not formatted by the formatter option:
// Assume this data tables displays User entities

use Kreyu\Bundle\DataTableBundle\ValueRowView;
use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView;

class User
{
    public function __construct(
        public string $firstName,
    ) {
    }
}

// This column displays User's first name and is exportable
$builder->addColumn('firstName', options: ['export' => true]);

// The row displays a User of name "John"
$rowView = new ValueRowView(data: new User(firstName: 'John'));

// Then export's ColumnValueView will be like so:
$view = $column->createExportValueView($rowView);
$view->vars['data']; // object(User)
$view->vars['value'] // string(4) "John"
  1. Export values of type Symfony\Contracts\Translation\TranslatableInterface are now automatically translated;
// Assume this data tables displays User entities

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Component\Translation\TranslatableMessage;

class User
{
    public function __construct(
        public TranslatableInterface $firstName,
    ) {
    }
}

// This column displays User's first name and is exportable
$builder->addColumn('firstName', options: ['export' => true]);

// The row displays a User of name "John"
$rowView = new ValueRowView(data: new User(firstName: new TranslatableMessage('John'));

// Then export's ColumnValueView will be like so:
$view = $column->createExportValueView($rowView);
$view->vars['data']; // object(TranslatableMessage)
$view->vars['value']; // string(4) "John"
  1. Export now inherits the value_translation_domain and value_translation_domain options:
// Export will use column value translation domain and parameters:
$builder->addColumn('firstName', options: [
    'value_translation_domain' => 'user',
    'value_translation_parameters' => ['%first_name%' => 'John'];
]);

// This however, can be overwritten for export:
$builder->addColumn('firstName', options: [
    'value_translation_domain' => 'user',
    'value_translation_parameters' => ['%first_name%' => 'John'];
    'export' => [
        'value_translation_domain' => 'export_user',
        'value_translation_parameters' => ['%first_name%' => 'Jane'];
    ]
]);

Warning

This may change the exported values, translating what previously would not be translated.

  1. The value_translation_parameters option now accepts callable value:
$builder->addColumn('firstName', options: [
    'value_translation_parameters' => function (string $firstName, User $user) {
        return [...];
    },
]);

The callable receives two arguments:

  • column value, e.g. column (row) data formatted by the optional formatter option;
  • column (row) data, e.g. value returned by property accessor or getter;

The ColumnValueView will contain the resolved callable.

  1. The column value view now contains two additional variables - itself as column and column name as name:
$builder->addColumn('firstName', options: ['export' => true]);

$view = $column->createValueView($rowView);
$view->vars['name']; // string(9) "firstName"
$view->vars['column']; // object(ColumnValueView)

@Kreyu Kreyu changed the title Various base ColumnType improvements and tests Various base ColumnType improvements and tests Nov 24, 2024
Copy link

cloudflare-workers-and-pages bot commented Nov 24, 2024

Deploying data-table-bundle with  Cloudflare Pages  Cloudflare Pages

Latest commit: c664cee
Status: ✅  Deploy successful!
Preview URL: https://4e1f67fc.data-table-bundle.pages.dev
Branch Preview URL: https://feature-column-type-tests-an.data-table-bundle.pages.dev

View logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant