View source
<?php
namespace Drupal\media_entity_facebook\Plugin\MediaEntity\Type;
use Drupal\Core\Form\FormStateInterface;
use Drupal\media_entity\MediaInterface;
use Drupal\media_entity\MediaTypeBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Config\Config;
use Drupal\media_entity_facebook\FacebookFetcher;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Facebook extends MediaTypeBase {
protected $facebookFetcher;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, Config $config, FacebookFetcher $facebook_fetcher) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $config);
$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('config.factory')
->get('media_entity.settings'), $container
->get('media_entity_facebook.facebook_fetcher'));
}
public function defaultConfiguration() {
return [
'source_field' => '',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = [];
$options = [];
$bundle = $form_state
->getFormObject()
->getEntity();
$allowed_field_types = [
'string',
'string_long',
'link',
];
foreach ($this->entityFieldManager
->getFieldDefinitions('media', $bundle
->id()) as $field_name => $field) {
if (in_array($field
->getType(), $allowed_field_types) && !$field
->getFieldStorageDefinition()
->isBaseField()) {
$options[$field_name] = $field
->getLabel();
}
}
$form['source_field'] = [
'#type' => 'select',
'#title' => $this
->t('Field with source information'),
'#description' => $this
->t('Field on media entity that stores facebook embed code or URL. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'),
'#default_value' => empty($this->configuration['source_field']) ? NULL : $this->configuration['source_field'],
'#options' => $options,
];
return $form;
}
public function providedFields() {
return [
'author_name',
'width',
'height',
'url',
'html',
];
}
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 getField(MediaInterface $media, $name) {
$content_url = $this
->getFacebookUrl($media);
if ($content_url === FALSE) {
return FALSE;
}
$data = $this->facebookFetcher
->getOembedData($content_url);
if ($data === FALSE) {
return FALSE;
}
switch ($name) {
case 'author_name':
return $data['author_name'];
case 'width':
return $data['width'];
case 'height':
return $data['height'];
case 'url':
return $data['url'];
case 'html':
return $data['html'];
}
}
public function thumbnail(MediaInterface $media) {
return $this
->getDefaultThumbnail();
}
public function getDefaultThumbnail() {
return $this->config
->get('icon_base') . '/facebook.png';
}
public function attachConstraints(MediaInterface $media) {
parent::attachConstraints($media);
if (isset($this->configuration['source_field'])) {
$source_field_name = $this->configuration['source_field'];
if ($media
->hasField($source_field_name)) {
foreach ($media
->get($source_field_name) as &$embed_code) {
$typed_data = $embed_code
->getDataDefinition();
$typed_data
->addConstraint('FacebookEmbedCode');
}
}
}
}
}