From ae8dbbacd4e21400cdf2e29e2b6f45e86a386ebc Mon Sep 17 00:00:00 2001 From: reganlawton Date: Mon, 29 Jun 2020 21:40:00 +1000 Subject: [PATCH 1/3] GraphQL update for CraftCMS v3.5 --- src/fields/OembedField.php | 8 +++++- src/gql/OembedFieldTypeGenerator.php | 42 ++++++++++++++++++++++++++++ src/models/OembedModel.php | 3 ++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/fields/OembedField.php b/src/fields/OembedField.php index 02ead7e..8df0bdd 100755 --- a/src/fields/OembedField.php +++ b/src/fields/OembedField.php @@ -105,7 +105,13 @@ public function getContentGqlType() */ public function normalizeValue($value, ElementInterface $element = null) { - if (is_string($value) && $decValue = json_decode($value, true)) { + if (is_array($value)) { + if (isset($value['url'])) { + return new OembedModel($value['url']); + } + } + + if (is_string($value) && $decValue = json_decode($value, true)) { if (isset($decValue['url'])) { return new OembedModel($decValue['url']); } diff --git a/src/gql/OembedFieldTypeGenerator.php b/src/gql/OembedFieldTypeGenerator.php index 36fc4df..f8d949f 100644 --- a/src/gql/OembedFieldTypeGenerator.php +++ b/src/gql/OembedFieldTypeGenerator.php @@ -60,6 +60,48 @@ public static function generateTypes($context = null): array return [$property]; } + /** + * @inheritdoc + */ + public static function generateType($context = null) + { + /** @var OembedField $context */ + $typeName = self::getName($context); + + $properties = [ + 'title' => Type::string(), + 'description' => Type::string(), + 'url' => Type::string(), + 'type' => Type::string(), + 'image' => Type::string(), + 'imageWidth' => Type::string(), + 'imageHeight' => Type::string(), + 'code' => Type::string(), + 'width' => Type::string(), + 'height' => Type::string(), + 'aspectRatio' => Type::string(), + 'authorName' => Type::string(), + 'authorUrl' => Type::string(), + 'providerName' => Type::string(), + 'providerUrl' => Type::string(), + ]; + + $property = GqlEntityRegistry::getEntity($typeName) + ?: GqlEntityRegistry::createEntity($typeName, new OembedFieldResolver([ + 'name' => $typeName, + 'description' => 'This entity has all the Oembed Field properties', + 'fields' => function () use ($properties) { + return $properties; + }, + ])); + + TypeLoader::registerType($typeName, function () use ($property) { + return $property; + }); + + return $property; + } + /** * @inheritdoc */ diff --git a/src/models/OembedModel.php b/src/models/OembedModel.php index 0c417e2..15e2d7d 100755 --- a/src/models/OembedModel.php +++ b/src/models/OembedModel.php @@ -59,6 +59,9 @@ public function __get($name) return $this->$name ?? null; } +// var_dump($this->url); +// die; + if ($this->oembed === null) { $oembed = Oembed::getInstance()->oembedService->embed($this->url); From 229eb1981598dca98affd09c1a39fe96ba1c4122 Mon Sep 17 00:00:00 2001 From: reganlawton Date: Mon, 29 Jun 2020 22:10:05 +1000 Subject: [PATCH 2/3] Fix to normalizeValue function on GraphQL field's magic method. Thanks @joshuabaker --- CHANGELOG.md | 5 +++++ README.md | 4 ++-- composer.json | 2 +- src/fields/OembedField.php | 2 +- src/gql/OembedFieldResolver.php | 1 - src/models/OembedModel.php | 3 --- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38f1a8b..bdf8289 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # oEmbed Changelog +## 1.3.4 - 2020-06-29 + +### Updated +- Fix to normalizeValue function on GraphQL field's `__get()` magic method. Thanks @joshuabaker + ## 1.3.3 - 2020-06-29 ### Updated diff --git a/README.md b/README.md index 12cff14..a2363f5 100755 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ You can access additional media details using the examples below. Additional Embed information can be found [here](https://github.com/oscarotero/Embed) -## GraphQl +## GraphQL I recommend enabling caching in the plugin settings menu to speed up the API resolve timing. @@ -123,4 +123,4 @@ Changes can be viewed [here](https://github.com/wrav/oembed/blob/master/CHANGELO ## Support -Get in touch via email or by [creating a Github issue](/wrav/oembed/issues) +Get in touch via email, Discord, or by [creating a Github issue](/wrav/oembed/issues) diff --git a/composer.json b/composer.json index 816addc..722be57 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "wrav/oembed", "description": "A simple plugin to extract media information from websites, like youtube videos, twitter statuses or blog articles.", "type": "craft-plugin", - "version": "1.3.3", + "version": "1.3.4", "keywords": [ "craft", "cms", diff --git a/src/fields/OembedField.php b/src/fields/OembedField.php index 8df0bdd..5fc468f 100755 --- a/src/fields/OembedField.php +++ b/src/fields/OembedField.php @@ -110,7 +110,7 @@ public function normalizeValue($value, ElementInterface $element = null) return new OembedModel($value['url']); } } - + if (is_string($value) && $decValue = json_decode($value, true)) { if (isset($decValue['url'])) { return new OembedModel($decValue['url']); diff --git a/src/gql/OembedFieldResolver.php b/src/gql/OembedFieldResolver.php index 7089138..88bc8b8 100644 --- a/src/gql/OembedFieldResolver.php +++ b/src/gql/OembedFieldResolver.php @@ -15,7 +15,6 @@ class OembedFieldResolver extends ObjectType { - /** * @inheritdoc */ diff --git a/src/models/OembedModel.php b/src/models/OembedModel.php index 15e2d7d..0c417e2 100755 --- a/src/models/OembedModel.php +++ b/src/models/OembedModel.php @@ -59,9 +59,6 @@ public function __get($name) return $this->$name ?? null; } -// var_dump($this->url); -// die; - if ($this->oembed === null) { $oembed = Oembed::getInstance()->oembedService->embed($this->url); From e0b0ffefff22d6d33b27ce2eb41c2721adaa8aff Mon Sep 17 00:00:00 2001 From: reganlawton Date: Mon, 29 Jun 2020 22:13:33 +1000 Subject: [PATCH 3/3] Remove old code --- src/gql/OembedFieldTypeGenerator.php | 42 ---------------------------- 1 file changed, 42 deletions(-) diff --git a/src/gql/OembedFieldTypeGenerator.php b/src/gql/OembedFieldTypeGenerator.php index f8d949f..36fc4df 100644 --- a/src/gql/OembedFieldTypeGenerator.php +++ b/src/gql/OembedFieldTypeGenerator.php @@ -60,48 +60,6 @@ public static function generateTypes($context = null): array return [$property]; } - /** - * @inheritdoc - */ - public static function generateType($context = null) - { - /** @var OembedField $context */ - $typeName = self::getName($context); - - $properties = [ - 'title' => Type::string(), - 'description' => Type::string(), - 'url' => Type::string(), - 'type' => Type::string(), - 'image' => Type::string(), - 'imageWidth' => Type::string(), - 'imageHeight' => Type::string(), - 'code' => Type::string(), - 'width' => Type::string(), - 'height' => Type::string(), - 'aspectRatio' => Type::string(), - 'authorName' => Type::string(), - 'authorUrl' => Type::string(), - 'providerName' => Type::string(), - 'providerUrl' => Type::string(), - ]; - - $property = GqlEntityRegistry::getEntity($typeName) - ?: GqlEntityRegistry::createEntity($typeName, new OembedFieldResolver([ - 'name' => $typeName, - 'description' => 'This entity has all the Oembed Field properties', - 'fields' => function () use ($properties) { - return $properties; - }, - ])); - - TypeLoader::registerType($typeName, function () use ($property) { - return $property; - }); - - return $property; - } - /** * @inheritdoc */