You are here

fb_instant_articles.module in Facebook Instant Articles 8.2

File

fb_instant_articles.module
View source
<?php

/**
 * @file
 * Contains fb_instant_articles.module..
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;

/**
 * Implements hook_help().
 */
function fb_instant_articles_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the fb_instant_articles module.
    case 'help.page.fb_instant_articles':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Base module for Facebook Instant Articles.') . '</p>';
      return $output;
    case 'fb_instant_articles.base_settings_form':
      $page_id = \Drupal::config('fb_instant_articles.settings')
        ->get('page_id');
      $output = '';
      $output .= '<h2>' . t('Setup') . '</h2>';
      $output .= '<p>' . t("Once you've activated this Drupal module, set up your Instant Articles and submit them to Facebook for a one-time review. The review is required before you can begin publishing. Follow these steps to get started:") . '</p>';
      $output .= '<ol>';
      $output .= '  <li>' . t('<a href="@sign_up_url" target="_blank">Sign up</a> for Instant Articles, if you haven\'t already, and enabled the same Facebook Page you have selected.', [
        '@sign_up_url' => 'https://www.facebook.com/instant_articles/signup',
      ]) . '</li>';
      $output .= '  <li>' . t('Claim the URL you will use to publish articles.</b> Right now, we think the URL for this page is: %url.', [
        '%url' => $_SERVER['HTTP_HOST'],
      ]);
      if ($page_id != '') {
        $claim_url = 'https://www.facebook.com/' . $page_id . '/settings/?tab=instant_articles#URL';
        $output .= t('<a href="@claim_url" target="_blank">Claim your URL here.</a>', [
          '@claim_url' => $claim_url,
        ]);
      }
      $output .= '</li>';
      $output .= '  <li>' . t('Install the Pages Manager App to preview your articles and styles on <a href="@ios_url" target="_blank">iOS</a> or <a href="@android_url" target="_blank">Android</a>.', [
        '@ios_url' => 'http://itunes.apple.com/app/facebook-pages-manager/id514643583?ls=1&mt=8&ign-mscache=1',
        '@android_url' => 'https://play.google.com/store/apps/details?id=com.facebook.pages.app',
      ]) . '</li>';
      $output .= '  <li>' . t('Create a style template for your articles using the <a href="@style_url" target="_blank">Style Editor</a>. Be sure to provide the name of the template you want to use in the Module Configuration settings below.', [
        '@style_url' => 'https://developers.facebook.com/docs/instant-articles/guides/design#style',
      ]) . '</li>';
      $output .= '  <li>' . t('[Optional] Enable Audience Network, if you choose. Learn more about <a href="@audience_url" target="_blank">Audience Network</a> for Instant Articles and <a href="@sign_up_url" target="_blank">sign up here</a>.', [
        '@audience_url' => 'https://fbinstantarticles.files.wordpress.com/2016/03/audience-network_wp_instant-articles-2-2-web_self-serve.pdf',
        '@sign_up_url' => '',
      ]) . '</li>';
      $output .= '  <li>' . t('[Optional] Set up your ads and analytics, including Audience Network, in the Configuration area, below.') . '</li>';
      if ($page_id != '') {
        $article_review_url = 'https://www.facebook.com/' . $page_id . '/settings/?tab=instant_articles#Setup-Step2';
        $output .= '  <li>' . t('<a href="@article_review_url" target="_blank">Submit your articles</a> for review.', [
          '@article_review_url' => $article_review_url,
        ]) . '</li>';
      }
      else {
        $output .= '  <li>' . t('Submit your articles for review.') . '</li>';
      }
      $output .= '</ol>';
      $output .= '<p>' . t('Other Resources:') . '</p>';
      $output .= '<ol>';
      $output .= '  <li>' . t('Read the <a href="@docs_url" target="_blank">documentation</a> to answer additional questions you might have about Instant Articles.', [
        '@docs_url' => 'https://developers.facebook.com/docs/instant-articles',
      ]) . '</li>';
      $output .= '  <li>' . t('Check out the <a href="@blog_url" target="_blank">Instant Articles blog</a> and sign up to receive notifications of important updates.', [
        '@blog_url' => 'https://developers.facebook.com/ia/blog/',
      ]) . '</li>';
      if ($page_id != '') {
        $tools_url = 'https://www.facebook.com/' . $page_id . '/settings/?tab=instant_articles';
        $roles_url = 'https://www.facebook.com/' . $page_id . '/settings/?tab=admin_roles';
        $output .= '  <li>' . t('To give other members of your team access to the <a href="@tools_url" target="_blank">Instant Articles tools</a>, assign them <a href="@roles_url" target="_blank">page roles here</a>.', [
          '@tools_url' => $tools_url,
          '@roles_url' => $roles_url,
        ]) . '</li>';
      }
      else {
        $output .= '  <li>' . t('To give other members of your team access to the Instant Articles tools, assign them page roles.') . '</li>';
      }
      $output .= '</ol>';
      return $output;
  }
}

