You are here

function disqus_entity_delete in Disqus 8

Implements hook_entity_delete().

File

./disqus.module, line 97
The Disqus Drupal module.

Code

function disqus_entity_delete(EntityInterface $entity) {

  // Only act on content entities.
  if (!$entity instanceof ContentEntityInterface) {
    return;
  }
  $field = \Drupal::service('disqus.manager')
    ->getFields($entity
    ->getEntityTypeId());
  if (!$entity
    ->hasField(key($field))) {
    return;
  }
  $messenger = \Drupal::messenger();
  $config = \Drupal::config('disqus.settings');

  // Close/remove the thread on disqus if required.
  $action = $config
    ->get('advanced.api.disqus_api_delete');
  if ($action != DisqusCommentManagerInterface::DISQUS_API_NO_ACTION) {
    $disqus = disqus_api();
    if ($disqus) {
      try {

        // Load the thread data from disqus. Passing thread is required to allow
        // the thread:ident call to work correctly. There is a pull request to
        // fix this issue.
        $thread = $disqus->threads
          ->details([
          'forum' => $config
            ->get('disqus_domain'),
          'thread:ident' => "{$entity->getEntityTypeId()}/{$entity->id()}",
          'thread' => '1',
        ]);
      } catch (Exception $exception) {
        $messenger
          ->addError(t('There was an error loading the thread details from Disqus.'));
        \Drupal::logger('disqus')
          ->error('Error loading thread details for entity : @identifier. Check your API keys.', [
          '@identifier' => "{$entity->getEntityTypeId()}/{$entity->id()}",
        ]);
      }
      if (isset($thread->id)) {
        if ($action == DisqusCommentManagerInterface::DISQUS_API_CLOSE) {
          try {
            $disqus->threads
              ->close([
              'access_token' => $config
                ->get('advanced.disqus_useraccesstoken'),
              'thread' => $thread->id,
              'forum' => $config
                ->get('disqus_domain'),
            ]);
          } catch (Exception $exception) {
            $messenger
              ->addError(t('There was an error closing the thread on Disqus.'));
            \Drupal::logger('disqus')
              ->error('Error closing thread for entity : @identifier. Check your user access token.', [
              '@identifier' => "{$entity->getEntityTypeId()}/{$entity->id()}",
            ]);
          }
        }
        if ($action == DisqusCommentManagerInterface::DISQUS_API_REMOVE) {
          try {
            $disqus->threads
              ->remove([
              'access_token' => $config
                ->get('advanced.disqus_useraccesstoken'),
              'thread' => $thread->id,
              'forum' => $config
                ->get('disqus_domain'),
            ]);
          } catch (Exception $exception) {
            $messenger
              ->addError(t('There was an error removing the thread on Disqus.'));
            \Drupal::logger('disqus')
              ->error('Error closing thread for entity : @identifier. Check your user access token.', [
              '@identifier' => "{$entity->getEntityTypeId()}/{$entity->id()}",
            ]);
          }
        }
      }
    }
  }
}