You are here

fb_instant_articles_api_rules.module in Facebook Instant Articles 7.2

Provide Drupal rules to manage facebook instant article using API.

File

modules/fb_instant_articles_api_rules/fb_instant_articles_api_rules.module
View source
<?php

/**
 * @file
 * Provide Drupal rules to manage facebook instant article using API.
 */

/**
 * Implements hook_action_info().
 *
 * @todo - Convert this entire module to support entity instead of only Node.
 *
 */
function fb_instant_articles_api_rules_action_info() {
  return array(
    'facebook_instant_articles_api_import_action' => array(
      'type' => 'node',
      'label' => t('Add facebook instant article'),
      'configurable' => FALSE,
      'triggers' => array(
        'node_insert',
        'node_update',
      ),
    ),
    'facebook_instant_articles_api_remove_action' => array(
      'type' => 'node',
      'label' => t('Delete facebook instant article'),
      'configurable' => FALSE,
      'triggers' => array(
        'node_delete',
      ),
    ),
  );
}

/**
 * Implementation of a Facebook instant article node import action.
 *
 */
function facebook_instant_articles_api_import_action($node, $context = array()) {
  $fb_instant_enabled = fb_instant_articles_display_is_article_type('node', $node->type);
  if ($fb_instant_enabled) {
    fb_instant_articles_api_rules_import_article($node);
  }
}

/**
 * Implementation of a Facebook instant article node delete action.
 *
 */
function facebook_instant_articles_api_remove_action($node, $context = array()) {
  $fb_instant_enabled = fb_instant_articles_display_is_article_type('node', $node->type);
  if ($fb_instant_enabled) {
    fb_instant_articles_api_rules_remove_article($node);
  }
}

/**
 * Implementation of a Facebook instant article import.
 *
 * @param object $node
 */
function fb_instant_articles_api_rules_import_article($node) {
  if (isset($node->nid)) {

    // We duplicate this because hook_node_load() is not triggered during node_insert().
    $context = array(
      'entity_type' => 'node',
      'entity' => $node,
    );
    $node->fb_instant_article = \Drupal\fb_instant_articles\ArticleWrapper::create($context)
      ->getArticle();

    // Run node_view and other field formatter hooks.
    node_build_content($node, 'fb_instant_article');
    $client = \Drupal\fb_instant_articles_api\DrupalClient::get();
    $client
      ->importArticle($node->fb_instant_article, $node->status === NODE_PUBLISHED);
  }
}

/**
 * Implementation of a Facebook instant article remove.
 *
 * @param object $node
 */
function fb_instant_articles_api_rules_remove_article($node) {
  if (isset($node->nid)) {
    $url = array();

    // We try to depend on $node->path since entity_uri may not be able to
    // get the alias if the alias is deleted first.
    if (!empty($node->path['alias'])) {
      $url['path'] = $node->path['alias'];
    }
    else {
      $url = entity_uri('node', $node);
    }

    // @see \Drupal\fb_instant_articles_display\EntityPropertyMapper\addCanonicalURL()
    $canonical_override = variable_get('fb_instant_articles_canonical_url_override', '');
    if (!empty($canonical_override)) {
      $url['options']['base_url'] = $canonical_override;
    }
    $url['options']['absolute'] = TRUE;
    $canonical_url = url($url['path'], $url['options']);
    $client = \Drupal\fb_instant_articles_api\DrupalClient::get();
    $client
      ->removeArticle($canonical_url);
  }
}

Functions

Namesort descending Description
facebook_instant_articles_api_import_action Implementation of a Facebook instant article node import action.
facebook_instant_articles_api_remove_action Implementation of a Facebook instant article node delete action.
fb_instant_articles_api_rules_action_info Implements hook_action_info().
fb_instant_articles_api_rules_import_article Implementation of a Facebook instant article import.
fb_instant_articles_api_rules_remove_article Implementation of a Facebook instant article remove.