Explorar el Código

Adding StatusYoutube class

logicp hace 7 años
padre
commit
614221716b
Se han modificado 2 ficheros con 84 adiciones y 1 borrados
  1. 8 1
      src/Form/StatusForm.php
  2. 76 0
      src/StatusYoutube.php

+ 8 - 1
src/Form/StatusForm.php

@@ -10,6 +10,7 @@ use Drupal\statusmessage\StatusService;
 use Drupal\statusmessage\StatusTypeService;
 use Drupal\statusmessage\Ajax\ClientCommand;
 use Drupal\statusmessage\StatusTwitter;
+use Drupal\statusmessage\StatusYoutube;
 use Drupal\Core\Ajax\AjaxResponse;
 use Drupal\heartbeat\Ajax\SelectFeedCommand;
 use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -214,13 +215,19 @@ $stophere = null;
   }
   public function statusAjaxSubmit(array &$form, FormStateInterface $form_state) {
     $message = $form_state->getValue('message');
+    preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $message, $match);
     if (strpos($message, 'twitter')) {
-      preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $message, $match);
       if ($this->previewGenerator !== null && !empty($match) && array_values($match)[0] !== null) {
         $url = is_array(array_values($match)[0]) ? array_values(array_values($match)[0])[0]: array_values($match)[0];
         $statusTwitter = new StatusTwitter($url);
         $nid = $statusTwitter->sendRequest();
       }
+    } else if (strpos($message, 'youtube')) {
+      if ($this->previewGenerator !== null && !empty($match) && array_values($match)[0] !== null) {
+        $url = is_array(array_values($match)[0]) ? array_values(array_values($match)[0])[0]: array_values($match)[0];
+        $statusYoutube = new StatusYoutube($url);
+        $nid = $statusYoutube->generateNode();
+      }
     }
 
     if (!empty($this->statusTypeService)) {

+ 76 - 0
src/StatusYoutube.php

@@ -0,0 +1,76 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: logicp
+ * Date: 6/14/17
+ * Time: 12:08 AM
+ */
+
+namespace Drupal\statusmessage;
+
+use Drupal\taxonomy\Entity\Term;
+use Drupal\node\Entity\Node;
+use Drupal\file\Entity\File;
+
+
+class StatusYoutube {
+
+  protected $parameter;
+
+
+  public function __construct($parameter) {
+    $this->parameter = $parameter;
+  }
+
+
+  private function parseUrl ($text) {
+    return explode('status/', $text)[1];
+  }
+
+  public function generateEmbed() {
+    return '<iframe class="heartbeat-youtube" width="auto" height="auto" src="' . $this->parameter . '" frameborder="0"></iframe>';
+  }
+
+  public function generateNode()
+  {
+
+    $provider_manager = \Drupal::service('video.provider_manager');
+    $enabled_providers = $provider_manager->loadDefinitionsFromOptionList(array('youtube' => 'youtube'));
+
+    if ($provider_matches = $provider_manager->loadApplicableDefinitionMatches($enabled_providers, $this->parameter)) {
+
+      $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' => 'dev_title',
+        'uid' => \Drupal::currentUser()->id(),
+        'field_video_embed' => $fid,
+      ]);
+
+      $node->save();
+
+      return $node->id();
+
+    }
+  }
+}
+