Skip to content

Commit

Permalink
Merge pull request #167 from JeroenJRP/parse-tags
Browse files Browse the repository at this point in the history
Add functionality to parse CKEditor's oembed tags
  • Loading branch information
reganlawton authored Dec 9, 2024
2 parents 071fb5f + 9c88fb3 commit 2932f38
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/services/OembedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ public function render($url, array $options = [], array $cacheProps = [])
}
}

/**
* @param string $input
* @param array $options
* @return string
*/
public function parseTags(string $input, array $options = [], array $cacheProps = [])
{
if (empty($input)) {
return '';
}

$output = preg_replace_callback('/\<oembed\s(?:.*?)url="(.*?)"(?:.*?)>(?:.*?)<\/oembed>/i', function($matches) {
$url = $matches[1];
return $this->render($url, $options, $cacheProps);
}, $input);

return $output;
}

/**
* @param $url
* @param array $options
Expand Down
18 changes: 18 additions & 0 deletions src/variables/OembedVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,22 @@ public function valid($url, array $options = [], array $cacheProps = [])
$media = $this->embed($url, $options, $cacheProps);
return (!empty($media) && isset($media->code));
}

/**
* Call it like this:
*
* {{ craft.oembed.parseTags(input, options) }}
*
* @param $input
* @param array $options
* @return string
*/
public function parseTags($input, array $options = [], array $cacheProps = [])
{
if (empty($input)) {
return null;
}

return Oembed::getInstance()->oembedService->parseTags($input, $options, $cacheProps);
}
}

0 comments on commit 2932f38

Please sign in to comment.