View source
<?php
namespace Drupal\media_entity_facebook\Plugin\media\Source;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceBase;
use Drupal\media\MediaSourceFieldConstraintsInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\media_entity_facebook\FacebookFetcher;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\media\MediaTypeInterface;
class Facebook extends MediaSourceBase implements MediaSourceFieldConstraintsInterface {
protected $facebookFetcher;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, FacebookFetcher $facebook_fetcher) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->facebookFetcher = $facebook_fetcher;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('config.factory'), $container
->get('media_entity_facebook.facebook_fetcher'));
}
public function getMetadataAttributes() {
return [
'author_name' => $this
->t('Author Name'),
'width' => $this
->t('Width'),
'height' => $this
->t('Height'),
'url' => $this
->t('URL'),
'html' => $this
->t('HTML'),
];
}
public function getMetadata(MediaInterface $media, $attribute_name) {
$content_url = $this
->getFacebookUrl($media);
if ($content_url === FALSE) {
return FALSE;
}
$data = $this->facebookFetcher
->getOembedData($content_url);
if ($data === FALSE) {
return FALSE;
}
switch ($attribute_name) {
case 'author_name':
return $data['author_name'];
case 'width':
return $data['width'];
case 'height':
return $data['height'];
case 'url':
return $this
->getFacebookUrl($media);
case 'html':
return $data['html'];
default:
return parent::getMetadata($media, $attribute_name);
}
}
protected function getFacebookUrl(MediaInterface $media) {
if (isset($this->configuration['source_field'])) {
$source_field = $this->configuration['source_field'];
if ($media
->hasField($source_field)) {
$property_name = $media->{$source_field}
->first()
->mainPropertyName();
$embed = $media->{$source_field}->{$property_name};
return static::parseFacebookEmbedField($embed);
}
}
return FALSE;
}
public static function parseFacebookEmbedField($data) {
$data = trim($data);
$content_url_regex = '/^https:\\/\\/(www\\.)?facebook\\.com\\//i';
if (preg_match($content_url_regex, $data)) {
return $data;
}
else {
$doc = new \DOMDocument();
if (@$doc
->loadHTML($data)) {
$iframes = $doc
->getElementsByTagName('iframe');
if ($iframes->length > 0 && $iframes
->item(0)
->hasAttribute('src')) {
$iframe_src = $iframes
->item(0)
->getAttribute('src');
$uri_parts = parse_url($iframe_src);
if ($uri_parts !== FALSE && isset($uri_parts['query'])) {
parse_str($uri_parts['query'], $query_params);
if (isset($query_params['href']) && preg_match($content_url_regex, $query_params['href'])) {
return $query_params['href'];
}
}
}
}
}
return FALSE;
}
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display
->setComponent($this
->getSourceFieldDefinition($type)
->getName(), [
'type' => 'facebook_embed',
'label' => 'visually_hidden',
]);
}
public function getSourceFieldConstraints() {
return [
'FacebookEmbedCode' => [],
];
}
}