You are here

public function DisqusComment::initializeIterator in Disqus 8

Initializes the iterator with the source data.

Return value

\Iterator Returns an iteratable object of data for this source.

Overrides SourcePluginBase::initializeIterator

File

src/Plugin/migrate/source/DisqusComment.php, line 145

Class

DisqusComment
Disqus comment source using disqus-api.

Namespace

Drupal\disqus\Plugin\migrate\source

Code

public function initializeIterator() {
  $items = [];
  if ($disqus = disqus_api()) {
    $items = [];
    try {
      $posts = $disqus->forums
        ->listPosts([
        'forum' => $this->config
          ->get('disqus_domain'),
      ]);
    } catch (\Exception $exception) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('There was an error loading the forum details. Please check you API keys and try again.'), MessengerInterface::TYPE_ERROR);
      $this->logger
        ->error('Error loading the Disqus PHP API. Check your forum name.', []);
      return new \ArrayIterator($items);
    }
    foreach ($posts as $post) {
      $id = $post->id;
      $items[$id]['id'] = $id;
      $items[$id]['pid'] = $post->parent;
      $thread = $disqus->threads
        ->details([
        'thread' => $post->thread,
      ]);
      $items[$id]['identifier'] = $thread->identifier;
      $items[$id]['name'] = $post->author->name;
      $items[$id]['email'] = $post->author->email;
      $items[$id]['user_id'] = $post->author->id;
      $items[$id]['url'] = $post->author->url;
      $items[$id]['ipAddress'] = $post->ipAddress;
      $items[$id]['isAnonymous'] = $post->author->isAnonymous;
      $items[$id]['createdAt'] = $post->createdAt;
      $items[$id]['comment'] = $post->message;
      $items[$id]['isEdited'] = $post->isEdited;
    }
  }
  return new \ArrayIterator($items);
}