Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

Add Vimeo support #22

Open
wants to merge 11 commits into
base: 8.x-1.x
Choose a base branch
from
2 changes: 2 additions & 0 deletions config/install/media_entity_embeddable_video.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ aol_sid: ''
wochit_progn: ''
youtube:
api_key: ''
vimeo:
access_token: ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline added.

7 changes: 7 additions & 0 deletions config/schema/media_entity_embeddable_video.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ media_entity_embeddable_video.settings:
api_key:
type: string
label: 'API key'
vimeo:
type: mapping
label: 'Vimeo'
mapping:
access_token:
type: string
label: 'Application API access token'

media_entity.bundle.type.embeddable_video:
type: mapping
Expand Down
75 changes: 75 additions & 0 deletions src/Plugin/MediaEntity/VideoProvider/Vimeo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* Contains \Drupal\media_entity_embeddable_video\Plugin\MediaEntity\VideoProvider\Vimeo.
*/

namespace Drupal\media_entity_embeddable_video\Plugin\MediaEntity\VideoProvider;

use Drupal\Core\Url;
use Drupal\media_entity_embeddable_video\VideoProviderBase;
use Drupal\media_entity_embeddable_video\VideoProviderInterface;
use GuzzleHttp\Exception\ClientException;

/**
* Provides embedding support for Vimeo videos.
*
* @EmbeddableVideoProvider(
* id = "vimeo",
* label = @Translation("Vimeo"),
* description = @Translation("Provides embedding support for Vimeo videos."),
* regular_expressions = {
* "@vimeo\.com/moogaloop\.swf\?clip_id=(?<id>[^""'\&]+)@i",
* "@vimeo\.com/[^""'\&\d]*(?<id>[^""'\&]+)@i"
* }
* )
*/
class Vimeo extends VideoProviderBase implements VideoProviderInterface {

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'width' => '640',
'height' => '480',
'autoplay' => FALSE,
];
}

/**
* {@inheritdoc}
*/
public function thumbnailURI() {
$headers = [];
if ($token = \Drupal::config('media_entity_embeddable_video.settings')->get('vimeo.access_token')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's inject this.

$headers['Authorization'] = 'bearer ' . $token;
}
$response = $this->httpClient->get(
Url::fromUri(
'https://api.vimeo.com/videos/' . $this->matches['id'] . '/pictures'
)->toString(),
[ 'headers' => $headers ]
);
if ($response->getStatusCode() == 200 && ($data = $response->getBody())) {
return json_decode($data)->data[0]->sizes[4]->link; // 960x720
}
return FALSE;
}

/**
* {@inheritdoc}
*/
public function render() {
return [
'#type' => 'html_tag',
'#tag' => 'iframe',
'#attributes' => [
'src' => '//player.vimeo.com/video/' . $this->matches['id'],
'width' => $this->configuration['width'],
'height' => $this->configuration['height']
],
];
}

}