Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added field preview #15 #22

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
86 changes: 86 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
25 changes: 23 additions & 2 deletions src/Oembed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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';
}
);
Expand Down
55 changes: 42 additions & 13 deletions src/fields/OembedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
// =========================================================================
Expand Down Expand Up @@ -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
Expand All @@ -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.
*/
Expand All @@ -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
reganlawton marked this conversation as resolved.
Show resolved Hide resolved
$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('<img src="%s" alt="" height="60px">', $url);
}
return '';
}

/**
* @return string|null
*/
Expand All @@ -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 = '<input name="'.$this->handle.'" class="text nicetext fullwidth oembed-field" value="'.$value.'" />';
$preview = '<p><strong>Preview</strong></p>';

$input = '<input name="' . $this->handle . '" class="text nicetext fullwidth oembed-field" value="' . $value . '" />';
$preview = sprintf('%s%s%s', '<p><strong>', Craft::t('oembed', 'Preview'), '</strong></p>');
if ($value) {
try {
if ($embed = new OembedModel($value)) {
$embed = $embed->embed();

if (!empty($embed)) {
$preview .= '<div class="oembed-preview">'.$embed->code.'</div>';
$preview .= '<div class="oembed-preview">' . $embed->code . '</div>';
} else {
$preview .= '<div class="oembed-preview"><p class="error">Please check your URL.</p></div>';
$preview .= sprintf(
'%s%s%s',
'<div class="oembed-preview"><p class="error">',
Craft::t('oembed', 'Please check your URL.'),
'</p></div>'
);
}
}
} catch (\Exception $exception) {
$preview .= '<div class="oembed-preview"><p class="error">Please check your URL.</p></div>';
$preview .= sprintf(
'%s%s%s',
'<div class="oembed-preview"><p class="error">',
Craft::t('oembed', 'Please check your URL.'),
'</p></div>'
);
}
} else {
$preview .= '<div class="oembed-preview"></div>';
}
return $input.$preview;
return $input . $preview;
}
}
6 changes: 6 additions & 0 deletions src/translations/de/oembed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'oEmbed plugin installed' => 'oEmbed plugin installiert',
'Preview' => 'Vorschau',
];