Skip to content

Commit

Permalink
+ | Column - Listing - Link
Browse files Browse the repository at this point in the history
  • Loading branch information
agnonym authored and ifox committed Jan 9, 2024
1 parent 6d51bd7 commit 50ee137
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Services/Listings/Columns/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace A17\Twill\Services\Listings\Columns;

use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Services\Listings\TableColumn;
use Closure;

class Link extends TableColumn
{
protected ?Closure $url = null;

protected Closure|string|null $content = null;

protected bool $targetBlank = false;

public function url(Closure $urlFunction): static
{
$this->url = $urlFunction;
return $this;
}

public function content(Closure|string $contentFunction): static
{
$this->content = $contentFunction;
return $this;
}

public function shouldOpenInNewWindow(bool $shouldOpenInNewWindow = true): static
{
$this->targetBlank = $shouldOpenInNewWindow;
return $this;
}

protected function getRenderValue(TwillModelContract $model): string
{
$url = null;
if (($urlFunction = $this->url) !== null) {
$url = $urlFunction($model);
}

$content = null;
if (($contentFunction = $this->content) !== null) {
if (is_string($this->content)) {
$content = $this->content;
} else {
$content = $contentFunction($model);
}
}

if ($url === null) {
return $content;
}

return
'<a href="' . $url . '"' . ($this->targetBlank ? ' target="_blank"' : '') . '>'
. ($content ?? $url)
. '</a>';
}
}

0 comments on commit 50ee137

Please sign in to comment.