You are here

class TransformerExtender in Facebook Instant Articles 7.2

Encapsulates Drupal-specific logic when using the Transformer class.

Class TransformerExtender @package Drupal\fb_instant_articles

Hierarchy

  • class \Drupal\fb_instant_articles\TransformerExtender extends \Facebook\InstantArticles\Transformer\Transformer

Expanded class hierarchy of TransformerExtender

1 file declares its use of TransformerExtender
EntityFieldMapper.php in modules/fb_instant_articles_display/src/EntityFieldMapper.php
Contains \Drupal\fb_instant_articles_display\EntityFieldMapper.

File

src/TransformerExtender.php, line 18
Contains \Drupal\fb_instant_articles\TransformerExtender.

Namespace

Drupal\fb_instant_articles
View source
class TransformerExtender extends Transformer {

  /**
   * Transformer constructor.
   *
   * Adds Drupal hook to allow transformer rules to be added by other modules.
   * We invoke the hook in a constructor - as opposed to inside transform() or a
   * new invokeRules() method - because of the way Transformer is normally used.
   *
   * @see hook_fb_instant_articles_transformer_rules()
   *
   * Note the parent class does not have a constructor, so we do not call
   * @code parent::__construct() @endcode.
   */
  public function __construct() {
    $rules = module_invoke_all('fb_instant_articles_transformer_rules');
    drupal_alter('fb_instant_articles_transformer_rules', $rules);
    $this
      ->addRules($rules);
  }

  /**
   * Adds rules from an array of rule information.
   *
   * @todo Propose adding this method upstream to the FB Instant SDK parent.
   *
   * @param array $rules
   *   An array of transformer rule arrays. This is a PHP array representation
   *   of the JSON list of Rules information expected by parent::loadRules().
   *
   * @see TransformerExtender::__construct()
   * @see \Facebook\InstantArticles\Transformer\Transformer::loadRules()
   */
  public function addRules(array $rules) {
    foreach ($rules as $rules) {
      $clazz = $rules['class'];
      try {
        $factory_method = new \ReflectionMethod($clazz, 'createFrom');
      } catch (\ReflectionException $e) {
        $factory_method = new \ReflectionMethod('Facebook\\InstantArticles\\Transformer\\Rules\\' . $clazz, 'createFrom');
      }
      $this
        ->addRule($factory_method
        ->invoke(null, $rules));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TransformerExtender::addRules public function Adds rules from an array of rule information.
TransformerExtender::__construct public function Transformer constructor.