Skip to content
Snippets Groups Projects

Issue # 3501958 by danrod: Fixes in the Settings form that wasn't doing the...

5 files
+ 68
31
Compare changes
  • Side-by-side
  • Inline
Files
5
+ 56
24
@@ -6,7 +6,10 @@ use Drupal\Core\Form\ConfigFormBase;
@@ -6,7 +6,10 @@ use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
 
use Drupal\Core\Config\TypedConfigManagerInterface;
 
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
 
use Drupal\file\Entity\File;
/**
/**
* Defines a form that configures gifplayer settings.
* Defines a form that configures gifplayer settings.
@@ -20,21 +23,30 @@ class SettingsForm extends ConfigFormBase {
@@ -20,21 +23,30 @@ class SettingsForm extends ConfigFormBase {
*/
*/
protected $entityTypeManager;
protected $entityTypeManager;
 
/**
 
* The entity type bundle info service.
 
*
 
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
 
*/
 
protected $entityTypeBundleInfo;
 
/**
/**
* Constructs a \Drupal\system\ConfigFormBase object.
* Constructs a \Drupal\system\ConfigFormBase object.
*
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* The factory for configuration objects.
 
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
 
* The typed config manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The core entity type manager.
* The core entity type manager.
 
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
 
* The entity type bundle info service.
*/
*/
public function __construct(
public function __construct(
ConfigFactoryInterface $config_factory,
ConfigFactoryInterface $config_factory,TypedConfigManagerInterface $typedConfigManager, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($config_factory, $typedConfigManager);
parent::__construct($config_factory);
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeManager = $entity_type_manager;
 
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
}
/**
/**
@@ -43,7 +55,9 @@ class SettingsForm extends ConfigFormBase {
@@ -43,7 +55,9 @@ class SettingsForm extends ConfigFormBase {
public static function create(ContainerInterface $container) {
public static function create(ContainerInterface $container) {
return new static(
return new static(
$container->get('config.factory'),
$container->get('config.factory'),
$container->get('entity_type.manager')
$container->get('config.typed'),
 
$container->get('entity_type.manager'),
 
$container->get('entity_type.bundle.info'),
);
);
}
}
@@ -73,7 +87,7 @@ class SettingsForm extends ConfigFormBase {
@@ -73,7 +87,7 @@ class SettingsForm extends ConfigFormBase {
$form['library_attach'] = [
$form['library_attach'] = [
'#type' => 'checkbox',
'#type' => 'checkbox',
'#title' => $this->t('Attach GifPlayer library globaly.'),
'#title' => $this->t('Attach GifPlayer library globally.'),
'#description' => $this->t(
'#description' => $this->t(
'Check it to load GifPlayer library globally. By default this is
'Check it to load GifPlayer library globally. By default this is
attached only to the GifPlayer field and can be used only inside forms
attached only to the GifPlayer field and can be used only inside forms
@@ -97,8 +111,7 @@ class SettingsForm extends ConfigFormBase {
@@ -97,8 +111,7 @@ class SettingsForm extends ConfigFormBase {
'File used as cover on GIF player fields. Leave this empty to use the
'File used as cover on GIF player fields. Leave this empty to use the
module default file. PNG formats only.'
module default file. PNG formats only.'
),
),
'#default_value' => ($config->get('cover_png') === 0) ?:
'#default_value' => ($config->get('cover_png') !== NULL) ? [$config->get('cover_png')] : [0],
[$config->get('cover_png')],
'#upload_validators' => ['png'],
'#upload_validators' => ['png'],
'#upload_location' => 'public://my_files/',
'#upload_location' => 'public://my_files/',
];
];
@@ -106,7 +119,7 @@ class SettingsForm extends ConfigFormBase {
@@ -106,7 +119,7 @@ class SettingsForm extends ConfigFormBase {
// Add hidden value of default FID of cover PNG if this has been added.
// Add hidden value of default FID of cover PNG if this has been added.
$form['cpid'] = [
$form['cpid'] = [
'#type' => 'hidden',
'#type' => 'hidden',
'#value' => $config->get('cover_png'),
'#value' => ($config->get('cover_png') !== NULL) ? [$config->get('cover_png')] : [0],
];
];
return parent::buildForm($form, $form_state);
return parent::buildForm($form, $form_state);
@@ -117,28 +130,47 @@ class SettingsForm extends ConfigFormBase {
@@ -117,28 +130,47 @@ class SettingsForm extends ConfigFormBase {
*/
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$values = $form_state->getValues();
$cid = (int) reset($values['cover_png']);
$cpid = (int) $values['cpid'];
// Set file status permanent.
$fid = reset($form_state->getValue('cover_png'));
if ($cid !== 0 && $file = $this->loadFile($cid)) {
$cpid = reset($values['cpid']);
$file->setPermanent();
$file->save();
// If $cpid is not empty and $fid is empty, means that the user has deleted the PNG file
 
 
// The PNG file hasn't changed
 
if ($fid === $cpid) {
 
parent::submitForm($form, $form_state);
}
}
// Delete previously uploaded file if that has been changed.
if (isset($fid) && is_numeric($fid)) {
if ($cid !== $cpid && $file = $this->loadFile($cpid)) {
$file = File::load($fid);
$file->delete();
 
// There is a new file that wants to replace the old file
 
if (($cpid > 0) && ($fid !== $cpid)) {
 
$oldfile = File::load($cpid);
 
$oldfile->delete();
 
}
 
 
// Set file status permanent.
 
if ($fid !== 0) {
 
$file->setPermanent();
 
$file->save();
 
}
 
 
}
 
 
// The user has just deleted the PNG file
 
if (($cpid > 0) && ($fid === FALSE)) {
 
$fid = 0;
}
}
// Save the updated settings.
// Save the updated settings.
$this->config('gifplayer.settings')
$this->config('gifplayer.settings')
->set('library_attach', $values['library_attach'])
->set('library_attach', $values['library_attach'])
->set('cover_png', $cid)
->set('cover_png', $fid)
->save();
->save();
parent::submitForm($form, $form_state);
parent::submitForm($form, $form_state);
}
}
/**
/**
* Load given id file.
* Load given id file.
@@ -152,11 +184,11 @@ class SettingsForm extends ConfigFormBase {
@@ -152,11 +184,11 @@ class SettingsForm extends ConfigFormBase {
protected function loadFile(int $fid) {
protected function loadFile(int $fid) {
// Load file and return this if exists.
// Load file and return this if exists.
if ($file = $this->entityTypeManager->getStorage('file')->load($fid)) {
if ($file = $this->entityTypeManager->getStorage('file')->load($fid)) {
return $file;
return $file;
}
}
return FALSE;
return FALSE;
}
}
}
}
Loading