TextFieldMapping.php in Webform Content Creator 3.x
File
src/Plugin/WebformContentCreator/FieldMapping/TextFieldMapping.php
View source
<?php
namespace Drupal\webform_content_creator\Plugin\WebformContentCreator\FieldMapping;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_content_creator\Plugin\FieldMappingBase;
use Drupal\webform_content_creator\Plugin\FieldMappingInterface;
use Drupal\webform_content_creator\WebformContentCreatorInterface;
class TextFieldMapping extends FieldMappingBase {
public function getSupportedWebformFields($webform_id) {
$supported_types = array_merge([
"string",
"string_long",
"tel",
], FieldMappingInterface::WEBFORM_TEXT_ELEMENTS, FieldMappingInterface::WEBFORM_OPTIONS_ELEMENTS);
return $this
->filterWebformFields($webform_id, $supported_types);
}
public function mapEntityField(ContentEntityInterface &$content, array $webform_element, array $data = [], FieldDefinitionInterface $field_definition) {
$field_id = $field_definition
->getName();
$field_value = $data[$field_id];
$max_length = $this
->checkMaxFieldSizeExceeded($field_definition, $field_value);
if ($max_length === 0) {
$content
->set($field_id, $field_value);
}
else {
$content
->set($field_id, substr($field_value, 0, $max_length));
}
}
protected function checkMaxFieldSizeExceeded(FieldDefinitionInterface $field_definition, $value = "") {
$field_settings = $field_definition
->getSettings();
if (empty($field_settings) || !array_key_exists('max_length', $field_settings)) {
return 0;
}
$max_length = $field_settings['max_length'];
if (empty($max_length)) {
return 0;
}
if ($max_length < strlen($value)) {
\Drupal::logger(WebformContentCreatorInterface::WEBFORM_CONTENT_CREATOR)
->notice($this
->t('Problem: Field max length exceeded (truncated).'));
return $max_length;
}
return strlen($value);
}
}