On drupal 10 CKEditor saving inline images as "Temporary" (not saving inline images as "permanent") for custom forms. How to resolve this problem.
Example with CKEditor WYSIWYG inline images field.
Also see : Create a Form with Drupal 8 using Form API
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$form['text'] = [
'#type' => 'text_format',
'#format' => 'full_html',
'#title' => $this->t('CKEditor text'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$text = $form_state->getValue('text')['value'];
try {
self::ckeditorSetFileUsage($text);
}
catch (EntityStorageException $e) {
}
// custom codes.
parent::submitForm($form, $form_state);
}
/**
* Set Images / Filesas Permanent.
*
* @param string $text
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public static function ckeditorSetFileUsage(string $text, $module = 'ckeditor')
{
$uuids = _editor_parse_file_uuids($text);
foreach ($uuids as $uuid) {
if ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid)) {
/** @var \Drupal\file\FileInterface $file */
if ($file->isTemporary()) {
$file->setPermanent();
$file->save();
}
\Drupal::service('file.usage')->add($file, $module, 'file', $file->fid->value);
}
}
}
Comments