Final Class Yiisoft\Yii\DataView\GridView\Column\Base\Cell
| Inheritance | Yiisoft\Yii\DataView\GridView\Column\Base\Cell |
|---|
Cell represents a single grid cell with content and attributes.
Public Methods
Method Details
Creates a new cell instance.
| public mixed __construct ( array $attributes = [], boolean|null $encode = null, string|\Stringable $content ) | ||
| $attributes | array |
HTML attributes for the cell as name-value pairs. |
| $encode | boolean|null |
Whether to encode content. See {@see \Yiisoft\Yii\DataView\GridView\Column\Base\encode()} for details. |
| $content | string|\Stringable |
Cell content items. Multiple items can be provided and will be concatenated when rendered. |
public function __construct(
private array $attributes = [],
private ?bool $encode = null,
string|Stringable ...$content,
) {
$this->content = $content;
}
Add attributes to existing cell attributes.
This method merges new attributes with existing ones. If an attribute already exists, it will be replaced with the new value.
| public self addAttributes ( array $attributes ) | ||
| $attributes | array |
HTML attributes as name-value pairs. |
| return | self |
New instance with merged attributes. |
|---|---|---|
public function addAttributes(array $attributes): self
{
$new = clone $this;
$new->attributes = array_merge($new->attributes, $attributes);
return $new;
}
Add a CSS class to the cell.
This method safely adds a CSS class to the cell's class attribute, maintaining any existing classes.
| public self addClass ( string|string[]|null $class ) | ||
| $class | string|string[]|null |
The CSS class(es) to be added. Null values will be ignored. |
| return | self |
New instance with added CSS class. |
|---|---|---|
public function addClass(string|array|null $class): self
{
$new = clone $this;
Html::addCssClass($new->attributes, $class);
return $new;
}
Set a single attribute value.
This is a convenience method for setting a single attribute without affecting other existing attributes.
| public self attribute ( string $name, mixed $value ) | ||
| $name | string |
Attribute name. |
| $value | mixed |
Attribute value. |
| return | self |
New instance with updated attribute. |
|---|---|---|
public function attribute(string $name, mixed $value): self
{
$new = clone $this;
$new->attributes[$name] = $value;
return $new;
}
Replace all attributes with a new set.
Unlike {@see \Yiisoft\Yii\DataView\GridView\Column\Base\addAttributes()}, this method completely replaces all existing attributes with the new set.
| public self attributes ( array $attributes ) | ||
| $attributes | array |
HTML attributes as name-value pairs. |
| return | self |
New instance with replaced attributes. |
|---|---|---|
public function attributes(array $attributes): self
{
$new = clone $this;
$new->attributes = $attributes;
return $new;
}
Set the cell content.
Multiple content parts can be provided and will be concatenated when rendered. Each part can be either a string or an object implementing {@see \Stringable}.
| public self content ( string|\Stringable $parts ) | ||
| $parts | string|\Stringable |
Cell content parts. |
| return | self |
New instance with updated content. |
|---|---|---|
public function content(string|Stringable ...$parts): self
{
$new = clone $this;
$new->content = $parts;
return $new;
}
Set whether to double-encode HTML entities in content.
This is useful when the content might already contain encoded entities, and you need to control whether they should be encoded again.
| public self doubleEncode ( boolean $enabled ) | ||
| $enabled | boolean |
Whether to double encode HTML entities. Set to false if content already contains encoded entities. |
| return | self |
New instance with updated double encode setting. |
|---|---|---|
public function doubleEncode(bool $enabled): self
{
$new = clone $this;
$new->doubleEncode = $enabled;
return $new;
}
Set content encoding behavior.
This method controls how the cell content is encoded to prevent XSS attacks while allowing intentional HTML content when needed.
| public self encode ( boolean|null $enabled ) | ||
| $enabled | boolean|null |
Whether to encode tag content. Supported values:
|
| return | self |
New instance with updated encoding setting. |
|---|---|---|
public function encode(?bool $enabled): self
{
$new = clone $this;
$new->encode = $enabled;
return $new;
}
Get cell HTML attributes.
| public array getAttributes ( ) | ||
| return | array |
HTML attributes as name-value pairs. |
|---|---|---|
public function getAttributes(): array
{
return $this->attributes;
}
Get cell content parts.
| public array getContent ( ) | ||
| return | array |
Array of content parts. |
|---|---|---|
public function getContent(): array
{
return $this->content;
}
Check if cell content is empty.
A cell is considered empty if all its content parts are empty strings when converted to string representation.
| public boolean isEmptyContent ( ) | ||
| return | boolean |
Whether all content parts are empty strings. |
|---|---|---|
public function isEmptyContent(): bool
{
foreach ($this->content as $part) {
if ((string) $part !== '') {
return false;
}
}
return true;
}
Get double encode setting.
| public boolean shouldDoubleEncode ( ) | ||
| return | boolean |
Whether HTML entities in content should be double encoded. |
|---|---|---|
public function shouldDoubleEncode(): bool
{
return $this->doubleEncode;
}
Get content encoding setting.
| public boolean|null shouldEncode ( ) | ||
| return | boolean|null |
Current encoding setting:
|
|---|---|---|
public function shouldEncode(): ?bool
{
return $this->encode;
}
Signup or Login in order to comment.