You are here

public function HeartbeatHashBlock::build in Heartbeat 8

Throws

\Drupal\Core\Database\InvalidQueryException

Overrides BlockPluginInterface::build

File

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

Class

HeartbeatHashBlock
Provides a 'HeartbeatBlock' block.

Namespace

Drupal\heartbeat\Plugin\Block

Code

public function build() {
  $myConfig = \Drupal::service('config.factory')
    ->getEditable('heartbeat_feed.settings');
  $tagConfig = \Drupal::config('heartbeat_hashtag.settings');
  $tid = $tagConfig
    ->get('tid');
  $friendData = \Drupal::config('heartbeat_friendship.settings')
    ->get('data');
  $feed = $myConfig
    ->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
        ->createHashStreamForUidsByType($uids, $feed, $tid) as $heartbeat) {
        $this
          ->renderMessage($messages, $heartbeat);
      }
    }
    else {
      foreach ($this->heartbeatStreamServices
        ->createStreamByType($feed) as $heartbeat) {
        $this
          ->renderMessage($messages, $heartbeat);
      }
    }
  }
  else {

    //        foreach ($this->heartbeatStreamServices->createStreamForUids($uids) as $heartbeat) {
    foreach ($this->heartbeatStreamServices
      ->loadAllStreams() as $heartbeat) {
      \Drupal::logger('HeartbeatHashBlock')
        ->error('Could not load Heartbeats for Hashtag');
      $this
        ->renderMessage($messages, $heartbeat);
    }
  }
  return [
    '#theme' => 'heartbeat_stream',
    '#messages' => $messages,
    '#attached' => array(
      'library' => 'heartbeat/heartbeat',
      'drupalSettings' => [
        'activeFeed' => 'jigga',
        'friendData' => $friendData,
      ],
    ),
    '#cache' => array(
      'max-age' => 0,
    ),
  ];
}