View source
<?php
namespace Drupal\webform_devel\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\webform\Form\WebformEntityAjaxFormTrait;
use Drupal\webform\Utility\WebformDialogHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformDevelEntitySchemaForm extends EntityForm {
use WebformEntityAjaxFormTrait;
protected $scheme;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->scheme = $container
->get('webform_devel.schema');
return $instance;
}
public function form(array $form, FormStateInterface $form_state) {
$webform_ui_exists = $this->moduleHandler
->moduleExists('webform_ui');
$webform = $this
->getEntity();
$header = $this->scheme
->getColumns();
if ($webform_ui_exists) {
$header['operations'] = $this
->t('Operations');
}
$rows = [];
$elements = $this->scheme
->getElements($webform);
foreach ($elements as $element_key => $element) {
$rows[$element_key] = [];
foreach ($element as $key => $value) {
if ($key === 'options_value' || $key === 'options_text') {
$value = implode('; ', array_slice($value, 0, 12)) . (count($value) > 12 ? '; …' : '');
}
$rows[$element_key][$key] = [
'#markup' => $value,
];
}
if ($element['datatype'] === 'Composite') {
$rows[$element_key]['#attributes']['class'][] = 'webform-devel-schema-composite';
}
if ($webform_ui_exists) {
if (strpos($element_key, '.') === FALSE && $webform
->getElement($element_key)) {
$element_url = new Url('entity.webform_ui.element.edit_form', [
'webform' => $webform
->id(),
'key' => $element_key,
], [
'query' => [
'destination' => Url::fromRoute('<current>')
->toString(),
],
]);
$rows[$element_key]['name'] = [
'#type' => 'link',
'#title' => $element_key,
'#url' => $element_url,
'#attributes' => WebformDialogHelper::getModalDialogAttributes(),
];
$rows[$element_key]['operations'] = [
'#type' => 'link',
'#title' => $this
->t('Edit'),
'#url' => $element_url,
'#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NORMAL, [
'button',
'button--small',
]),
];
}
else {
$rows[$element_key]['operations'] = [
'#markup' => '',
];
}
$rows[$element_key]['#attributes']['data-webform-key'] = explode('.', $element_key)[0];
}
}
$form['schema'] = [
'#type' => 'table',
'#header' => $header,
'#attributes' => [
'class' => [
'webform-devel-schema-table',
],
],
] + $rows;
WebformDialogHelper::attachLibraries($form);
$form['#attached']['library'][] = 'webform_devel/webform_devel';
return $form;
}
protected function actionsElement(array $form, FormStateInterface $form_state) {
$actions = parent::actionsElement($form, $form_state);
unset($actions['delete']);
$actions['submit']['#value'] = $this
->t('Export');
$actions['reset']['#attributes']['style'] = 'display: none';
return $actions;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->setRedirect('webform_devel.schema.export', [
'webform' => $this
->getEntity()
->id(),
]);
}
}