View source
<?php
namespace Drupal\webform\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProcessedText extends WebformMarkupBase {
protected $renderer;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->renderer = $container
->get('renderer');
return $instance;
}
protected function defineDefaultProperties() {
if (function_exists('filter_formats')) {
$formats = filter_formats($this->currentUser);
$format = reset($formats);
$default_format = $format ? $format
->id() : filter_fallback_format();
}
else {
$default_format = '';
}
return [
'wrapper_attributes' => [],
'label_attributes' => [],
'text' => '',
'format' => $default_format,
] + parent::defineDefaultProperties();
}
protected function defineTranslatableProperties() {
return array_merge(parent::defineTranslatableProperties(), [
'text',
]);
}
public function buildText(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$render_element = $element;
$html = (string) $this->renderer
->renderPlain($render_element);
$element['#markup'] = MailFormatHelper::htmlToText($html);
unset($element['#type'], $element['#text'], $element['#format']);
return parent::buildText($element, $webform_submission, $options);
}
public function preview() {
return $this->moduleHandler
->moduleExists('filter') ? parent::preview() : [];
}
public function form(array $form, FormStateInterface $form_state) {
if (!$form_state
->getUserInput() && $this->currentUser
->hasPermission('administer webform')) {
$this
->messenger()
->addWarning($this
->t('Processed text element can not be opened within a modal. Please see <a href="https://www.drupal.org/node/2741877">Issue #2741877: Nested modals don\'t work</a>.'));
}
$form = parent::form($form, $form_state);
unset($form['display']);
$form['markup']['#title'] = $this
->t('Processed text settings');
$form['markup']['text'] = [
'#type' => 'text_format',
'#format' => '',
];
return $form;
}
protected function setConfigurationFormDefaultValue(array &$form, array &$element_properties, array &$property_element, $property_name) {
if ($property_name === 'text') {
$property_element['#format'] = $element_properties['format'];
unset($element_properties['format']);
}
parent::setConfigurationFormDefaultValue($form, $element_properties, $property_element, $property_name);
}
protected function getConfigurationFormProperty(array &$properties, $property_name, $property_value, array $element) {
if ($property_name === 'text') {
$properties['text'] = $property_value['value'];
$properties['format'] = $property_value['format'];
}
else {
parent::getConfigurationFormProperty($properties, $property_name, $property_value, $element);
}
}
}