You are here

public function StatusYoutube::setNodeData in Heartbeat 8

1 call to StatusYoutube::setNodeData()
StatusYoutube::generateNode in modules/statusmessage/src/StatusYoutube.php

File

modules/statusmessage/src/StatusYoutube.php, line 52

Class

StatusYoutube

Namespace

Drupal\statusmessage

Code

public function setNodeData() {
  $provider_manager = \Drupal::service('video.provider_manager');
  $enabled_providers = $provider_manager
    ->loadDefinitionsFromOptionList(array(
    'youtube' => 'youtube',
  ));
  if ($provider_matches = $provider_manager
    ->loadApplicableDefinitionMatches($enabled_providers, $this->url)) {
    $definition = $provider_matches['definition'];
    $matches = $provider_matches['matches'];
    $uri = $definition['stream_wrapper'] . '://' . $matches['id'];
    $storage = \Drupal::entityManager()
      ->getStorage('file');
    $results = $storage
      ->getQuery()
      ->condition('uri', $uri)
      ->execute();
    if (!(count($results) > 0)) {
      $user = \Drupal::currentUser();
      $file = File::Create([
        'uri' => $uri,
        'filemime' => $definition['mimetype'],
        'filesize' => 1,
        'uid' => $user
          ->id(),
      ]);
      $file
        ->save();
      $fid = $file
        ->id();
    }
    else {
      $fid = array_values($results)[0];
    }
    $node = Node::create([
      'type' => 'youtube_video',
      'title' => $this->generator
        ->getTitle(),
      'status' => 1,
      'uid' => \Drupal::currentUser()
        ->id(),
      'field_video_embed' => $fid,
    ]);
    if ($this->message) {
      $node
        ->set('body', [
        'value' => '<div class="status-youtube"> ' . $this->message . ' </div>',
        'format' => 'full_html',
      ]);
    }
    return $node;
  }
  return null;
}