You are here

oembed_providers.module in oEmbed Providers 1.0.x

Same filename and directory in other branches
  1. 2.x oembed_providers.module
  2. 1.1.x oembed_providers.module

This module allows site builders and developers to manage oEmbed providers.

File

oembed_providers.module
View source
<?php

/**
 * @file
 * This module allows site builders and developers to manage oEmbed providers.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\oembed_providers\Traits\HelperTrait;

/**
 * Implements hook_media_source_info_alter().
 */
function oembed_providers_media_source_info_alter(array &$sources) {
  $config_allowed_providers = \Drupal::config('oembed_providers.settings')
    ->get('allowed_providers');
  $config_allowed_providers = $config_allowed_providers ? $config_allowed_providers : [];
  $provider_repository = \Drupal::service('media.oembed.provider_repository');
  $available_providers = [];
  foreach ($provider_repository
    ->getAll() as $provider) {
    $available_providers[] = $provider
      ->getName();
  }

  // Return sources that are 1) allowed per config and 2) exist as
  // an available provider.
  $sources['oembed:video']['providers'] = array_intersect($available_providers, $config_allowed_providers);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function oembed_providers_form_media_type_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\Core\Entity\EntityFormInterface */
  $callback_object = $form_state
    ->getBuildInfo()['callback_object'];

  /** @var \Drupal\media\MediaSourceInterface */
  $source = $callback_object
    ->getEntity()
    ->getSource();

  // Only render warning message for media types with oEmbed source.
  if ($source instanceof OEmbedInterface) {
    $warning = [
      '#markup' => HelperTrait::disabledProviderSecurityWarning(),
      // Simulate warning message.
      '#prefix' => '<div role="contentinfo" aria-label="Warning message" class="messages messages--warning">',
      '#suffix' => '</div>',
    ];
    array_unshift($form['source_dependent']['source_configuration'], $warning);
  }
}