Last updated 17 August 2017. Created on 12 January 2017.
Edited by rootwork, jackpelorus, Sam152. Log in to edit this page.

@todo, write some docs

In front of this message do not go crazy ..
There is a way to turn this into learning about regular expressions.

The module "video_embed_field" has the ability in the version for Drupal 8 to use Plugins to be able to add embedded video providers, perzonalido, from unknown providers or a proprietary streaming.

To start we assume that you know how to make a module and create the base structure for Drupal 8.

Once this is done, it is time to create our provider.

Create the following folder structure from the root of the module.


Src-> Plugin-> video_embed_field-> Provider

And there inside create the file "myProvider.php" (you can change the name you like, always respecting the naming convention of Drupal 8.).
In there we will be the base code of the provider and we will modify it according to our needs.

<?php
namespace Drupal\myprovider_embed_player\Plugin\video_embed_field\Provider;
use Drupal\video_embed_field\ProviderPluginBase;
/**
 * MyProvider provider plugin.
 *
 * @VideoEmbedProvider(
 *   id = "myprovider",
 *   title = @Translation("MyProvider")
 * )
 */
class myProviderPlayer extends ProviderPluginBase {

  /**
   * {@inheritdoc}
   */
  public function renderEmbedCode($width, $height, $autoplay) {
    $embed_code = [
      '#type' => 'video_embed_iframe',
      '#provider' => 'myprovider',
      '#url' => sprintf('//assets.ngeo.com/modules-video/latest/assets/ngsEmbeddedVideo.html'),
      '#query' => [
        'guid' => $this->getVideoId(),
        ],
      '#attributes' => [
        'width' => $width,
        'height' => $height,
        'frameborder' => '0',
        'allowfullscreen' => 'allowfullscreen',
      ],
    ];
    return $embed_code;
  }
 
  /**
   * {@inheritdoc}
   */
  public function getRemoteThumbnailUrl() {
    $url = 'http://img.natgeo.com/vi/%s/%s.jpg';
    $high_resolution = sprintf($url, $this->getVideoId(), 'maxresdefault');
    $backup = sprintf($url, $this->getVideoId(), 'mqdefault');
    try {
      $this->httpClient->head($high_resolution);
      return $high_resolution;
    }
    catch (\Exception $e) {
      return $backup;
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getIdFromInput($input) {
    preg_match('/(\/\/)(\w{0,20}.ngeo.com)(\/[[:alnum:]]*\/*.*\/[[:alnum:]]*.html)\?guid=(?<guid>[[:alnum:]]*.*)/', $input, $matches);

    return isset($matches['guid']) ? $matches['guid'] : FALSE;
  }

}

Then we should look for the function "getIdFromInput", where the magic of all this.
The module "Video Embed Field" needs to match the url with its internal validation system for that reason we must write a regular expression to replace the existing one in the example and fit our needs and devote us the necessary parameters, as requested by our type of embedment defined above in the creation of the supplier.

  /**
   * {@inheritdoc}
   */
  public static function getIdFromInput($input) {
    preg_match('/(\/\/)(\w{0,20}.ngeo.com)(\/[[:alnum:]]*\/*.*\/[[:alnum:]]*.html)\?guid=(?<guid>[[:alnum:]]*.*)/', $input, $matches);

    return isset($matches['guid']) ? $matches['guid'] : FALSE;
  }
To help with this I leave two tools:
www.regextester.com
regex101.com

regards
Jack

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.