-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathDisplay.php
More file actions
59 lines (52 loc) · 2.01 KB
/
Copy pathDisplay.php
File metadata and controls
59 lines (52 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace ModStart\Field;
use ModStart\Core\Dao\ModelUtil;
class Display extends AbstractField
{
protected $addable = false;
protected $editable = false;
public function content($content)
{
$this->hookRendering(function (AbstractField $field, $item, $index) use ($content) {
return AutoRenderedFieldValue::make($content);
});
$this->addable(true);
return $this;
}
public function asLink($linkTemplate = null, $openInBlank = true)
{
$this->hookRendering(function (AbstractField $field, $item, $index) use ($linkTemplate, $openInBlank) {
if (null !== $linkTemplate) {
if ($linkTemplate instanceof \Closure || strpos($linkTemplate, '::') !== false) {
$linkUrl = call_user_func($linkTemplate, $item);
} else {
$linkUrl = $linkTemplate;
if (preg_match_all('/\\{(.+?)\\}/', $linkUrl, $mat)) {
foreach ($mat[1] as $i => $k) {
$v = ModelUtil::traverse($item, $k);
$linkUrl = str_replace($mat[0][$i], $v, $linkUrl);
}
}
}
$linkTitle = ModelUtil::traverse($item, $field->column());
} else {
$linkUrl = ModelUtil::traverse($item, $field->column());
$linkTitle = $linkUrl;
}
if (!empty($linkTitle)) {
$html = [
'<a href="', $linkUrl, '" target="_blank" ref="noreferrer noopener" ',
($openInBlank ? 'target="_blank"' : ''),
'>',
($openInBlank ? '<i class="iconfont icon-link"></i> ' : ''),
htmlspecialchars($linkTitle),
'</a>',
];
} else {
$html = [];
}
return AutoRenderedFieldValue::make(join('', $html));
});
return $this;
}
}