diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c73a50..7e78a20 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # oEmbed Changelog + +## 1.2.0 + +### Added +- Added Field TablePreview (by boscho87) #15 +- Added translations + ## 1.1.8 - 2019-09-16 ### Updated diff --git a/releases.json b/releases.json new file mode 100644 index 0000000..619408d --- /dev/null +++ b/releases.json @@ -0,0 +1,86 @@ +[ + { + "version": "1.1.3", + "downloadUrl": "https://github.com/wrav/oembed/archive/master.zip", + "date": "2019-03-29:00:00+10:30", + "notes": [ + "[Added] Added `valid` method to handle errors gracefully (Thanks @iparr).", + "[Added] Added data caching for parsed URLs to help increase page response time.", + "[Updated] Updated docs." + ] + }, + { + "version": "1.1.2", + "downloadUrl": "https://github.com/wrav/oembed/archive/master.zip", + "date": "2019-03-18:00:00+10:30", + "notes": [ + "[Updated] Version bump." + ] + }, + { + "version": "1.1.1", + "downloadUrl": "https://github.com/wrav/oembed/archive/master.zip", + "date": "2019-03-18:00:00+10:30", + "notes": [ + "[Updated] Fix bug in field type rendering." + ] + }, + { + "version": "1.1.0", + "downloadUrl": "https://github.com/wrav/oembed/archive/master.zip", + "date": "2019-03-13:00:00+10:30", + "notes": [ + "[Updated] Updated composer package `essence/essence` to `embed/embed`, to handle more edge case URLs." + ] + }, + { + "version": "1.0.5", + "downloadUrl": "https://github.com/wrav/oembed/archive/master.zip", + "date": "2019-02-08:00:00+10:30", + "notes": [ + "[Updated] Fix bug where field is in Matrix field and the AJAX event action isn't bound / fired until after entry initial saved" + ] + }, + { + "version": "1.0.4", + "downloadUrl": "https://github.com/wrav/oembed/archive/master.zip", + "date": "2019-01-17:00:00+10:30", + "notes": [ + "[Updated] Allowing support for CraftCMS v3.1" + ] + }, + { + "version": "1.0.3", + "downloadUrl": "https://github.com/wrav/oembed/archive/master.zip", + "date": "2018-12-07:00:00+10:30", + "notes": [ + "[Updated] Prevent JS asset rendering on frontend", + "[Updated] Revert javascript to use jQuery" + ] + }, + { + "version": "1.0.2", + "downloadUrl": "https://github.com/wrav/oembed/archive/master.zip", + "date": "2018-12-06:00:00+10:30", + "notes": [ + "[Updated] Refactored javascript to your native JS over jQuery" + ] + }, + { + "version": "1.0.1", + "downloadUrl": "https://github.com/wrav/oembed/archive/master.zip", + "date": "2018-11-26:00:00+10:30", + "notes": [ + "[Updated] Change preview controller action access to prevent anonymous access", + "[Updated] Refactored the preview action to use a template with wrapper to allow for future styling and updates" + ] + }, + { + "version": "1.0.0", + "downloadUrl": "https://github.com/wrav/oembed/archive/master.zip", + "date": "2018-11-18:00:00+10:30", + "notes": [ + "[Added] Initial release and migration from previous project" + ] + } +] diff --git a/src/Oembed.php b/src/Oembed.php index 9222708..81fe7c6 100755 --- a/src/Oembed.php +++ b/src/Oembed.php @@ -51,6 +51,27 @@ class Oembed extends Plugin // Public Methods // ========================================================================= + /** + * Oembed constructor. + * @param $id + * @param null $parent + * @param array $config + */ + public function __construct($id, $parent = null, array $config = []) + { + $i18n = Craft::$app->getI18n(); + if (!isset($i18n->translations[$id]) && !isset($i18n->translations[$id . '*'])) { + $i18n->translations[$id] = [ + 'class' => PhpMessageSource::class, + 'sourceLanguage' => 'en-US', + 'basePath' => '@wrav/oembed/translations', + 'forceTranslation' => true, + 'allowOverrides' => true, + ]; + } + parent::__construct($id, $parent, $config); + } + /** * Set our $plugin static property to this class so that it can be accessed via * Oembed::$plugin @@ -111,7 +132,7 @@ function (PluginEvent $event) { Event::on( View::class, View::EVENT_END_PAGE, - function(Event $event) { + function (Event $event) { if (Craft::$app->getRequest()->getIsCpRequest() && preg_match('/^\/.+\/entries\//', Craft::$app->getRequest()->getUrl())) { $url = Craft::$app->assetManager->getPublishedUrl('@wrav/oembed/assetbundles/oembed/dist/js/oembed.js', true); @@ -123,7 +144,7 @@ function(Event $event) { Event::on( UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, - function(RegisterUrlRulesEvent $event) { + function (RegisterUrlRulesEvent $event) { $event->rules['oembed/preview'] = 'oembed/default/preview'; } ); diff --git a/src/fields/OembedField.php b/src/fields/OembedField.php index ef36ad5..c4e0238 100755 --- a/src/fields/OembedField.php +++ b/src/fields/OembedField.php @@ -13,17 +13,18 @@ use Craft; use craft\base\ElementInterface; use craft\base\Field; +use craft\base\PreviewableFieldInterface; use yii\db\Schema; use wrav\oembed\models\OembedModel; /** * OembedField Field * - * @author reganlawton + * @author reganlawton | boscho87 * @package Oembed * @since 1.0.0 */ -class OembedField extends Field +class OembedField extends Field implements PreviewableFieldInterface { // Public Properties // ========================================================================= @@ -71,7 +72,7 @@ public function getContentColumnType(): string } /** - * @param mixed $value The raw field value + * @param mixed $value The raw field value * @param ElementInterface|null $element The element the field is associated with, if there is one * * @return mixed The prepared field value @@ -90,7 +91,7 @@ public function normalizeValue($value, ElementInterface $element = null) * Modifies an element query. * * @param ElementInterface $query The element query - * @param mixed $value The value that was set on this field’s corresponding [[ElementCriteriaModel]] + * @param mixed $value The value that was set on this field’s corresponding [[ElementCriteriaModel]] * param, if any. * @return null|false `false` in the event that the method is sure that no elements are going to be found. */ @@ -99,6 +100,26 @@ public function serializeValue($value, ElementInterface $element = null) return parent::serializeValue($value, $element); } + /** + * @param mixed $value + * @param ElementInterface $element + * @return string + */ + public function getTableAttributeHtml($value, ElementInterface $element): string + { + // https://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api + $youtubeUrl = (string)$value; + $query = parse_url($youtubeUrl, PHP_URL_QUERY) ?? ''; + parse_str($query, $params); + $id = $params['v'] ?? ''; + if ($id) { + $url = sprintf('https://img.youtube.com/vi/%s/default.jpg', $id); + //Todo possible improvement, (make size configurable) + return sprintf('', $url); + } + return ''; + } + /** * @return string|null */ @@ -109,32 +130,40 @@ public function getSettingsHtml() /** * @param ElementInterface|null $element The element the field is associated with, if there is one - * @param mixed $value The field’s value. This will either be the [[normalizeValue() normalized + * @param mixed $value The field’s value. This will either be the [[normalizeValue() normalized * value]], raw POST data (i.e. if there was a validation error), or null * @return string The input HTML. */ public function getInputHtml($value, ElementInterface $element = null): string { - $input = ''; - $preview = '
Preview
'; - + $input = ''; + $preview = sprintf('%s%s%s', '', Craft::t('oembed', 'Preview'), '
'); if ($value) { try { if ($embed = new OembedModel($value)) { $embed = $embed->embed(); - if (!empty($embed)) { - $preview .= ' '; + $preview .= ' '; } else { - $preview .= ' '; + $preview .= sprintf( + '%s%s%s', + ' ' + ); } } } catch (\Exception $exception) { - $preview .= ' '; + $preview .= sprintf( + '%s%s%s', + ' ' + ); } } else { $preview .= ''; } - return $input.$preview; + return $input . $preview; } } diff --git a/src/translations/de/oembed.php b/src/translations/de/oembed.php new file mode 100644 index 0000000..50111e8 --- /dev/null +++ b/src/translations/de/oembed.php @@ -0,0 +1,6 @@ + 'oEmbed plugin installiert', + 'Preview' => 'Vorschau', +];