You are here

function heartbeat_entity_insert in Heartbeat 8

Implements hook_entity_insert().

File

./heartbeat.module, line 97
Contains heartbeat.module.

Code

function heartbeat_entity_insert(EntityInterface $entity) {
  $bundleSaved = false;
  switch (true) {
    case $entity instanceof \Drupal\node\Entity\Node:
      $user = $entity
        ->getOwner();
      $media = HeartBeat::mediaFieldTypes($entity
        ->getFields());
      if ($entity
        ->id() && $user
        ->isAuthenticated()) {
        $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
        $tokenService = \Drupal::service('token');
        foreach ($heartbeatTypeService
          ->getTypes() as $type) {
          $heartbeatTypeEntity = $heartbeatTypeService
            ->load($type);
          if ($heartbeatTypeEntity
            ->getMainEntity() == 'node') {
            $arguments = json_decode($heartbeatTypeEntity
              ->getArguments());
            foreach ($arguments as $key => $argument) {
              $variables[$key] = $argument;
            }
            $preparsedMessageString = strtr($heartbeatTypeEntity
              ->getMessage(), $variables);
            $entities = array(
              'node' => $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(),
              ]);

              //TODO Find better fix than this str_replace
              $heartbeatActivity
                ->setMessage(str_replace('&039;', "'", $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
                    ->getTitle(),
                  'status' => !$bundleSaved ? 1 : 0,
                ]);
                $heartbeatActivity
                  ->setMessage($heartbeatMessage);
                $heartbeatActivity
                  ->save();
              }
              else {
                continue;
              }
            }
          }
        }
        updateFeeds();
      }
      break;
    case $entity instanceof \Drupal\user\Entity\User:

      //      $userService = \Drupal\User\Entity\user
      break;
    case $entity instanceof \Drupal\flag\Entity\Flag:
      break;
    case $entity instanceof \Drupal\statusmessage\Entity\Status:
      $user = \Drupal\user\Entity\User::load($entity
        ->get('uid')
        ->getValue()[0]['target_id']);
      $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
      $tokenService = \Drupal::service('token');
      heartbeat_handle_entity($entity, $tokenService, $heartbeatTypeService, $user, 'status');
      break;
  }
}