Explorar el Código

Adding ability to share in comments

logicp hace 7 años
padre
commit
ebd5a9393c
Se han modificado 3 ficheros con 64 adiciones y 4 borrados
  1. 5 0
      css/statusmessage.css
  2. 54 1
      src/Form/StatusForm.php
  3. 5 3
      src/MarkupGenerator.php

+ 5 - 0
css/statusmessage.css

@@ -67,3 +67,8 @@
 #edit-mediatabs + #ajax-wrapper {
   display: inline-block;
 }
+
+.heartbeat-message .comment-body a.status-comment-share {
+  text-decoration: none;
+  color: #333;
+}

+ 54 - 1
src/Form/StatusForm.php

@@ -197,7 +197,6 @@ class StatusForm extends FormBase {
     return null;
   }
 
-
   public function statusAjaxSubmit(array &$form, FormStateInterface $form_state) {
     $message = $form_state->getValue('message');
     $file = $form_state->getValue('media');
@@ -240,6 +239,8 @@ class StatusForm extends FormBase {
               'recipient' => $userViewed
             ]);
 
+            StatusHeartPost::parseHashtags($message);
+
             if ($type->getMedia() && $file !== null) {
               $statusEntity->set('field_image', array_values($file)[0]);
             }
@@ -310,5 +311,57 @@ class StatusForm extends FormBase {
     $form_state->setStorage([]);
   }
 
+
+
+  public static function parseHashtags($message) {
+
+    $tids = array();
+    $i = 0;
+    $tagsArray = explode('#', $message);
+
+    unset($tagsArray[0]);
+
+    $num = count($tagsArray);
+
+
+    foreach ($tagsArray as $hashtag) {
+      if ($i === $num - 1) {
+        $lastTagArray = explode(' ', $hashtag);
+        if (strlen($lastTagArray[1]) > 1) {
+          $hashtag = trim($lastTagArray[0]);
+        }
+      }
+      $tid = \Drupal::entityQuery("taxonomy_term")
+        ->condition("name", trim($hashtag))
+        ->condition('vid', [
+          'twitter',
+          'tags',
+          'kekistan'
+        ], 'IN')
+        ->execute();
+
+      if (count($tid) > 0) {
+        if (\Drupal::moduleHandler()->moduleExists('heartbeat')) {
+          \Drupal\heartbeat\Entity\Heartbeat::updateTermUsage(array_values($tid)[0], 'tags');
+        }
+        $tids[] = array_values($tid)[0];
+      } else {
+        $term = Term::create([
+          'name' => trim($hashtag),
+          'vid' => 'tags',
+          'field_count' => 1
+        ]);
+        if ($term->save()) {
+          $tids[] = $term->id();
+          if (\Drupal::moduleHandler()->moduleExists('heartbeat')) {
+            \Drupal\heartbeat\Entity\Heartbeat::newTermUsage($term->id());
+          }
+        }
+      }
+      $i++;
+    }
+    return $tids;
+  }
+
 }
 

+ 5 - 3
src/MarkupGenerator.php

@@ -12,7 +12,7 @@ use Drupal\statusmessage\TemplateCreator;
  */
 class MarkupGenerator implements Parser {
 
-  private $match;
+  public $match;
 
   public $parsedMarkup;
 
@@ -34,7 +34,8 @@ class MarkupGenerator implements Parser {
    * @return mixed
    */
   public function validateUrl($text) {
-    return preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $text, $this->match);
+    preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $text, $this->match);
+    return $this->match;
   }
 
   /**
@@ -42,7 +43,8 @@ class MarkupGenerator implements Parser {
    * @return mixed
    */
   public function parseMarkup($url) {
-    $url = strpos($url, 'http://') ? 'http://' . $url : $url;
+    $url = strpos($url, 'http') ? 'http://' . $url : $url;
+    $url = !is_array($url) ? $url : array_values($url)[0];
     $this->parsedMarkup = Embed::create($url);
     return true;
   }