You are here

public function HeartbeatUsernameBlock::build in Heartbeat 8

Throws

\Drupal\Core\Database\InvalidQueryException

Overrides BlockPluginInterface::build

File

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

Class

HeartbeatUsernameBlock
Provides a 'HeartbeatUsernameBlock' block.

Namespace

Drupal\heartbeat\Plugin\Block

Code

public function build() {
  $myConfig = \Drupal::service('config.factory')
    ->getEditable('heartbeat_feed.settings');
  $tagConfig = \Drupal::config('heartbeat_username.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
        ->createUsernameStreamForUidsByType($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('HeartbeatUsernameBlock')
        ->error('Could not load Heartbeats for Username');
      $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,
    ),
  ];
}