View source
<?php
namespace Drupal\webform_entity_print_attachment\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Twig\WebformTwigExtension;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_attachment\Plugin\WebformElement\WebformAttachmentBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformEntityPrintAttachment extends WebformAttachmentBase {
protected $exportTypeManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->exportTypeManager = $container
->get('plugin.manager.entity_print.export_type');
return $instance;
}
protected function defineDefaultProperties() {
$properties = [
'view_mode' => 'html',
'template' => '',
] + parent::defineDefaultProperties();
unset($properties['trim']);
return $properties;
}
public function finalize(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
parent::finalize($element, $webform_submission);
list($element['#type'], $element['#export_type']) = explode(':', $element['#type']);
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$file_extension = $this
->getExportTypeFileExtension();
$t_args = [
'@extension' => $file_extension,
];
$form['attachment']['filename']['#description'] .= '<br/><br/>' . $this
->t('File name must include *.@extension file extension.', $t_args);
$form['attachment']['filename']['#pattern'] = '^.*\\.' . $file_extension . '$';
$form['attachment']['filename']['#pattern_error'] = $this
->t('File name must include *.@extension file extension.', $t_args);
WebformElementHelper::process($form['attachment']['filename']);
$form['attachment']['view_mode'] = [
'#type' => 'select',
'#title' => $this
->t('View mode'),
'#options' => [
'html' => $this
->t('HTML'),
'table' => $this
->t('Table'),
'twig' => $this
->t('Twig template…'),
],
];
$form['attachment']['template'] = [
'#type' => 'webform_codemirror',
'#title' => $this
->t('Twig template'),
'#title_display' => 'invisible',
'#mode' => 'twig',
'#states' => [
'visible' => [
':input[name="properties[view_mode]"]' => [
'value' => 'twig',
],
],
],
];
$form['attachment']['help'] = WebformTwigExtension::buildTwigHelp() + [
'#states' => [
'visible' => [
':input[name="properties[view_mode]"]' => [
'value' => 'twig',
],
],
],
];
WebformElementHelper::setPropertyRecursive($form['attachment']['help'], '#access', TRUE);
return $form;
}
protected function getExportTypeFileExtension() {
list(, $export_type_id) = explode(':', $this
->getPluginId());
$definition = $this->exportTypeManager
->getDefinition($export_type_id);
return $definition['file_extension'];
}
public function getExportAttachmentsBatchLimit() {
return 10;
}
}