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

GraphQL Upgrade #148

Merged
merged 4 commits into from
Feb 26, 2024
Merged
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
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