Skip to content

Commit e244add

Browse files
Add country flag table column decorator (#1958)
Co-authored-by: Michael Voříšek <[email protected]>
1 parent 6007748 commit e244add

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

demos/collection/grid.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727

2828
$grid->setModel($model);
2929

30+
// add country flag column
31+
$grid->addColumn('flag', [
32+
Table\Column\CountryFlag::class,
33+
'codeField' => $model->fieldName()->iso,
34+
'nameField' => $model->fieldName()->name,
35+
]);
36+
3037
// Adding Quicksearch on Name field using auto query.
3138
$grid->addQuickSearch([$model->fieldName()->name], true);
3239

src/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ public function renderRow(): void
482482
if (!is_array($columns)) {
483483
$columns = [$columns];
484484
}
485-
$field = !is_int($name) && $this->model->hasField($name) ? $this->model->getField($name) : null;
485+
$field = is_int($name) ? null : $this->model->getField($name);
486486
foreach ($columns as $column) {
487487
$html_tags = array_merge($column->getHtmlTags($this->model, $field), $html_tags);
488488
}

src/Table/Column/CountryFlag.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Atk4\Ui\Table\Column;
6+
7+
use Atk4\Data\Field;
8+
use Atk4\Data\Model;
9+
use Atk4\Ui\Table;
10+
11+
class CountryFlag extends Table\Column
12+
{
13+
/** Name of country code model field (in ISO 3166-1 alpha-2 format) */
14+
public string $codeField;
15+
16+
/** Optional name of model field which contains full country name. */
17+
public ?string $nameField = null;
18+
19+
public function getHtmlTags(Model $row, ?Field $field): array
20+
{
21+
$countryCode = $row->get($this->codeField);
22+
$countryName = $this->nameField ? $row->get($this->nameField) : null;
23+
24+
return [
25+
$field->shortName => $countryCode === null
26+
? ''
27+
: $this->getApp()->getTag('i', [
28+
'class' => strtolower($countryCode) . ' flag',
29+
'title' => strtoupper($countryCode) . ($countryName === null ? '' : ' - ' . $countryName),
30+
]),
31+
];
32+
}
33+
}

0 commit comments

Comments
 (0)