Commit 4caa897c authored by Chris Burge's avatar Chris Burge
Browse files

Issue #3268283 by Chris Burge: ProviderBucket config entities should have...

Issue #3268283 by Chris Burge: ProviderBucket config entities should have custom provider dependencies
parent d4674b45
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -41,3 +41,15 @@ function oembed_providers_post_update_add_provider_to_media_source() {
  // in provider bucket-generated definitions.
  \Drupal::service('plugin.manager.media.source')->clearCachedDefinitions();
}

/**
 * Update Provider Buckets' dependencies on Custom Providers.
 */
function oembed_providers_post_update_update_provider_bucket_dependencies() {
  $entities = \Drupal::service('entity_type.manager')->getStorage('oembed_provider_bucket')->loadMultiple();
  // Resave all existing ProviderBucket config entities to add any missing
  // custom provider dependencies.
  foreach ($entities as $entity) {
    $entity->save();
  }
}
+26 −0
Original line number Diff line number Diff line
@@ -86,4 +86,30 @@ class ProviderBucket extends ConfigEntityBase {
    \Drupal::service('plugin.manager.media.source')->clearCachedDefinitions();
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();

    // Create an array of custom providers keyed by the machine name with
    // the provider name as the value.
    $custom_providers = [];
    foreach (\Drupal::service('media.oembed.provider_repository')->getCustomProviders() as $machine_name => $custom_provider) {
      $custom_providers[$machine_name] = $custom_provider['provider_name'];
    }
    // Identify custom providers used by this provider bucket by matching on
    // the provider name.
    $provider_bucket_custom_providers = array_intersect($custom_providers, $this->providers);

    // Load custom provider entities and loop through to add as dependencies.
    $provider_bucket_custom_providers = array_keys($provider_bucket_custom_providers);
    $custom_provider_entities = \Drupal::service('entity_type.manager')->getStorage('oembed_provider')->loadMultiple($provider_bucket_custom_providers);
    foreach ($custom_provider_entities as $custom_provider_entity) {
      $this->addDependency('config', $custom_provider_entity->getConfigDependencyName());
    }

    return $this;
  }

}
+3 −1
Original line number Diff line number Diff line
uuid: a5255e05-7dd6-4fad-b6c4-52ac24841e9c
langcode: en
status: true
dependencies: {  }
dependencies:
  config:
    - oembed_providers.provider.unl_mediahub
id: test_bucket
label: 'Test Bucket'
providers:
+24 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\Tests\oembed_providers\Functional;

use Drupal\oembed_providers\Entity\ProviderBucket;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\media\Traits\OEmbedTestTrait;

@@ -161,4 +162,27 @@ class ProviderBucketTest extends BrowserTestBase {
    $this->AssertFalse(in_array('YouTube', $providers));
  }

  /**
   * Tests dependency calculation for ProviderBucket entities.
   */
  public function testProviderBucketDependencyCalculation() {
    // Create a test provider bucket with a custom provider.
    $provider_bucket = ProviderBucket::create([
      'id' => 'my_test_bucket',
      'label' => 'My Test Bucket',
      'descriptions' => 'A Description of My Test Bucket',
      'providers' => [
        'Example Provider',
      ],
    ]);
    $provider_bucket->save();

    $expected = [
      'config' => [
        'oembed_providers.provider.example_provider',
      ],
    ];
    $this->AssertSame($expected, $provider_bucket->getDependencies());
  }

}