Skip to content

Commit bc2f38a

Browse files
committed
Craft CMS 3 release WIP
1 parent b9d934e commit bc2f38a

File tree

4 files changed

+218
-4
lines changed

4 files changed

+218
-4
lines changed

src/fields/FetchField.php

+27
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use craft\helpers\Template;
2121

2222
use supercool\fetch\Fetch;
23+
use supercool\fetch\models\Fetch as FetchModel;
2324
use supercool\fetch\assetbundles\FetchAsset;
2425

2526
class FetchField extends Field
@@ -91,6 +92,21 @@ public function getContentColumnType(): string
9192
*/
9293
public function normalizeValue($value, ElementInterface $element = null)
9394
{
95+
if ( ! empty($value) )
96+
{
97+
$model = new FetchModel;
98+
$model->url = $value;
99+
100+
if ( $model->validate() )
101+
{
102+
return $model;
103+
}
104+
else
105+
{
106+
return $value;
107+
}
108+
}
109+
94110
return $value;
95111
}
96112

@@ -110,6 +126,17 @@ public function normalizeValue($value, ElementInterface $element = null)
110126
*/
111127
public function serializeValue($value, ElementInterface $element = null)
112128
{
129+
$value = trim($value);
130+
131+
if ( ! empty($value) )
132+
{
133+
// check if there is a protocol, add if not
134+
if ( parse_url($value, PHP_URL_SCHEME) === null )
135+
{
136+
$value = 'http://' . $value;
137+
}
138+
}
139+
113140
return parent::serializeValue($value, $element);
114141
}
115142

src/models/Fetch.php

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
/**
4+
* Fetch plugin for Craft CMS 3.x
5+
*
6+
* A field type to embed videos for Craft CMS
7+
*
8+
* @link http://www.supercooldesign.co.uk/
9+
* @copyright Copyright (c) 2018 Supercool Ltd
10+
*/
11+
12+
namespace supercool\fetch\models;
13+
14+
use craft\base\Model;
15+
use craft\helpers\Template as TemplateHelper;
16+
17+
use supercool\fetch\Fetch as FetchPlugin;
18+
use supercool\fetch\validators\FetchValidator;
19+
20+
class Fetch extends Model
21+
{
22+
23+
// Properties
24+
// =========================================================================
25+
26+
/**
27+
* @var
28+
*/
29+
public $url;
30+
31+
/**
32+
* @var
33+
*/
34+
private $_result;
35+
36+
37+
// Public Methods
38+
// =========================================================================
39+
40+
41+
/**
42+
* Use the plain url as the string representation.
43+
*
44+
* @return \Twig_Markup
45+
*/
46+
public function __toString()
47+
{
48+
return $this->url;
49+
}
50+
51+
52+
/**
53+
* Returns the embed code as a Twig_Markup
54+
*
55+
* @return \Twig_Markup
56+
*/
57+
public function getTwig()
58+
{
59+
return TemplateHelper::raw($this->_getHtml());
60+
}
61+
62+
63+
/**
64+
* Returns the embed code as a plain HTML
65+
*
66+
* @return string
67+
*/
68+
public function getHtml()
69+
{
70+
return $this->_getHtml();
71+
}
72+
73+
/**
74+
* Returns the whole json object
75+
*
76+
* @return string
77+
*/
78+
public function getObject()
79+
{
80+
return $this->_getObject();
81+
}
82+
83+
84+
/**
85+
* Returns the provider
86+
*
87+
* @return string
88+
*/
89+
public function getProvider()
90+
{
91+
return $this->_getProvider();
92+
}
93+
94+
95+
/**
96+
* @inheritDoc BaseModel::rules()
97+
*
98+
* @return array
99+
*/
100+
public function rules()
101+
{
102+
$rules = parent::rules();
103+
$rules[] = ['url', FetchValidator::class];
104+
return $rules;
105+
}
106+
107+
108+
// Private Methods
109+
// =========================================================================
110+
111+
private function _getHtml()
112+
{
113+
if (!isset($this->_result))
114+
{
115+
$this->_result = FetchPlugin::$plugin->getFetch()->get($this->url);
116+
}
117+
118+
return $this->_result['html'];
119+
}
120+
121+
122+
private function _getObject()
123+
{
124+
if (!isset($this->_result))
125+
{
126+
$this->_result = FetchPlugin::$plugin->getFetch()->get($this->url);
127+
}
128+
129+
return $this->_result['object'];
130+
}
131+
132+
133+
private function _getProvider()
134+
{
135+
if (!isset($this->_result))
136+
{
137+
$this->_result = FetchPlugin::$plugin->getFetch()->get($this->url);
138+
}
139+
140+
return $this->_result['provider'];
141+
}
142+
143+
}

src/services/Fetch.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function get($url, $scripts = true)
130130
{
131131
return [
132132
'success' => false,
133-
'error' => Craft::t('fetch', 'Sorry that service isn’t supported yet.')
133+
'error' => Craft::t("fetch", "Sorry that service isn’t supported yet.")
134134
];
135135
}
136136

@@ -163,7 +163,7 @@ public function get($url, $scripts = true)
163163
{
164164
return array(
165165
'success' => false,
166-
'error' => Craft::t("Sorry that image didn’t seem to work.")
166+
'error' => Craft::t("fetch", "Sorry that image didn’t seem to work.")
167167
);
168168
}
169169
}
@@ -177,7 +177,7 @@ public function get($url, $scripts = true)
177177
{
178178
return array(
179179
'success' => false,
180-
'error' => Craft::t("Sorry that url didn’t seem to work.")
180+
'error' => Craft::t("fetch", "Sorry that url didn’t seem to work.")
181181
);
182182
}
183183
}
@@ -236,7 +236,7 @@ public function get($url, $scripts = true)
236236
// Don’t cache ones that didn’t work
237237
return [
238238
'success' => false,
239-
'error' => Craft::t('fetch', 'Sorry content for that url couldn’t be found.')
239+
'error' => Craft::t("fetch", "Sorry content for that url couldn’t be found.")
240240
];
241241
}
242242
else

src/validators/FetchValidator.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* Fetch plugin for Craft CMS 3.x
5+
*
6+
* A field type to embed videos for Craft CMS
7+
*
8+
* @link http://www.supercooldesign.co.uk/
9+
* @copyright Copyright (c) 2018 Supercool Ltd
10+
*/
11+
12+
namespace supercool\fetch\validators;
13+
14+
use Craft;
15+
use yii\validators\Validator;
16+
17+
use supercool\fetch\Fetch;
18+
19+
class FetchValidator extends Validator
20+
{
21+
22+
/**
23+
* @inhertdoc
24+
*/
25+
public function validateAttribute($object, $attribute)
26+
{
27+
// get value
28+
$value = $object->$attribute;
29+
30+
if ( ! empty($value) )
31+
{
32+
// process it
33+
$result = Fetch::$plugin->getFetch()->get($value);
34+
35+
// if it didn’t work, spit back the error message
36+
if ( $result['success'] === false )
37+
{
38+
$message = $result['error'];
39+
$this->addError($object, $attribute, $message);
40+
}
41+
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)