View source
<?php
namespace Drupal\yamlform\Plugin\YamlFormElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\yamlform\YamlFormElementBase;
use Drupal\yamlform\YamlFormInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\yamlform\YamlFormSubmissionInterface;
class Table extends YamlFormElementBase {
public function getDefaultProperties() {
return [
'header' => [],
'empty' => '',
];
}
public function getTranslatableProperties() {
return array_merge(parent::getTranslatableProperties(), [
'header',
]);
}
public function isInput(array $element) {
return FALSE;
}
public function isContainer(array $element) {
return TRUE;
}
public function prepare(array &$element, YamlFormSubmissionInterface $yamlform_submission) {
parent::prepare($element, $yamlform_submission);
$element['#attributes']['class'][] = 'js-form-wrapper';
$element['#tree'] = FALSE;
}
protected function build($format, array &$element, $value, array $options = []) {
return parent::build($format, $element, $value, $options);
}
public function getDefaultFormat() {
return 'table';
}
public function getFormats() {
return [
'table',
];
}
public function getTestValue(array $element, YamlFormInterface $yamlform) {
return NULL;
}
public function formatHtml(array &$element, $value, array $options = []) {
$rows = [];
foreach ($value as $row_key => $row_element) {
$element[$row_key] = [];
foreach ($row_element['#value'] as $column_key => $column_element) {
if (isset($column_element['#value'])) {
if (is_string($column_element['#value']) || $column_element['#value'] instanceof TranslatableMarkup) {
$value = [
'#markup' => $column_element['#value'],
];
}
else {
$value = $column_element['#value'];
}
}
elseif (isset($column_element['#markup'])) {
$value = [
'#markup' => $column_element['#markup'],
];
}
else {
$value = '';
}
$rows[$row_key][$column_key] = [
'data' => $value,
];
}
}
return $rows + $element;
}
public function formatText(array &$element, $value, array $options = []) {
$build = $this
->formatHtml($element, $value, $options);
$html = \Drupal::service('renderer')
->renderPlain($build);
$html = preg_replace('#\\s*</td>\\s*<td[^>]*>\\s*#', ' | ', $html);
$html = preg_replace('#\\s*</th>\\s*<th[^>]*>\\s*#', ' | ', $html);
$html = preg_replace('#^\\s+#m', '', $html);
$html = preg_replace('#\\s+$#m', '', $html);
$html = preg_replace('#\\n+#s', "\n", $html);
$html = strip_tags($html);
$html = preg_replace("/(^[\r\n]*|[\r\n]+)[\\s\t]*[\r\n]+/", "\n", $html);
if (!empty($element['#header'])) {
$lines = explode("\n", trim($html));
$lines[0] .= "\n" . str_repeat('-', Unicode::strlen($lines[0]));
$html = implode("\n", $lines);
}
return $html;
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['table'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Table settings'),
];
$form['table']['header'] = [
'#title' => $this
->t('Header (YAML)'),
'#type' => 'yamlform_codemirror',
'#mode' => 'yaml',
];
$form['table']['empty'] = [
'#type' => 'textfield',
'#title' => $this
->t('Empty text'),
'#description' => $this
->t('Text to display when no rows are present.'),
];
return $form;
}
public function getElementSelectorOptions(array $element) {
return [];
}
}