Skip to content
Snippets Groups Projects
Commit 3a2a6707 authored by Mikael Meulle's avatar Mikael Meulle Committed by Pierre Dureau
Browse files

Issue #3456083 by just_like_good_vibes: Add text format trusted callback to WysiwygWidget source

parent a3595c42
No related branches found
No related tags found
1 merge request!121Resolves issue 3456083 (Bug WysiwygWidget)
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\ui_patterns\SourcePluginBase;
/**
......@@ -20,7 +21,36 @@ use Drupal\ui_patterns\SourcePluginBase;
* tags = { "widget" }
* )
*/
class WysiwygWidget extends SourcePluginBase {
class WysiwygWidget extends SourcePluginBase implements TrustedCallbackInterface {
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['textFormat'];
}
/**
* Customize the text_format element.
*/
public static function textFormat($element) {
if (!isset($element['format']['format']['#access']) ||
$element['format']['format']['#access']) {
return $element;
}
// See code at Drupal\filter\Element\TextFormat::processTextFormat()
// when the format is not accessible, we need to make it accessible.
$element['format']['format']['#access'] = TRUE;
$config = \Drupal::configFactory()->get('filter.settings');
$fallback_format = $config->get('fallback_format');
$formats = filter_formats();
// We add the fallback format to the list of options if removed.
if (array_key_exists($fallback_format, $formats)
&& !array_key_exists($fallback_format, $element['format']['format']['#options'])) {
$element['format']['format']['#options'][$fallback_format] = $formats[$fallback_format]->label();
}
return $element;
}
/**
* {@inheritdoc}
......@@ -49,13 +79,18 @@ class WysiwygWidget extends SourcePluginBase {
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
return [
'value' => [
'#type' => 'text_format',
'#default_value' => $this->getSetting('value')['value'],
'#format' => $this->getSetting('value')['format'],
],
$value = $this->getSetting('value');
$element = [
'#type' => 'text_format',
];
if (is_array($value) && array_key_exists("value", $value)) {
$element['#default_value'] = $value['value'];
}
if (is_array($value) && array_key_exists("format", $value)) {
$element['#format'] = $value['format'];
}
$element['#pre_render'] = [[static::class, 'textFormat']];
return ['value' => $element];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment