Browse Source

Adding ability to share in comments

logicp 7 years ago
parent
commit
d6f5278820
4 changed files with 43 additions and 52 deletions
  1. 1 50
      heartbeat.module
  2. 0 1
      js/heartbeat.js
  3. 12 0
      src/Entity/Heartbeat.php
  4. 30 1
      src/Form/HeartbeatCommentForm.php

+ 1 - 50
heartbeat.module

@@ -257,56 +257,8 @@ function heartbeat_handle_entity($entity, $tokenService, $heartbeatTypeService,
 }
 
 function updateFeeds() {
-  $configFactory = \Drupal::service('config.factory');
-  $updateFeedConfig = $configFactory->getEditable('heartbeat_update_feed.settings');
-  $feedConfig = $configFactory->get('heartbeat_feed.settings');
-
-//  $form = \Drupal::formBuilder()->getForm('\Drupal\heartbeat\Form\HeartbeatUpdateFeedForm');
-
-//  $form_state = new FormState();
-//  $form_state->set('timestamp', $updateFeedConfig->get('timestamp'));
-
-//  \Drupal::formBuilder()->submitForm('\Drupal\heartbeat\Form\HeartbeatUpdateFeedForm', $form_state);
-
-//  $errors = $form_state->getErrors();
-
-
-  //  $updateFeedConfig->set('update', true)->save();
-//  $feedUpdateConfig = new stdClass();
-//  $feedUpdateConfig->feed = $feedConfig->get('message');
-//  $feedUpdateConfig->update = true;
-//  $feedUpdateConfig->timestamp = $updateFeedConfig->get('timestamp');
-
-
-//  $curlQuery = '/heartbeat/update_feed/' . $updateFeedConfig->get('timestamp');
-  //Set options for the curl request
-
-
-
-//  $curlOptions = array(
-//    'headers' => array(
-//      'X-DataSource-Auth' => 'a',
-//      'method' => 'POST',
-//    ),
-//  );
-//  //Perform the request and decode the results
-//  $result = chr_curl_http_request($curlQuery, $curlOptions);
-
+//  $configFactory = \Drupal::service('config.factory');
 }
-//public function saveHeartbeatMessage($entity, $tokenService, $preparsedMessageString, $entities, $entityTypeId, $media) {
-//  $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
-//
-//
-//  $heartbeatActivity = Heartbeat::create([
-//    'type' => $heartbeatTypeEntity->id(),
-//    'uid' => $user->id(),
-//    'nid' => $entity->id(),
-//    'name' => 'Dev Test',
-//  ]);
-//
-//  $heartbeatActivity->setMessage($heartbeatMessage);
-//  $heartbeatActivity->save();
-//}
 
 
 /**
@@ -330,7 +282,6 @@ function heartbeat_entity_view(array &$build, \Drupal\Core\Entity\EntityInterfac
  * Implements hook_cron().
  */
 function heartbeat_cron() {
-  $friendship = false;
   //Iterate over the Heartbeat Types and ensure that the weight of bundle-specific types are lower than that of their
   //parent type. This will allow us to ensure Bundle specific types end up being published as opposed to
   //Types which represent all content types

+ 0 - 1
js/heartbeat.js

@@ -127,7 +127,6 @@
   }
 
   function getParent(node) {
-    console.dir(node);
     if (node != null && node != undefined && node.classList != undefined && node.classList.contains('heartbeat-comment')) {
       let id = node.id.substr(node.id.indexOf('-') + 1);
       $.ajax({

+ 12 - 0
src/Entity/Heartbeat.php

@@ -500,6 +500,14 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
       case $entityType === 'status':
 
         $parsedMessage = $tokenService->replace($preparsedMessage . '<a class="status-post" href="/admin/structure/' . $entityType . '/[' . $entityType . ':id]">', $entities);
+
+        if (strpos($parsedMessage, '#')) {
+          self::parseHashtags($parsedMessage);
+        }
+        if (strpos($parsedMessage, '@')) {
+          self::parseUsernames($parsedMessage);
+        }
+
         /** @noinspection NestedTernaryOperatorInspection */
         $message = $parsedMessage;
         $message .= $mediaData ? self::buildMediaMarkup($mediaData) : 'Post';
@@ -642,6 +650,10 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
     $message = '';
     foreach ($tagsArray as $replacements) {
       $message .= $replacements;
+
+    }
+    if (!strpos($message, '#') || strpos($message, '#') !== 1) {
+      $message = '#' . $message;
     }
   }
 

+ 30 - 1
src/Form/HeartbeatCommentForm.php

@@ -7,6 +7,8 @@ use Drupal\Core\Form\FormStateInterface;
 use Drupal\Comment\Entity\Comment;
 use Drupal\Core\Ajax\AjaxResponse;
 use Drupal\Core\Ajax\AppendCommand;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Drupal\statusmessage\MarkupGenerator;
 
 /**
  * Class HeartbeatCommentForm.
@@ -16,6 +18,20 @@ use Drupal\Core\Ajax\AppendCommand;
  */
 class HeartbeatCommentForm extends FormBase {
   protected $entityId;
+  protected $markupGenerator;
+
+
+    /**
+     * {@inheritdoc}
+     */
+  public static function create(ContainerInterface $container)
+  {
+    return new static(
+//      $container->get('status_type_service'),
+//      $container->get('statusservice'),
+      $container->get('markupgenerator'));
+  }
+
 
   /**
    * {@inheritdoc}
@@ -24,6 +40,11 @@ class HeartbeatCommentForm extends FormBase {
     return 'heartbeat_comment_form';
   }
 
+  public function __construct() {
+    $this->markupGenerator = new MarkupGenerator();
+  }
+
+
   /**
    * {@inheritdoc}
    */
@@ -60,11 +81,19 @@ class HeartbeatCommentForm extends FormBase {
       $config = \Drupal::config('heartbeat_comment.settings');
 
       if (strlen(trim($commentBody)) > 1) {
+        $extraMarkup = null;
+        if (!empty($urls = $this->markupGenerator->validateUrl($commentBody))) {
+          $url = array_values($urls)[0];
+          $url = !is_array($url) ? $url : array_values($url)[0];
+          $this->markupGenerator->parseMarkup($url);
+          $extraMarkup = '<a href="' . $url . '" class="status-comment-share"> ' . $this->markupGenerator->generatePreview() . '</a>';
+        }
+
         $comment = Comment::create([
           'entity_type' => 'heartbeat',
           'entity_id' => $config->get('entity_id'),
           'field_name' => 'comment',
-          'comment_body' => $commentBody,
+          'comment_body' => ['value' => $commentBody . $extraMarkup, 'format' => 'basic_html'],
           'comment_type' => 'comment',
           'subject' => 'Heartbeat Comment',
           'uid' => \Drupal::currentUser()->id(),