You are here

function heartbeat_handle_entity in Heartbeat 8

1 call to heartbeat_handle_entity()
heartbeat_entity_insert in ./heartbeat.module
Implements hook_entity_insert().

File

./heartbeat.module, line 195
Contains heartbeat.module.

Code

function heartbeat_handle_entity($entity, $tokenService, $heartbeatTypeService, $user, $mainEntity) {
  $bundleSaved = false;
  if ($entity
    ->id() && $user
    ->isAuthenticated()) {
    $media = HeartBeat::mediaFieldTypes($entity
      ->getFields());
    foreach ($heartbeatTypeService
      ->getTypes() as $type) {
      $heartbeatTypeEntity = $heartbeatTypeService
        ->load($type);
      if ($heartbeatTypeEntity
        ->getMainEntity() == $mainEntity) {
        $arguments = json_decode($heartbeatTypeEntity
          ->getArguments());
        foreach ($arguments as $key => $argument) {
          $variables[$key] = $argument;
        }
        $preparsedMessageString = strtr($heartbeatTypeEntity
          ->getMessage(), $variables);
        $entities = array(
          $mainEntity => $entity,
          'user' => $user,
        );
        if ($entity
          ->bundle() === $heartbeatTypeEntity
          ->getBundle() && array_key_exists($heartbeatTypeEntity
          ->getBundle(), $heartbeatTypeService
          ->getEntityBundles($entity
          ->getEntityType()))) {
          $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity
            ->getEntityTypeId(), $media);
          $heartbeatActivity = Heartbeat::create([
            'type' => $heartbeatTypeEntity
              ->id(),
            'uid' => $user
              ->id(),
            'nid' => $entity
              ->id(),
            'name' => $entity
              ->getTitle(),
          ]);
          $heartbeatActivity
            ->setMessage($heartbeatMessage);
          if ($heartbeatActivity
            ->save()) {
            $bundleSaved = true;
          }
          continue;
        }
        else {
          if ($heartbeatTypeEntity
            ->getBundle() === null || trim($heartbeatTypeEntity
            ->getBundle()) === '') {
            $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity
              ->getEntityTypeId(), $media);

            //      $translatedMessage = t($messageTemplate);
            $heartbeatActivity = Heartbeat::create([
              'type' => $heartbeatTypeEntity
                ->id(),
              'uid' => $user
                ->id(),
              'nid' => $entity
                ->id(),
              'name' => $entity
                ->getName(),
              'status' => !$bundleSaved ? 1 : 0,
            ]);
            $heartbeatActivity
              ->setMessage(str_replace('&039;', "'", $heartbeatMessage));
            $heartbeatActivity
              ->save();
          }
          else {
            continue;
          }
        }
      }
    }
  }

  //  updateFeeds();
}