Final Class Yiisoft\Yii\DataView\GridView\Column\ActionColumnRenderer
| Inheritance | Yiisoft\Yii\DataView\GridView\Column\ActionColumnRenderer |
|---|---|
| Implements | Yiisoft\Yii\DataView\GridView\Column\ColumnRendererInterface |
ActionColumnRenderer renders action buttons in a grid column.
Public Methods
Method Details
| public mixed __construct ( callable|null $urlCreator = null, array|null $buttons = null, array|string|null $buttonClass = null, array $buttonAttributes = [], string|null $template = null, string|null $before = null, string|null $after = null ) | ||
| $urlCreator | callable|null |
Callback for generating button URLs. |
| $buttons | array|null |
Action buttons configuration. |
| $buttonClass | array|string|null |
CSS class(es) for buttons. |
| $buttonAttributes | array |
Default HTML attributes for buttons. |
| $template | string|null |
Template for rendering buttons. |
| $before | string|null |
Content to prepend to buttons. |
| $after | string|null |
Content to append to buttons. |
public function __construct(
?callable $urlCreator = null,
?array $buttons = null,
private readonly array|string|null $buttonClass = null,
private readonly array $buttonAttributes = [],
private readonly ?string $template = null,
private readonly ?string $before = null,
private readonly ?string $after = null,
) {
$this->urlCreator = $urlCreator ?? static fn(): string => '#';
$this->buttons = $buttons ?? [
'view' => new ActionButton('🔎', attributes: ['title' => 'View']),
'update' => new ActionButton('✎', attributes: ['title' => 'Update']),
'delete' => new ActionButton('❌', attributes: ['title' => 'Delete']),
];
}
public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell
{
/** @var ActionColumn $column This annotation needs for IDE only. */
$contentSource = $column->content;
if ($contentSource !== null) {
$content = (string) (is_callable($contentSource) ? $contentSource($context->data, $context) : $contentSource);
} else {
$buttons = $column->buttons ?? $this->buttons;
/**
* @var string $content We assume that we work with valid UTF-8 strings, so `preg_replace_callback()` never
* returns `null`.
*/
$content = preg_replace_callback(
'/{([\w\-\/]+)}/',
function (array $matches) use ($column, $buttons, $context): string {
$name = $matches[1];
if (
isset($buttons[$name])
&& $this->isVisibleButton(
$column,
$name,
$context->data,
$context->key,
$context->index,
)
) {
return $this->renderButton($buttons[$name], $name, $context);
}
return '';
},
$this->getTemplate($column, $buttons),
);
$content = trim($content);
}
$content = "\n"
. ($column->before ?? $this->before ?? '')
. $content
. ($column->after ?? $this->after ?? '')
. "\n";
return $cell
->addAttributes($column->bodyAttributes)
->content($content)
->encode(false);
}
public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
return $cell->addAttributes($column->columnAttributes);
}
public function renderHeader(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
return $cell
->content($column->header ?? $context->translate('Actions'))
->addAttributes($column->headerAttributes);
}
Signup or Login in order to comment.