From 9c88fb3cf9b6f3feef949d513b8f2f8627f87130 Mon Sep 17 00:00:00 2001 From: Jeroen Rothbauer <15321825+JeroenJRP@users.noreply.github.com> Date: Fri, 6 Dec 2024 15:52:05 +0100 Subject: [PATCH] Add functionality to parse CKEditor's oembed tags --- src/services/OembedService.php | 19 +++++++++++++++++++ src/variables/OembedVariable.php | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/services/OembedService.php b/src/services/OembedService.php index 98ab204..b9a8135 100755 --- a/src/services/OembedService.php +++ b/src/services/OembedService.php @@ -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>/i', function($matches) { + $url = $matches[1]; + return $this->render($url, $options, $cacheProps); + }, $input); + + return $output; + } + /** * @param $url * @param array $options diff --git a/src/variables/OembedVariable.php b/src/variables/OembedVariable.php index 012d262..9b04d83 100755 --- a/src/variables/OembedVariable.php +++ b/src/variables/OembedVariable.php @@ -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); + } }