You are here

class ArticleWrapper in Facebook Instant Articles 7.2

Wraps a FB Instant SDK article object with Drupal Base module configs and hooks for extensibility (without class inheritance, which the FB Instant SDK prevents by design).

Class ArticleWrapper @package Drupal\fb_instant_articles\ArticleWrapper

Hierarchy

Expanded class hierarchy of ArticleWrapper

File

src/ArticleWrapper.php, line 20
Contains \Drupal\fb_instant_articles\ArticleWrapper.

Namespace

Drupal\fb_instant_articles
View source
class ArticleWrapper {

  /**
   * A stateful FB Instant Articles SDK singleton for Drupal integration.
   *
   * @see getArticle()
   *
   * @var \Facebook\InstantArticles\Elements\InstantArticle
   */
  private $instantArticle;

  /**
   * ArticleWrapper constructor.
   *
   * Instantiates the InstantArticle object, adding Drupal Base module configs
   * where appropriate. Also invokes a hook to allow other modules to alter the
   * InstantArticle object before render or any other operation.
   *
   * @param array $context
   *   An associative array of contextual information altering the
   *   InstantArticle object.
   * @param int $language_direction
   *   Language direction currently in use, one of LANGUAGE_LTR or LANGUAGE_RTL.
   *
   * @see hook_fb_instant_articles_article_alter()
   *
   * Note modules making use of this wrapper must set the required Canonical URL
   * with:
   * @code
   * InstantArticle->withCanonicalUrl($url);
   * @endcode
   */
  private function __construct($context = array(), $language_direction = LANGUAGE_LTR) {
    $this->instantArticle = InstantArticle::create()
      ->addMetaProperty('op:generator:application', 'drupal/fb_instant_articles')
      ->addMetaProperty('op:generator:application:version', self::getApplicationVersion())
      ->withStyle(variable_get('fb_instant_articles_style', 'default'));
    if ($language_direction == LANGUAGE_RTL) {
      $this->instantArticle
        ->enableRTL();
    }
    drupal_alter('fb_instant_articles_article', $this->instantArticle, $context);
  }

  /**
   * Creates a Drupal wrapper for an InstantArticle object.
   *
   * @param array $context
   *   An associative array of contextual information altering the
   *   InstantArticle object.
   *
   * @return \Drupal\fb_instant_articles\ArticleWrapper
   */
  public static function create($context) {
    global $language;
    return new ArticleWrapper($context, $language->direction);
  }

  /**
   * Gets the wrapped InstantArticle object.
   *
   * @return \Facebook\InstantArticles\Elements\InstantArticle
   */
  public function getArticle() {
    return $this->instantArticle;
  }

  /**
   * Gets the Drupal module (or core compatibility) version number.
   *
   * @return string
   *   Either the module version defined by Drupal.org's packaging system, or
   *   (in the case of a direct git branch checkout) the Drupal core
   *   compatibility version (example: 7.x or 8.x).
   */
  private function getApplicationVersion() {
    $module = 'fb_instant_articles';
    $filename = drupal_get_path('module', $module) . '/' . $module . '.info';
    $info = drupal_parse_info_file($filename);
    return isset($info['version']) ? $info['version'] : DRUPAL_CORE_COMPATIBILITY;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ArticleWrapper::$instantArticle private property A stateful FB Instant Articles SDK singleton for Drupal integration.
ArticleWrapper::create public static function Creates a Drupal wrapper for an InstantArticle object.
ArticleWrapper::getApplicationVersion private function Gets the Drupal module (or core compatibility) version number.
ArticleWrapper::getArticle public function Gets the wrapped InstantArticle object.
ArticleWrapper::__construct private function ArticleWrapper constructor.