/**
 * Implements hook_page_attachments_alter().
 */
function fb_instant_articles_page_attachments_alter(array &$page) {
  $config = \Drupal::config('fb_instant_articles.settings');
  $path_matcher = \Drupal::service('path.matcher');
  if ($path_matcher
    ->isFrontPage() && ($page_id = $config
    ->get('page_id'))) {
    $meta_page_id = [
      '#tag' => 'meta',
      '#attributes' => [
        'property' => 'fb:pages',
        'content' => $page_id,
      ],
    ];
    $page['#attached']['html_head'][] = [
      $meta_page_id,
      'fia_pagesid',
    ];
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for \Drupal\node\NodeTypeForm.
 *
 * @see fb_instant_articles_form_node_type_form_builder()
 */
function fb_instant_articles_form_node_type_form_alter(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\node\NodeTypeInterface $node_type */
  $node_type = $form_state
    ->getFormObject()
    ->getEntity();

  // Add a vertical tab to the node type form.
  $form['fb_instant_articles'] = [
    '#type' => 'details',
    '#title' => t('Facebook Instant Articles'),
    '#group' => 'additional_settings',
    '#access' => \Drupal::currentUser()
      ->hasPermission('administer fb_instant_articles'),
  ];

  // Facebook Instant Articles enabled checkbox.
  $form['fb_instant_articles']['fb_instant_articles_enabled'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable Facebook Instant Articles'),
    '#description' => t('Enable content of this type with support for Facebook Instant Articles.'),
    '#weight' => 0,
    '#default_value' => $node_type
      ->getThirdPartySetting('fb_instant_articles', 'fb_instant_articles_enabled'),
  ];

  // Use an #entity_builder callback to edit a third party setting on the node
  // type entity before it gets saved to the database.
  $form['#entity_builders'][] = 'fb_instant_articles_form_node_type_form_builder';

  // Use a #submit callback to save a entity view display entity. This has to be
  // on #submit so that the node type entity has been saved since the entity
  // view display entity is dependent on the node type entity.
  $form['actions']['submit']['#submit'][] = 'fb_instant_articles_form_node_type_form_submit';
}

/**
 * Entity builder for the node type form with the FBIA toggle.
 *
 * @see fb_instant_articles_form_node_type_form_alter()
 */
function fb_instant_articles_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('fb_instant_articles_enabled') === 1) {
    $type
      ->setThirdPartySetting('fb_instant_articles', 'fb_instant_articles_enabled', 1);
  }
  else {
    $type
      ->unsetThirdPartySetting('fb_instant_articles', 'fb_instant_articles_enabled');
  }
}

/**
 * Submit function for the node type form with the FBIA toggle.
 */
function fb_instant_articles_form_node_type_form_submit(&$form, FormStateInterface $form_state) {
  $entity_type = $form_state
    ->getValue('type');
  if ($form_state
    ->getValue('fb_instant_articles_enabled') === 1) {

    /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
    if ($display = \Drupal::entityTypeManager()
      ->getStorage('entity_view_display')
      ->load('node.' . $entity_type . '.fb_instant_articles')) {
      $display
        ->setStatus(TRUE)
        ->save();
    }
    else {

      // Create a new view mode if it's not already created.
      $display = EntityViewDisplay::create([
        'id' => 'node.' . $entity_type . '.fb_instant_articles',
        'targetEntityType' => 'node',
        'bundle' => $entity_type,
        'mode' => 'fb_instant_articles',
        'status' => TRUE,
      ]);
      $display
        ->save();
    }
  }
  else {

    /** @var \Drupal\Core\Entity\Display\EntityDisplayInterface $display */
    if ($display = \Drupal::entityTypeManager()
      ->getStorage('entity_view_display')
      ->load('node.' . $entity_type . '.fb_instant_articles')) {
      $display
        ->setStatus(FALSE)
        ->save();
    }
  }
}

/**
 * Implements hook_entity_type_alter().
 */
function fb_instant_articles_entity_type_alter(array &$entity_types) {

  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  $entity_types['entity_view_display']
    ->setFormClass('edit', 'Drupal\\fb_instant_articles\\Form\\EntityViewDisplayEditForm');
}