-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputfieldOembed.module
executable file
·116 lines (91 loc) · 2.82 KB
/
InputfieldOembed.module
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* COPYRIGHT NOTICE
* Copyright (c) 2023 Neue Rituale GbR
* @author NR <code@neuerituale.com>
*/
namespace ProcessWire;
/**
* Collects input for InputfieldOembed fields
* @method string render()
* @method string oembedPreview($oembed): string
*/
class InputfieldOembed extends InputfieldURL {
public static function getModuleInfo(): array {
return [
'title' => 'Oembed',
'version' => 116,
'summary' => 'Inputfield for FieldtypeOembed.',
'icon' => 'globe',
'requires' => 'FieldtypeOembed',
];
}
public function __construct() {
parent::__construct();
$this->label = 'Oembed';
}
/**
* Render inputfield
* @return string
* @throws WireException
*/
public function ___render(): string {
$this->config->styles->add($this->config->urls->InputfieldOembed . "InputfieldOembed.css");
$fieldtype = $this->get('hasField');
if(
$fieldtype &&
$fieldtype->type instanceof FieldtypeOembed &&
!empty($this->get('value')) &&
$this->hasPage instanceof Page
) {
$preview = $this->oembedPreview($this->hasPage->getFormatted($fieldtype->name));
if($preview) {
$this->appendMarkup($preview);
}
elseif($fieldtype->hideOembedErrors ?? false) {
$this->appendMarkup("<p class='notes'>".$this->_('No preview available...')."</p>");
}
else {
$this->error(__('No oembed data or provider found.'));
}
}
return parent::___render();
}
/**
* Build preview for inputfield
* @param ?Oembed $oembed
* @return string
*/
public function ___oembedPreview(Oembed $oembed = null): string {
$out = '';
if($oembed instanceof Oembed && !$oembed->empty) {
$out .= '<div class="InputfieldOembedPreview">';
if($oembed->has('thumbnailUrl')) $out .= "<img src='$oembed->thumbnailUrl' width='150' />";
if($oembed->has('title')) $out .= "<h4>$oembed->title</h4>";
$out .= '<ul>';
if($oembed->has('providerName')) $out .= '<li>' . __('Provider: ') . $oembed->providerName . '</li>';
if($oembed->has('type')) $out .= '<li>' . __('Type: ') . $oembed->type . '</li>';
if($oembed->has('authorName')) $out .= '<li>' . __('Author: ') . $oembed->authorName . '</li>';
$out .= '</ul>';
$out .= '</div>';
}
return $out;
}
/**
* Configure Inputfield
* @return InputfieldWrapper
*/
public function ___getConfigInputfields(): InputfieldWrapper {
$inputfields = parent::___getConfigInputfields();
/** @var InputfieldCheckbox $inputfields */
$inputfields->add([
'type' => 'checkbox',
'name' => 'hideOembedErrors',
'label' => __('Oembed errors'),
'checkboxLabel' => __('Skip errors'),
'description' => __('Hide the error message in field rendering, if no oembed data or provider found. This is useful if you want to use the field for simple URLs as well.'),
'checked' => $this->hasField->hideOembedErrors
]);
return $inputfields;
}
}