AllowedProvidersForm.php in oEmbed Providers 1.0.x
File
src/Form/AllowedProvidersForm.php
View source
<?php
namespace Drupal\oembed_providers\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\oembed_providers\OEmbed\ProviderRepositoryDecorator;
use Drupal\oembed_providers\Traits\HelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AllowedProvidersForm extends ConfigFormBase {
const SETTINGS = 'oembed_providers.settings';
protected $providerRepository;
public function __construct(ConfigFactoryInterface $config_factory, ProviderRepositoryDecorator $provider_repository) {
$this
->setConfigFactory($config_factory);
$this->providerRepository = $provider_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('media.oembed.provider_repository'));
}
public function getFormId() {
return 'oembed_allowed_providers_settings';
}
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config(static::SETTINGS);
$form['security_warning'] = [
'#markup' => HelperTrait::disabledProviderSecurityWarning(),
'#prefix' => '<div role="contentinfo" aria-label="Warning message" class="messages messages--warning">',
'#suffix' => '</div>',
];
if (empty($config
->get('allowed_providers'))) {
$form['install_markup'] = [
'#markup' => $this
->t('The <em>oEmbed Providers</em> module now manages oEmbed providers. Allowed oEmbed providers must be configured below.'),
'#prefix' => '<div role="contentinfo" aria-label="Warning message" class="messages messages--warning">',
'#suffix' => '</div>',
];
}
$form['markup'] = [
'#markup' => $this
->t('<p>Providers enabled below will be made available as media sources.</p>'),
];
$providers = $this->providerRepository
->getAll();
$provider_keys = [];
foreach ($providers as $provider) {
$provider_keys[$provider
->getName()] = $provider
->getName();
}
$form['allowed_providers'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Allowed Providers'),
'#default_value' => $config
->get('allowed_providers') ? $config
->get('allowed_providers') : [],
'#options' => $provider_keys,
];
$form['#attached']['library'][] = 'oembed_providers/settings_form';
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$allowed_providers = [];
foreach ($form_state
->getValue('allowed_providers') as $provider) {
if ($provider) {
$allowed_providers[] = $provider;
}
}
$this->configFactory
->getEditable(static::SETTINGS)
->set('allowed_providers', $allowed_providers)
->save();
parent::submitForm($form, $form_state);
}
}