View source
<?php
namespace Drupal\yamlform\Plugin\YamlFormElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Mail\MailFormatHelper;
class ProcessedText extends YamlFormMarkupBase {
public function getDefaultProperties() {
return parent::getDefaultProperties() + [
'text' => '',
'format' => filter_default_format(\Drupal::currentUser()),
];
}
public function getTranslatableProperties() {
return array_merge(parent::getTranslatableProperties(), [
'text',
]);
}
public function buildText(array &$element, $value, array $options = []) {
$render_element = $element;
$html = (string) \Drupal::service('renderer')
->renderPlain($render_element);
$element['#markup'] = MailFormatHelper::htmlToText($html);
unset($element['#type'], $element['#text'], $element['#format']);
return parent::buildText($element, $value, $options);
}
public function form(array $form, FormStateInterface $form_state) {
if (!$form_state
->getUserInput() && \Drupal::currentUser()
->hasPermission('administer yamlform')) {
drupal_set_message($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>.'), 'warning');
}
$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);
}
}
}