You are here

public function HeartbeatMoreBlock::build in Heartbeat 8

Throws

\Drupal\Core\Database\InvalidQueryException

Overrides BlockPluginInterface::build

File

src/Plugin/Block/HeartbeatMoreBlock.php, line 114

Class

HeartbeatMoreBlock
Provides a 'HeartbeatBlock' block.

Namespace

Drupal\heartbeat\Plugin\Block

Code

public function build() {
  $feedConfig = \Drupal::config('heartbeat_feed.settings');
  $myConfig = \Drupal::config('heartbeat_more.settings');
  $friendData = \Drupal::config('heartbeat_friendship.settings')
    ->get('data');
  $hid = $myConfig
    ->get('hid');

  //TODO This naming convention should be changed to something more specific, like last Heartbeat ID
  $feed = $feedConfig
    ->get('message');
  $uids = null;
  $messages = array();
  $query = Database::getConnection()
    ->select('heartbeat_friendship', 'hf')
    ->fields('hf', [
    'uid',
    'uid_target',
  ]);
  $conditionOr = $query
    ->orConditionGroup()
    ->condition('hf.uid', \Drupal::currentUser()
    ->id())
    ->condition('hf.uid_target', \Drupal::currentUser()
    ->id());
  $results = $query
    ->condition($conditionOr)
    ->execute();
  if ($result = $results
    ->fetchAll()) {
    $uids = array();
    foreach ($result as $uid) {
      $uids[] = $uid->uid_target;
      $uids[] = $uid->uid;
    }
  }
  if ($feed !== null) {
    $uids = count($uids) > 1 ? array_unique($uids) : $uids;
    if (!empty($uids)) {
      foreach ($this->heartbeatStreamServices
        ->getOlderStreamForUidsByType($uids, $feed, $hid) as $heartbeat) {
        $this
          ->renderMessage($messages, $heartbeat);
      }
    }
    if (count($messages) > 0) {
      return [
        '#theme' => 'heartbeat_stream',
        '#messages' => $messages,
        '#attached' => array(
          'library' => 'heartbeat/heartbeat',
          'drupalSettings' => [
            'activeFeed' => 'jigga',
            'friendData' => $friendData,
          ],
        ),
        '#cache' => array(
          'max-age' => 0,
        ),
      ];
    }
  }
  return [
    '#type' => 'markup',
    '#markup' => 'End of feed',
  ];
}