You are here

public function DisqusComment::import in Disqus 8

Import the row.

Derived classes must implement import(), to construct one new object (pre-populated) using ID mappings in the Migration.

Parameters

\Drupal\migrate\Row $row: The row object.

array $old_destination_id_values: (optional) The old destination IDs. Defaults to an empty array.

Return value

array|bool An indexed array of destination IDs in the same order as defined in the plugin's getIds() method if the plugin wants to save the IDs to the ID map, TRUE to indicate success without saving IDs to the ID map, or FALSE to indicate a failure.

Overrides MigrateDestinationInterface::import

File

src/Plugin/migrate/destination/DisqusComment.php, line 100

Class

DisqusComment
Disqus comment destination.

Namespace

Drupal\disqus\Plugin\migrate\destination

Code

public function import(Row $row, array $old_destination_id_values = []) {
  $identifier = $row
    ->getDestinationProperty('identifier');
  $disqus = disqus_api();
  if ($disqus) {
    try {
      $thread = $disqus->threads
        ->details([
        'forum' => $this->config
          ->get('disqus_domain'),
        'thread:ident' => $identifier,
        'thread' => '1',
      ]);
    } catch (\Exception $exception) {
      $this->logger
        ->error('Error loading thread details for entity : @identifier. Check your API keys.', [
        '@identifier' => $identifier,
      ]);
      $thread = NULL;
    }
    if (!isset($thread->id)) {
      try {
        $thread = $disqus->threads
          ->create([
          'forum' => $this->config
            ->get('disqus_domain'),
          'access_token' => $this->config
            ->get('advanced.disqus_useraccesstoken'),
          'title' => $row
            ->getDestinationProperty('title'),
          'identifier' => $identifier,
        ]);
      } catch (\Exception $exception) {
        $this->logger
          ->error('Error creating thread for entity : @identifier. Check your user access token.', [
          '@identifier' => $identifier,
        ]);
      }
    }
    try {
      $message = $row
        ->getDestinationProperty('message');
      $author_name = $row
        ->getDestinationProperty('author_name');
      $author_email = $row
        ->getDestinationProperty('author_email');
      $author_url = $row
        ->getDestinationProperty('author_url');
      $date = $row
        ->getDestinationProperty('date');
      $ip_address = $row
        ->getDestinationProperty('ip_address');
      $ids = FALSE;
      if (empty($author_name) || empty($author_email)) {

        // Post comment as created by site's moderator.
        $ids = [
          'disqus_id' => $disqus->posts
            ->create([
            'message' => $message,
            'thread' => $thread->id,
            'access_token' => $this->config
              ->get('advanced.disqus_useraccesstoken'),
            'date' => $date,
            'ip_address' => $ip_address,
          ])->id,
        ];
      }
      else {

        // Cannot create comment as anonymous user, needs 'api_key'
        // (api_key is not the public key).
        $ids = [
          'disqus_id' => $disqus->posts
            ->create([
            'thread' => $thread->id,
            'message' => $message,
            'author_name' => $author_name,
            'author_email' => $author_email,
            'author_url' => $author_url,
            'api_key' => $this->config
              ->get('advanced.disqus_publickey'),
          ]),
        ];
      }
      return $ids;
    } catch (\Exception $exception) {
      $this->logger
        ->error('Error creating post on thread @thread, error: @error', [
        '@thread' => $thread->id,
        '@error' => $exception
          ->getMessage(),
      ]);
    }
    return FALSE;
  }
}