You are here

public function DrupalClient::importEntity in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 src/DrupalClient.php \Drupal\fb_instant_articles\DrupalClient::importEntity()

Import a content entity into Instant Articles.

Runs the given entity through the serialization process and finally calls importArticle() to send it to Instant Articles.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: Entity to import into Instant Articles.

File

src/DrupalClient.php, line 115

Class

DrupalClient
Encapsulate Drupal-specific logic for FBIA Client.

Namespace

Drupal\fb_instant_articles

Code

public function importEntity(ContentEntityInterface $entity) {

  // Ensure that we have the services we need.
  if (!isset($this->serializer)) {
    throw new \LogicException($this
      ->t('Error importing entity to Facebook Instant Articles. Serializer is not defined.'));
  }

  /** @var \Facebook\InstantArticles\Elements\InstantArticle $article */
  $article = $this->serializer
    ->normalize($entity, 'fbia');

  // Default published status to TRUE for entities that don't implement
  // EntityPublishedInterface. For those that do implement
  // EntityPublishedInterface, go by the published status of the entity. Note
  // that the Development Mode setting under API settings will override this,
  // so ensure that it is turned off in production.
  $published = TRUE;
  if ($entity instanceof EntityPublishedInterface) {
    $published = $entity
      ->isPublished();
  }
  $this
    ->importArticle($article, $published);
  $this->logger
    ->info($this
    ->t('Imported %label (@entity_id) into Facebook Instant Articles.', [
    '%label' => $entity
      ->label(),
    '@entity_id' => $entity
      ->id(),
  ]));
}