diff --git a/CHANGELOG.md b/CHANGELOG.md index 8710832..d1df280 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # oEmbed Changelog +## 3.0.8 - 2024-02-26 + +### Update +- Resolved GraphQL issues. Special thanks to @davidwebca + ## 3.0.7 - 2023-12-15 ### Update diff --git a/composer.json b/composer.json index c8a97ee..8588890 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": "3.0.7", + "version": "3.0.8", "keywords": [ "craft", "cms", diff --git a/src/fields/OembedField.php b/src/fields/OembedField.php index 1db6703..44fd7a9 100644 --- a/src/fields/OembedField.php +++ b/src/fields/OembedField.php @@ -93,10 +93,44 @@ public function getContentGqlType(): \GraphQL\Type\Definition\Type|array { $typeArray = OembedFieldTypeGenerator::generateTypes($this); + $handle = $this->handle; + return [ - 'name' => $this->handle, + 'name' => $handle, 'description' => "Oembed field", 'type' => array_shift($typeArray), + // The `args` array specifies the GraphQL arguments that the `embed` function accepts so we can apply options for the oEmbed service + 'args' => [ + 'options' => [ + 'name' => 'options', + 'type' => Type::string(), + 'description' => 'This should be a JSON-encoded string.', + ], + 'cacheProps' => [ + 'name' => 'cacheProps', + 'type' => Type::string(), + 'description' => 'This should be a JSON-encoded string.', + ], + ], + // Use the `resolve` method to convert the field value into a format that can be used by the oEmbed services embed method + 'resolve' => function($source, $arguments) use ($handle) { + try { + $url = $source[$handle]['url']; + } catch (\Exception $e) { + throw new \Exception('The oEmbed field is not set.'); + } + + if (isset($arguments['options'])) { + $arguments['options'] = Json::decode($arguments['options']); + } + if (isset($arguments['cacheProps'])) { + $arguments['cacheProps'] = Json::decode($arguments['cacheProps']); + } + + $embed = Oembed::getInstance()->oembedService->embed($url, $arguments['options'] ?? [], $arguments['cacheProps'] ?? []); + + return $embed; + } ]; } diff --git a/src/services/OembedService.php b/src/services/OembedService.php index addf2f3..9ac0a55 100755 --- a/src/services/OembedService.php +++ b/src/services/OembedService.php @@ -187,6 +187,11 @@ public function embed($url, array $options = [], array $cacheProps = []) if (!empty($options['attributes'])) { foreach ((array)$options['attributes'] as $key => $value) { $iframe->setAttribute($key, $value); + + // If key in array, add to the media object + if (in_array($key, ['width', 'height'])) { + $media->$key = $value; + } } } @@ -211,7 +216,11 @@ public function embed($url, array $options = [], array $cacheProps = []) // Set the code $code = $dom->saveXML($iframe, LIBXML_NOEMPTYTAG); + // Apply the code to the media object $media->code = $code; + + // Set the URL if not set + $media->url = $media->url ?: $url; } catch (\Exception $exception) { Craft::info($exception->getMessage(), 'oembed'); } finally {