You are here

function fb_instant_articles_api_entity_save in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 modules/fb_instant_articles_api/fb_instant_articles_api.module \fb_instant_articles_api_entity_save()

Save the given entity to Instant Articles via the API.

When an entity is inserted or updated, import the content into Instant Articles via the API.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Entity to import into Instant Articles. In practice we only send content entities.

2 calls to fb_instant_articles_api_entity_save()
fb_instant_articles_api_entity_insert in modules/fb_instant_articles_api/fb_instant_articles_api.module
Implements hook_entity_insert().
fb_instant_articles_api_entity_update in modules/fb_instant_articles_api/fb_instant_articles_api.module
Implements hook_entity_update().

File

modules/fb_instant_articles_api/fb_instant_articles_api.module, line 39
Hook implementations.

Code

function fb_instant_articles_api_entity_save(EntityInterface $entity) {

  // We're only interested in content entities.
  if (!$entity instanceof ContentEntityInterface) {
    return;
  }

  // Only attempt import if this entity is of a bundle that is enabled for
  // instant articles.
  $display_id = $entity
    ->getEntityTypeId() . '.' . $entity
    ->bundle() . '.' . EntityViewDisplayEditForm::FBIA_VIEW_MODE;
  if (\Drupal::entityTypeManager()
    ->getStorage('entity_view_display')
    ->load($display_id)) {
    try {

      /** @var \Drupal\fb_instant_articles\DrupalClient $client */
      $client = \Drupal::service('fb_instant_articles.drupal_client');
      $client
        ->importEntity($entity);
      \Drupal::messenger()
        ->addStatus(t('%label published to Facebook Instant Articles.', [
        '%label' => $entity
          ->label(),
      ]));
    } catch (MissingApiCredentialsException $e) {
      \Drupal::messenger()
        ->addError(t('Error while trying to send entity to Facebook. API credentials haven\'t been configured. Visit the <a href="@api_config">Facebook Instant Articles API configuration page</a> to setup API access.', [
        '@api_config' => Url::fromRoute('fb_instant_articles.api_settings_form')
          ->toString(),
      ]));
    }
  }
}