View source
<?php
namespace Drupal\oembed\Element;
use Bangpound\oEmbed\Consumer;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Element\RenderElement;
use Symfony\Component\DependencyInjection\ContainerInterface;
class oEmbed extends RenderElement implements ContainerFactoryPluginInterface {
protected $consumer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Consumer $consumer) {
$this->consumer = $consumer;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public function getInfo() {
$class = get_class($this);
return array(
'#theme' => 'oembed',
'#embed' => NULL,
'#parameters' => array(),
'#attributes' => array(),
'#pre_render' => array(
array(
$class,
'preRenderFetch',
),
array(
$class,
'preRenderRetheme',
),
),
);
}
public static function preRenderFetch($element) {
$embed = oembed_get_data($element['#url'], $element['#parameters']);
if (!$embed) {
$element['#printed'] = TRUE;
return $element;
}
$element['#embed'] = $embed;
return $element;
}
public static function preRenderRetheme($element) {
if (!empty($element['#printed'])) {
return $element;
}
$embed = $element['#embed'];
$element['#theme'] = 'oembed__' . $embed
->getType() . '__' . implode('__', explode(':', $embed
->getProviderName(), 2));
return $element;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$consumer = $container
->get('oembed.consumer');
return new static($configuration, $plugin_id, $plugin_definition, $consumer);
}
}