/** * Gets the image preview (virtual field) * @return string|null * @uses MeTools\Utility\Youtube::getId() * @uses MeTools\Utility\Youtube::getPreview() */ protected function _getPreview() { if (empty($this->_properties['text'])) { return null; } //Checks for the first image in the text preg_match('#<\\s*img [^\\>]*src\\s*=\\s*(["\'])(.*?)\\1#im', $this->_properties['text'], $matches); if (!empty($matches[2])) { return Router::url($matches[2], true); } //Checks for a YouTube video and its preview preg_match('/\\[youtube](.+?)\\[\\/youtube]/', $this->_properties['text'], $matches); if (!empty($matches[1])) { return Youtube::getPreview(isUrl($matches[1]) ? Youtube::getId($matches[1]) : $matches[1]); } return null; }
/** * Parses Youtube code. * You can use video ID or video url. * * Examples: * <code> * [youtube]bL_CJKq9rIw[/youtube] * </code> * * <code> * [youtube]http://youtube.com/watch?v=bL_CJKq9rIw[/youtube] * </code> * @param string $text Text * @return string * @uses MeTools\Utility\Youtube::getId() * @uses MeTools\View\Helper\HtmlHelper::youtube() * @uses $pattern */ public function youtube($text) { return preg_replace_callback($this->pattern['youtube'], function ($matches) { if (isUrl($matches[1])) { return $this->Html->youtube(Youtube::getId($matches[1])); } return $this->Html->youtube($matches[1]); }, $text); }