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\media\Source\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_type' => NULL,
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['media_type'] = [
'#type' => 'select',
'#title' => $this
->t('Media type'),
'#default_value' => $this->configuration['media_type'],
'#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
->checkType()) {
$form_state
->setValue('errors', TRUE);
return $form;
}
foreach ([
'permanent_token',
'account_domain',
] as $key) {
if ($this->config
->get('bynder.settings')
->get($key) === '') {
$form_state
->setValue('errors', TRUE);
(new UnableToConnectException())
->logException()
->displayMessage();
return $form;
}
}
return $form;
}
protected function checkType() {
if ($this->configuration['media_type']) {
$type = $this->entityTypeManager
->getStorage('media_type')
->load($this->configuration['media_type']);
if (!$type) {
(new BundleNotExistException($this->configuration['media_type']))
->logException()
->displayMessage();
return FALSE;
}
elseif (!$type
->getSource() instanceof Bynder) {
(new BundleNotBynderException($type
->label()))
->logException()
->displayMessage();
return FALSE;
}
return TRUE;
}
}
}