Skip to content

Commit

Permalink
SilverStripe 5 compatibility (#16)
Browse files Browse the repository at this point in the history
* 🐛  (data mapping) uses up-to-date methods where available. Removes others

This is step one of making this module compatible with Embed 4.x which is required by Silverstripe Framework 4.11. Fields which are no longer available by default are now not retrieved. It may be possible to retrieve this data still with
additional changes.

* ⬆️  (composer) makes embed dependency explicit and locked to version 4

Without an explicit dependency defined dependency this module is subject to fail when paired with a mismatched version of Silverstripe

* 🎨  (whitespace) changes spaces to tabs

* ⬆️  (dependencies) locks package to Silverstripe > 4.11

* Fixes thumbnail URL getting other attributes to work

* Fixes getting other attributes to work

* ⬆️  (dependencies) allows install in Silverstripe 5

* 🐛  (data-conversion) replaces deprecated method

* 🔥  (code) removes commented out lines

---------

Co-authored-by: sinanghareb <[email protected]>
  • Loading branch information
davejtoews and sinan-evanshunt authored Mar 22, 2024
1 parent 499974a commit fea6f4e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"issues": "https://github.com/nathancox/silverstripe-embedfield/issues"
},
"require": {
"silverstripe/framework": "~4.0"
"embed/embed": "^4",
"silverstripe/framework": "^4.11|^5"
},
"extra": {
"screenshots": [
Expand Down
19 changes: 9 additions & 10 deletions src/Forms/EmbedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use SilverStripe\Forms\TextField;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Security\SecurityToken;
use SilverStripe\Core\Convert;
use SilverStripe\ORM\DataObjectInterface;
use nathancox\EmbedField\Model\EmbedObject;

Expand Down Expand Up @@ -75,20 +74,20 @@ public function Type() {
}

public function setValue($value, $data = null) {

if ($value instanceof EmbedObject) {
$this->object = $value;
parent::setValue($this->object->ID);

}
$this->object = EmbedObject::get()->byID($value);

parent::setValue($value);
}


public function saveInto(DataObjectInterface $record) {

$val = $this->Value(); // array[sourceurl],[data] (as json)

$name = $this->getName();
Expand Down Expand Up @@ -142,7 +141,7 @@ public function saveInto(DataObjectInterface $record) {
* This is called by the javascript
*/
public function update(HTTPRequest $request) {

if (!SecurityToken::inst()->checkRequest($request)) {
return '';
}
Expand Down Expand Up @@ -175,14 +174,14 @@ public function update(HTTPRequest $request) {
if ($object && $object->sourceExists()) {

if ($this->embedType && $this->embedType != $object->Type) {
return Convert::array2json(array(
return json_encode(array(
'status' => 'invalidurl',
'message' => '<a href="'.$sourceURL.'" target="_blank">' . $sourceURL . '</a> is not a valid source type.',
'data' => array()
));
}

return Convert::array2json(array(
return json_encode(array(
'status' => 'success',
'message' => '',
'data' => array(
Expand All @@ -194,15 +193,15 @@ public function update(HTTPRequest $request) {
));

} else {
return Convert::array2json(array(
return json_encode(array(
'status' => 'invalidurl',
'message' => '<a href="'.$sourceURL.'" target="_blank">' . $sourceURL . '</a> is not a valid embed source.',
'data' => array()
));
}
}else{

return Convert::array2json(array(
return json_encode(array(
'status' => 'nourl',
'message' => '',
'data' => array()
Expand Down
41 changes: 20 additions & 21 deletions src/Model/EmbedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,37 @@ function updateFromURL($sourceURL = null) {
if ($this->SourceURL) {
$sourceURL = $this->SourceURL;
}
$info = Embed::create($sourceURL);
//Oembed::get_oembed_from_url($sourceURL);
$embed = new Embed();
$info = $embed->get($sourceURL);

$this->updateFromObject($info);
}

function updateFromObject($info) {
if ($info && $info->getWidth()) {
$this->sourceExists = true;

$this->Title = $info->getTitle();
$this->Type = $info->type;
if ($info && $info->url) {
$this->sourceExists = true;

$this->Width = $info->getWidth();
$this->Height = $info->getHeight();
$this->Title = $info->title;

$this->ThumbnailURL = $info->getImage();
$this->ThumbnailWidth = $info->thumbnail_width;
$this->ThumbnailHeight = $info->thumbnail_height;
$this->Type = $info->getOEmbed()->get('type') ? (string) $info->getOEmbed()->get('type') : '';
$this->Width = $info->getOEmbed()->get('width') ? (string) $info->getOEmbed()->get('width') : '';
$this->Height = $info->getOEmbed()->get('height') ? (string) $info->getOEmbed()->get('height') : '';

$this->ProviderURL = $info->provider_url;
$this->ProviderName = $info->provider_name;
$this->ThumbnailURL = (string) $info->image;
$this->ThumbnailWidth = $info->getOEmbed()->get('thumbnail_width') ? (string) $info->getOEmbed()->get('thumbnail_width') : '';
$this->ThumbnailHeight = $info->getOEmbed()->get('thumbnail_height') ? (string) $info->getOEmbed()->get('thumbnail_height') : '';

$this->ProviderURL = (string) $info->providerUrl;
$this->ProviderName = $info->providerName;

$this->AuthorURL = $info->author_url;
$this->AuthorName = $info->author_name;
$this->AuthorURL = (string) $info->authorUrl;
$this->AuthorName = $info->authorName;

$embed = $info->getCode();
$this->EmbedHTML = $embed;
$this->URL = $info->url;
$this->Origin = $info->origin;
$this->WebPage = $info->web_page;
$embed = $info->code;
$this->EmbedHTML = (string) $embed;
$this->URL = (string) $info->url;
$this->Origin = (string) $info->providerUrl;
$this->WebPage = (string) $info->url;

} else {
$this->sourceExists = false;
Expand Down

0 comments on commit fea6f4e

Please sign in to comment.