View source
<?php
namespace Drupal\bynder\Plugin\EntityBrowser\Widget;
use Drupal\bynder\BynderApiInterface;
use Drupal\bynder\Exception\BundleNotBynderException;
use Drupal\bynder\Exception\BundleNotExistException;
use Drupal\bynder\Exception\UnableToConnectException;
use Drupal\bynder\Plugin\MediaEntity\Type\Bynder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\entity_browser\WidgetBase;
use Drupal\entity_browser\WidgetValidationManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Config\ConfigFactoryInterface;
abstract class BynderWidgetBase extends WidgetBase {
protected $bynderApi;
protected $loggerFactory;
protected $languageManager;
protected $requestStack;
protected $config;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, BynderApiInterface $bynder_api, LoggerChannelFactoryInterface $logger_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack, ConfigFactoryInterface $config_factory) {
$this->bynderApi = $bynder_api;
$this->loggerFactory = $logger_factory;
$this->languageManager = $language_manager;
$this->requestStack = $request_stack;
$this->config = $config_factory;
parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $validation_manager);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('event_dispatcher'), $container
->get('entity_type.manager'), $container
->get('plugin.manager.entity_browser.widget_validation'), $container
->get('bynder_api'), $container
->get('logger.factory'), $container
->get('language_manager'), $container
->get('request_stack'), $container
->get('config.factory'));
}
public function defaultConfiguration() {
return [
'media_bundle' => NULL,
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['media_bundle'] = [
'#type' => 'select',
'#title' => $this
->t('Media bundle'),
'#default_value' => $this->configuration['media_bundle'],
'#required' => TRUE,
'#options' => [],
];
return $form;
}
public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
$form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
if (!$this
->checkBundle()) {
$form_state
->setValue('errors', TRUE);
return $form;
}
foreach ([
'consumer_key',
'consumer_secret',
'token',
'token_secret',
'account_domain',
] as $key) {
if ($this->config
->get('bynder.settings')
->get($key) === '') {
$form_state
->setValue('errors', TRUE);
(new UnableToConnectException())
->logException()
->displayMessage();
return $form;
}
}
if (!$this->bynderApi
->hasAccessToken() || $this->requestStack
->getCurrentRequest()
->getMethod() == 'POST' && $this->requestStack
->getCurrentRequest()->request
->get('op') == 'Reload after submit' && $form_state
->isProcessingInput() === NULL) {
$form_state
->setValue('errors', TRUE);
$form['message'] = [
'#markup' => $this
->t('You need to <a href="#login" class="oauth-link">log into Bynder</a> before importing assets.'),
'#prefix' => '<p>',
'#suffix' => '</p>',
];
$form['reload'] = [
'#type' => 'button',
'#value' => 'Reload after submit',
'#attached' => [
'library' => [
'bynder/oauth',
],
],
'#attributes' => [
'class' => [
'oauth-reload',
'visually-hidden',
],
],
];
return $form;
}
return $form;
}
protected function checkBundle() {
$bundle = $this->entityTypeManager
->getStorage('media_bundle')
->load($this->configuration['media_bundle']);
if (!$bundle) {
(new BundleNotExistException($this->configuration['media_bundle']))
->logException()
->displayMessage();
return FALSE;
}
elseif (!$bundle
->getType() instanceof Bynder) {
(new BundleNotBynderException($bundle
->label()))
->logException()
->displayMessage();
return FALSE;
}
return TRUE;
}
}