Commit 63f8d038 authored by Stefanos Petrakis's avatar Stefanos Petrakis Committed by Sascha Grossenbacher
Browse files

Issue #3272702 by stefanos.petrakis, Berdir, emptyvoid, chrisolof: Support for...

Issue #3272702 by stefanos.petrakis, Berdir, emptyvoid, chrisolof: Support for Facebook/Instagram API (2022), switch to embed/embed v4
parent 64596b7f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@
  },
  "license": "GPL-2.0+",
  "require": {
    "embed/embed": "^3.4",
    "embed/embed": "^4.4",
    "drupal/embed": "^1.4"
  }
}
+10 −0
Original line number Diff line number Diff line
url_embed.settings:
  type: config_object
  label: 'Url Embed config.'
  mapping:
    facebook_app_id:
      type: string
      label: 'Facebook App ID'
    facebook_app_secret:
      type: string
      label: 'Facebook App Secret'
+88 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\url_embed\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Configure Url Embed settings for this site.
 */
class UrlEmbedAdminSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'url_embed_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['url_embed.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('url_embed.settings');

    $form['facebook_app'] = [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => $this->t('Facebook / Instagram App'),
      '#description' => $this->t('As of Oct 24 2020, Facebook and Instagram require you to create a live <a href="@doc_url" target="_blank">facebook app with an oEmbed product enabled</a> to embed posts.', [
        '@doc_url' => 'https://developers.facebook.com/docs/plugins/oembed',
      ]),
    ];

    // If we have Facebook credentials in the config, check that it is a valid app and show a message to the user.
    if (!empty($config->get('facebook_app_id')) && !empty($config->get('facebook_app_id'))) {
      $debug = url_embed_debug_facebook_access_token($config->get('facebook_app_id') . '|' . $config->get('facebook_app_secret'));
      if (!empty($debug['is_valid'])) {
        $form['facebook_app']['facebook_app_status']['#markup'] = '<div class="messages messages--status">' . $this->t('The Facebook app is active.') . '</div>';
      }
      else {
        $form['facebook_app']['facebook_app_status']['#markup'] = '<div class="messages messages--warning">' . $this->t('The App ID and App Secret combination is invalid. Make sure you entered your app credentials correctly.') . '</div>';
      }
    }

    $form['facebook_app']['facebook_app_id'] = [
      '#type' => 'textfield',
      '#title' => $this->t('App ID'),
      '#default_value' => $config->get('facebook_app_id'),
    ];

    $form['facebook_app']['facebook_app_secret'] = [
      '#type' => 'textfield',
      '#title' => $this->t('App Secret'),
      '#default_value' => $config->get('facebook_app_secret'),
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('url_embed.settings');
    $config
      ->set('facebook_app_id', $form_state->getValue('facebook_app_id'))
      ->set('facebook_app_secret', $form_state->getValue('facebook_app_secret'))
      ->save();

    parent::submitForm($form, $form_state);
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -107,7 +107,7 @@ class UrlEmbedDialog extends FormBase {

    try {
      if (!empty($url_element['data-embed-url']) && $info = $this->urlEmbed()->getEmbed($url_element['data-embed-url'])) {
        $url_element['data-url-provider'] = $info->getProviderName();
        $url_element['data-url-provider'] = $info->providerName;
      }
    }
    catch (\Exception $e) {
+8 −5
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ namespace Drupal\url_embed\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\url_embed\UrlEmbedHelperTrait;
use Drupal\Core\Logger\RfcLogLevel;

/**
 * Plugin implementation of the 'url_embed' formatter.
@@ -34,13 +35,15 @@ class LinkEmbedFormatter extends FormatterBase {
    foreach ($items as $delta => $item) {
      if ($url = $item->getUrl()->toString()) {
        try {
          if ($info = $this->urlEmbed()->getEmbed($url)) {
          $html = $this->urlEmbed()->getEmbed($url)->code->html ?? NULL;
          if (!$html) {
            throw new \Exception('Could not retrieve HTML content for ' . $url);
          }
          $element[$delta] = array(
            '#type' => 'inline_template',
              '#template' => $info->getCode(),
            '#template' => $html,
          );
        }
        }
        catch (\Exception $exception) {
          watchdog_exception('url_embed', $exception);
        }
Loading