sub-topic @BootstrapProvider
Create a plugin
We'll use the \Drupal\bootstrap\Plugin\Provider\JsDelivr
CDN Provider as an
example of how to create a quick custom CDN provider using its API URLs.
Replace all following instances of THEMENAME
with the actual machine name of
your sub-theme.
You may also feel free to replace the provided URLs with your own. Most of the popular CDN API output can be easily parsed, however you may need to provide addition parsing in your custom CDN Provider if you're not getting the desired results.
If you're truly interested in implementing a CDN Provider, it is highly recommended that you read the accompanying PHP based documentation on the classes and methods responsible for actually retrieving, parsing and caching the data from the CDN's API.
Create a file at ./themes/THEMENAME/src/Plugin/Provider/MyCdn.php
with the
following contents:
<?php
namespace Drupal\THEMENAME\Plugin\Provider;
use Drupal\bootstrap\Plugin\Provider\ApiProviderBase;
/**
* The "mycdn" CDN Provider plugin.
*
* @ingroup plugins_provider
*
* @BootstrapProvider(
* id = "mycdn",
* label = @Translation("My CDN"),
* description = @Translation("My CDN (jsDelivr)"),
* weight = -1
* )
*/
class JsDelivr extends ApiProviderBase {
/**
* {@inheritdoc}
*/
protected function getApiAssetsUrlTemplate() {
return 'https://data.jsdelivr.com/v1/package/npm/@[email protected]@version/flat';
}
/**
* {@inheritdoc}
*/
protected function getApiVersionsUrlTemplate() {
return 'https://data.jsdelivr.com/v1/package/npm/@library';
}
/**
* {@inheritdoc}
*/
protected function getCdnUrlTemplate() {
return 'https://cdn.jsdelivr.net/npm/@[email protected]@version/@file';
}
}
?>
Rebuild the cache
Once you have saved, you must rebuild your cache for this new plugin to be
discovered. This must happen anytime you make a change to the actual file name
or the information inside the @BootstrapProvider
annotation.
To rebuild your cache, navigate to admin/config/development/performance
and
click the Clear all caches
button. Or if you prefer, run drush cr
from the
command line.
VoilĂ ! After this, you should have a fully functional @BootstrapProvider
plugin!
Parent topics
Source docs/plugins/Provider.md
Classes
Name | Description |
---|---|
ApiProviderBase | CDN Provider base that uses an API to populate its assets. |
BootstrapProvider | Defines a BootstrapProvider annotation object. |
Broken | Broken CDN Provider instance. |
CdnJs | The "cdnjs" CDN Provider plugin. |
CdnProvider | The "cdn_provider" theme setting. |
CdnProviderBase | A base class for CDN Provider settings. |
Custom | The "custom" CDN Provider plugin. |
DrupalBootstrapStyles | The "drupal_bootstrap_styles" CDN Provider plugin. |
JsDelivr | The "jsdelivr" CDN Provider plugin. |
ProviderBase | CDN Provider base class. |
ProviderManager | Manages discovery and instantiation of Bootstrap CDN Providers. |
Interfaces
Name | Description |
---|---|
ProviderInterface | ProviderInterface. |