Skip to content

Commit

Permalink
Merge pull request #148 from wrav/feature/gql-params
Browse files Browse the repository at this point in the history
GraphQL Upgrade
  • Loading branch information
reganlawton authored Feb 26, 2024
2 parents 2d8a521 + 5eab9cb commit a1df39b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 35 additions & 1 deletion src/fields/OembedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
];
}

Expand Down
9 changes: 9 additions & 0 deletions src/services/OembedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand All @@ -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 {
Expand Down

0 comments on commit a1df39b

Please sign in to comment.