You are here

public function TestController::getHeartbeats in Heartbeat 8

1 string reference to 'TestController::getHeartbeats'
heartbeat.routing.yml in ./heartbeat.routing.yml
heartbeat.routing.yml

File

src/Controller/TestController.php, line 112

Class

TestController
Class TestController.

Namespace

Drupal\heartbeat\Controller

Code

public function getHeartbeats() {
  $data = file_get_contents("public://heartbeats.dat");
  $heartbeats = unserialize($data);
  $errors = false;
  if (is_array($heartbeats)) {
    $heartbeats = array_reverse($heartbeats);
    foreach ($heartbeats as $heartbeat) {
      if ($heartbeat instanceof \Drupal\heartbeat\Entity\Heartbeat) {

        //          try {
        //            $heartbeat->save();
        //          } catch (\Exception $e) {
        //            $message = $e->getMessage();
        //          }
        //        }
        $nid = $heartbeat
          ->get('nid')
          ->getValue()[0]['target_id'];
        $title = 'Dev Test';
        $type = $type = $this->heartbeat_heartbeattype
          ->load($heartbeat
          ->get('type')
          ->getValue()[0]['target_id']);
        if ($type
          ->get('mainentity') == 'node') {
          $node = $this
            ->entityTypeManager()
            ->getStorage('node')
            ->load($nid);
          if ($node !== null) {
            $title = $node
              ->getTitle();
          }
        }
        $heartbeatActivity = Heartbeat::create([
          'uid' => $heartbeat
            ->getOwnerId(),
          'nid' => $heartbeat
            ->getNid()
            ->getValue()[0]['target_id'],
          'name' => $title,
          'type' => $heartbeat
            ->getType(),
          'message' => $heartbeat
            ->getMessage()
            ->getValue()[0]['value'],
        ]);
        if (!$heartbeatActivity
          ->save()) {
          $errors = true;
        }
      }
    }
  }
  $result = $errors ? 'Error restoring Heartbeats' : 'Heartbeats restored';
  return [
    '#type' => 'markup',
    '#markup' => $this
      ->t($result),
  ];
}