View source
<?php
namespace Drupal\webform_attachment\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Element\WebformMessage;
use Drupal\webform\Plugin\WebformElement\WebformDisplayOnTrait;
use Drupal\webform\Plugin\WebformElementAttachmentInterface;
use Drupal\webform\Plugin\WebformElementBase;
use Drupal\webform\Plugin\WebformElementDisplayOnInterface;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WebformAttachmentBase extends WebformElementBase implements WebformElementAttachmentInterface, WebformElementDisplayOnInterface {
use WebformDisplayOnTrait;
protected $conditionsValidator;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->conditionsValidator = $container
->get('webform_submission.conditions_validator');
return $instance;
}
protected function defineDefaultProperties() {
return [
'title' => '',
'title_display' => '',
'display_on' => WebformElementDisplayOnInterface::DISPLAY_ON_NONE,
'filename' => '',
'sanitize' => FALSE,
'link_title' => '',
'trim' => FALSE,
'download' => FALSE,
'wrapper_attributes' => [],
'label_attributes' => [],
] + $this
->defineDefaultBaseProperties();
}
protected function defineDefaultBaseProperties() {
$properties = parent::defineDefaultBaseProperties();
unset($properties['prepopulate'], $properties['states_clear']);
return $properties;
}
protected function defineTranslatableProperties() {
return array_merge(parent::defineTranslatableProperties(), [
'filename',
'link_title',
]);
}
protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$attachment_element = $this
->getFormElementClassDefinition();
$format = $this
->getItemFormat($element);
switch ($format) {
case 'name':
return $attachment_element::getFileName($element, $webform_submission);
case 'url':
return $attachment_element::getFileUrl($element, $webform_submission)
->toString();
default:
case 'link':
return $attachment_element::getFileLink($element, $webform_submission);
}
}
protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$attachment_element = $this
->getFormElementClassDefinition();
$format = $this
->getItemFormat($element);
switch ($format) {
case 'name':
return $attachment_element::getFileName($element, $webform_submission);
default:
case 'link':
case 'url':
$fileUrl = $attachment_element::getFileUrl($element, $webform_submission);
return $fileUrl ? $fileUrl
->toString() : '';
}
}
public function hasValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
return TRUE;
}
public function getItemFormats() {
return [
'link' => $this
->t('File link'),
'name' => $this
->t('File name'),
'url' => $this
->t('File URL'),
];
}
public function getItemDefaultFormat() {
return 'link';
}
public function preSave(array &$element, WebformSubmissionInterface $webform_submission) {
$key = $element['#webform_key'];
$data = $webform_submission
->getData();
if (isset($data[$key])) {
unset($data[$key]);
$webform_submission
->setData($data);
}
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['attachment'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Attachment settings'),
];
$form['attachment']['message'] = [
'#type' => 'webform_message',
'#message_message' => $this
->t("Please make sure to enable 'Include files as attachments' for each email handler that you want to send this attachment."),
'#message_type' => 'warning',
'#message_close' => TRUE,
'#message_storage' => WebformMessage::STORAGE_SESSION,
'#access' => TRUE,
];
$form['attachment']['display_on'] = [
'#type' => 'select',
'#title' => $this
->t('Display on'),
'#options' => $this
->getDisplayOnOptions(TRUE),
];
$form['attachment']['display_on_message'] = [
'#type' => 'webform_message',
'#message_message' => $this
->t("The attachment's link will only be diplayed on the form after the submission is completed."),
'#message_type' => 'warning',
'#access' => TRUE,
'#states' => [
'visible' => [
[
':input[name="properties[display_on]"]' => [
'value' => 'form',
],
],
'or',
[
':input[name="properties[display_on]"]' => [
'value' => 'both',
],
],
],
],
];
$form['attachment']['filename'] = [
'#type' => 'textfield',
'#title' => $this
->t('File name'),
'#description' => $this
->t("Please enter the attachment's file name with a file extension. The file extension will be used to determine the attachment's content (mime) type."),
'#maxlength' => NULL,
];
$form['attachment']['link_title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Link title'),
'#description' => $this
->t('Enter the title to be displayed when the attachment is displayed as a link.'),
];
$form['attachment']['trim'] = [
'#type' => 'checkbox',
'#return_value' => TRUE,
'#title' => $this
->t("Remove whitespace around the attachment's content"),
'#description' => $this
->t("If checked, all spaces and returns around the attachment's content with be removed."),
'#weight' => 10,
];
$form['attachment']['sanitize'] = [
'#type' => 'checkbox',
'#return_value' => TRUE,
'#title' => $this
->t('Sanitize file name'),
'#description' => $this
->t('If checked, file name will be transliterated, lower-cased and all special characters converted to dashes (-).'),
'#weight' => 10,
];
$form['attachment']['download'] = [
'#type' => 'checkbox',
'#return_value' => TRUE,
'#title' => $this
->t('Force users to download the attachment'),
'#description' => $this
->t('If checked the attachment will be automatically download.'),
'#weight' => 10,
];
$form['attachment']['tokens'] = [
'#access' => TRUE,
'#weight' => 10,
] + $this->tokenManager
->buildTreeElement();
$form['conditional_logic']['states_attachment'] = [
'#type' => 'webform_message',
'#message_message' => t('Disabled attachments will not be included as file attachments in sent emails.'),
'#message_type' => 'warning',
'#message_close' => TRUE,
'#message_storage' => WebformMessage::STORAGE_SESSION,
'#access' => TRUE,
];
return $form;
}
public function getTestValues(array $element, WebformInterface $webform, array $options = []) {
return NULL;
}
public function getEmailAttachments(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
if (!$this->conditionsValidator
->isElementEnabled($element, $webform_submission)) {
return [];
}
$attachment_element = $this
->getFormElementClassDefinition();
$file_content = $attachment_element::getFileContent($element, $webform_submission);
$file_name = $attachment_element::getFileName($element, $webform_submission);
$file_mime = $attachment_element::getFileMimeType($element, $webform_submission);
$file_url = $attachment_element::getFileUrl($element, $webform_submission);
$attachments = [];
if ($file_name && $file_content && $file_mime) {
$attachments[] = [
'filecontent' => $file_content,
'filename' => $file_name,
'filemime' => $file_mime,
'_fileurl' => $file_url ? $file_url
->toString() : NULL,
];
}
return $attachments;
}
public function getExportAttachments(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
return $this
->getEmailAttachments($element, $webform_submission, $options);
}
public function hasExportAttachments() {
return TRUE;
}
public function getExportAttachmentsBatchLimit() {
return NULL;
}
}