View source
<?php
namespace Drupal\oembed_providers;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\Element\Checkboxes;
use Drupal\oembed_providers\OEmbed\ProviderRepositoryDecorator;
use Drupal\oembed_providers\Traits\HelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OembedProviderBucketForm extends EntityForm {
protected $providerRepository;
protected $entityTypeManager;
protected $messenger;
public function __construct(ProviderRepositoryDecorator $provider_repository, EntityTypeManagerInterface $entityTypeManager, MessengerInterface $messenger) {
$this->providerRepository = $provider_repository;
$this->entityTypeManager = $entityTypeManager;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('media.oembed.provider_repository'), $container
->get('entity_type.manager'), $container
->get('messenger'));
}
public function form(array $form, FormStateInterface $form_state) {
if ($this->operation == 'edit') {
$form['#title'] = $this
->t('<em>Edit oEmbed provider bucket</em> @label', [
'@label' => $this->entity
->label(),
]);
}
$entity = $this->entity;
$form['security_warning'] = [
'#markup' => HelperTrait::disabledProviderSecurityWarning(),
'#prefix' => '<div role="contentinfo" aria-label="Warning message" class="messages messages--warning">',
'#suffix' => '</div>',
];
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Bucket name'),
'#default_value' => $entity
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $entity
->id(),
'#machine_name' => [
'exists' => [
$this,
'exist',
],
],
'#disabled' => !$entity
->isNew(),
];
$form['description'] = [
'#type' => 'textfield',
'#title' => $this
->t('Description'),
'#default_value' => $entity
->get('description'),
];
$providers = $this->providerRepository
->getAll();
$provider_keys = [];
foreach ($providers as $provider) {
$provider_keys[$provider
->getName()] = $provider
->getName();
}
$form['markup'] = [
'#markup' => $this
->t('<p>Providers enabled below will be made available to this media source.</p>'),
];
$form['providers'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Allowed Providers'),
'#default_value' => $entity
->get('providers') ? $entity
->get('providers') : [],
'#options' => $provider_keys,
];
$form['#attached']['library'][] = 'oembed_providers/provider_bucket_form';
return parent::form($form, $form_state);
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$providers = Checkboxes::getCheckedCheckboxes($entity
->get('providers'));
$entity
->set('providers', $providers);
$status = $entity
->save();
if ($status === SAVED_NEW) {
$this
->messenger()
->addMessage($this
->t('The %label oEmbed provider bucket was created.', [
'%label' => $entity
->label(),
]));
}
else {
$this
->messenger()
->addMessage($this
->t('The %label oEmbed provider bucket was updated.', [
'%label' => $entity
->label(),
]));
}
$form_state
->setRedirect('entity.oembed_provider_bucket.collection');
}
public function exist($id) {
if ($id == 'add') {
return TRUE;
}
$entity = $this->entityTypeManager
->getStorage('oembed_provider_bucket')
->getQuery()
->condition('id', $id)
->execute();
return (bool) $entity;
}
}