class CdnProvider in Express 8
The "cdn_provider" theme setting.
Plugin annotation
@BootstrapSetting(
id = "cdn_provider",
type = "select",
title = @Translation("CDN Provider"),
description = @Translation("Choose between jsdelivr or a custom cdn source."),
defaultValue = "jsdelivr",
empty_value = "",
weight = -1,
groups = {
"advanced" = @Translation("Advanced"),
"cdn" = @Translation("CDN (Content Delivery Network)"),
},
options = { },
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\bootstrap\Plugin\PluginBase
- class \Drupal\bootstrap\Plugin\Setting\SettingBase implements SettingInterface
- class \Drupal\bootstrap\Plugin\Setting\Advanced\Cdn\CdnProvider
- class \Drupal\bootstrap\Plugin\Setting\SettingBase implements SettingInterface
- class \Drupal\bootstrap\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CdnProvider
File
- themes/
contrib/ bootstrap/ src/ Plugin/ Setting/ Advanced/ Cdn/ CdnProvider.php, line 40 - Contains \Drupal\bootstrap\Plugin\Setting\Advanced\Cdn\CdnProvider.
Namespace
Drupal\bootstrap\Plugin\Setting\Advanced\CdnView source
class CdnProvider extends SettingBase {
/**
* The current provider.
*
* @var \Drupal\bootstrap\Plugin\Provider\ProviderInterface
*/
protected $provider;
/**
* The current provider manager instance.
*
* @var \Drupal\bootstrap\Plugin\ProviderManager
*/
protected $providerManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->providerManager = new ProviderManager($this->theme);
if (isset($plugin_definition['cdn_provider'])) {
$this->provider = $this->theme
->getProvider($plugin_definition['cdn_provider']);
}
}
/**
* {@inheritdoc}
*/
public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
// Retrieve the provider from form values or the setting.
$default_provider = $form_state
->getValue('cdn_provider', $this->theme
->getSetting('cdn_provider'));
$group = $this
->getGroupElement($form, $form_state);
$group
->setProperty('description', '<div class="alert alert-info messages warning"><strong>' . t('NOTE') . ':</strong> ' . t('Using one of the "CDN Provider" options below is the preferred method for loading Bootstrap CSS and JS on simpler sites that do not use a site-wide CDN. Using a "CDN Provider" for loading Bootstrap, however, does mean that it depends on a third-party service. There is no obligation or commitment by these third-parties that guarantees any up-time or service quality. If you need to customize Bootstrap and have chosen to compile the source code locally (served from this site), you must disable the "CDN Provider" option below by choosing "- None -" and alternatively enable a site-wide CDN implementation. All local (served from this site) versions of Bootstrap will be superseded by any enabled "CDN Provider" below. <strong>Do not do both</strong>.') . '</div>');
$group
->setProperty('open', !!$default_provider);
// Intercept possible manual import of API data via AJAX callback.
$this
->importProviderData($form_state);
$providers = $this->theme
->getProviders();
$options = [];
foreach ($providers as $plugin_id => $provider) {
$options[$plugin_id] = $provider
->getLabel();
$this
->createProviderGroup($group, $provider);
}
// Override the options with the provider manager discovery.
$setting = $this
->getSettingElement($form, $form_state);
$setting
->setProperty('options', $options);
}
/**
* AJAX callback for reloading CDN provider elements.
*
* @param array $form
* Nested array of form elements that comprise the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public static function ajaxCallback(array $form, FormStateInterface $form_state) {
return $form['advanced']['cdn'][$form_state
->getValue('cdn_provider', Bootstrap::getTheme()
->getSetting('cdn_provider'))];
}
/**
* Creates the necessary containers for each provider.
*
* @param \Drupal\bootstrap\Utility\Element $group
* The group element instance.
* @param \Drupal\bootstrap\Plugin\Provider\ProviderInterface $provider
* The provider instance.
*/
private function createProviderGroup(Element $group, ProviderInterface $provider) {
$plugin_id = Html::cleanCssIdentifier($provider
->getPluginId());
// Create the provider container.
$group->{$plugin_id} = [
'#type' => 'container',
'#prefix' => '<div id="cdn-provider-' . $plugin_id . '">',
'#suffix' => '</div>',
'#states' => [
'visible' => [
':input[name="cdn_provider"]' => [
'value' => $plugin_id,
],
],
],
];
// Add in the provider description.
if ($description = $provider
->getDescription()) {
$group->{$plugin_id}->description = [
'#markup' => '<div class="lead">' . $description . '</div>',
'#weight' => -99,
];
}
// Indicate there was an error retrieving the provider's API data.
if ($provider
->hasError() || $provider
->isImported()) {
if ($provider
->hasError()) {
$group->{$plugin_id}->error = [
'#markup' => '<div class="alert alert-danger messages error"><strong>' . t('ERROR') . ':</strong> ' . t('Unable to reach or parse the data provided by the @title API. Ensure the server this website is hosted on is able to initiate HTTP requests. If the request consistently fails, it is likely that there are certain PHP functions that have been disabled by the hosting provider for security reasons. It is possible to manually copy and paste the contents of the following URL into the "Imported @title data" section below.<br /><br /><a href=":provider_api" target="_blank">:provider_api</a>.', [
'@title' => $provider
->getLabel(),
':provider_api' => $provider
->getApi(),
]) . '</div>',
'#weight' => -20,
];
}
$group->{$plugin_id}->import = [
'#type' => 'details',
'#title' => t('Imported @title data', [
'@title' => $provider
->getLabel(),
]),
'#description' => t('The provider will attempt to parse the data entered here each time it is saved. If no data has been entered, any saved files associated with this provider will be removed and the provider will again attempt to request the API data normally through the following URL: <a href=":provider_api" target="_blank">:provider_api</a>.', [
':provider_api' => $provider
->getPluginDefinition()['api'],
]),
'#weight' => 10,
'#open' => FALSE,
];
$group->{$plugin_id}->import->cdn_provider_import_data = [
'#type' => 'textarea',
'#default_value' => file_exists(ProviderManager::FILE_PATH . '/' . $plugin_id . '.json') ? file_get_contents(ProviderManager::FILE_PATH . '/' . $plugin_id . '.json') : NULL,
];
$group->{$plugin_id}->import->submit = [
'#type' => 'submit',
'#value' => t('Save provider data'),
'#executes_submit_callback' => FALSE,
'#ajax' => [
'callback' => [
get_class($this),
'ajaxCallback',
],
'wrapper' => 'cdn-provider-' . $plugin_id,
],
];
}
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
return [
'library_info',
];
}
/**
* Imports data for a provider that was manually uploaded in theme settings.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
private function importProviderData(FormStateInterface $form_state) {
if ($form_state
->getValue('clicked_button') === t('Save provider data')
->render()) {
$provider_path = ProviderManager::FILE_PATH;
file_prepare_directory($provider_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$provider = $form_state
->getValue('cdn_provider', $this->theme
->getSetting('cdn_provider'));
$file = "{$provider_path}/{$provider}.json";
if ($import_data = $form_state
->getValue('cdn_provider_import_data', FALSE)) {
file_unmanaged_save_data($import_data, $file, FILE_EXISTS_REPLACE);
}
elseif ($file && file_exists($file)) {
file_unmanaged_delete($file);
}
// Clear the cached definitions so they can get rebuilt.
$this->providerManager
->clearCachedDefinitions();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CdnProvider:: |
protected | property | The current provider. | |
CdnProvider:: |
protected | property | The current provider manager instance. | |
CdnProvider:: |
public static | function | AJAX callback for reloading CDN provider elements. | 1 |
CdnProvider:: |
public | function |
The alter method to store the code. Overrides SettingBase:: |
2 |
CdnProvider:: |
private | function | Creates the necessary containers for each provider. | |
CdnProvider:: |
public | function |
The cache tags associated with this object. Overrides SettingBase:: |
|
CdnProvider:: |
private | function | Imports data for a provider that was manually uploaded in theme settings. | |
CdnProvider:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
protected | property | The currently set theme object. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SettingBase:: |
public | function |
The alter method to store the code. Overrides FormInterface:: |
|
SettingBase:: |
public | function |
Determines whether a theme setting should added to drupalSettings. Overrides SettingInterface:: |
25 |
SettingBase:: |
public | function |
Retrieves the setting's default value. Overrides SettingInterface:: |
|
SettingBase:: |
public | function |
Overrides SettingInterface:: |
|
SettingBase:: |
public | function | Retrieves all the form properties from the setting definition. | |
SettingBase:: |
public | function |
Overrides SettingInterface:: |
|
SettingBase:: |
public | function |
Retrieves the group form element the setting belongs to. Overrides SettingInterface:: |
|
SettingBase:: |
public | function |
Retrieves the setting's groups. Overrides SettingInterface:: |
|
SettingBase:: |
public | function |
Retrieves the settings options, if set. Overrides SettingInterface:: |
|
SettingBase:: |
public | function |
Retrieves the form element for the setting. Overrides SettingInterface:: |
|
SettingBase:: |
public | function |
Retrieves the setting's human-readable title. Overrides SettingInterface:: |
|
SettingBase:: |
public static | function |
Form submission handler. Overrides FormInterface:: |
|
SettingBase:: |
public static | function |
Form submission handler. Overrides FormInterface:: |
1 |
SettingBase:: |
public static | function |
Form validation handler. Overrides FormInterface:: |
|
SettingBase:: |
public static | function |
Form validation handler. Overrides FormInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |