FieldMappingBase.php in Webform Content Creator 3.x
File
src/Plugin/FieldMappingBase.php
View source
<?php
namespace Drupal\webform_content_creator\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\webform_content_creator\WebformContentCreatorUtilities;
abstract class FieldMappingBase extends PluginBase implements FieldMappingInterface {
public function getPlugin() {
return $this;
}
public function getId() {
return $this->pluginDefinition['id'];
}
public function getLabel() {
return $this->pluginDefinition['label'];
}
public function getWeight() {
return $this->pluginDefinition['weight'];
}
public function getFieldTypes() {
return $this->pluginDefinition['field_types'];
}
public function isGeneric() {
return empty($this->pluginDefinition['field_types']) ? TRUE : FALSE;
}
public function supportsCustomFields() {
return TRUE;
}
public function getEntityComponentFields(FieldDefinitionInterface $field_definition) {
return [];
}
public function getSupportedWebformFields($webform_id) {
return WebformContentCreatorUtilities::getWebformElements($webform_id);
}
public function mapEntityField(ContentEntityInterface &$content, array $webform_element, array $data = [], FieldDefinitionInterface $field_definition) {
$field_id = $field_definition
->getName();
$field_value = $data[$field_id];
$content
->set($field_id, $field_value);
}
protected function filterWebformFields($webform_id, array $supported_types, array $available_fields = NULL) {
if (!isset($available_fields)) {
$available_fields = WebformContentCreatorUtilities::getWebformElements($webform_id);
}
$webform_field_types = WebformContentCreatorUtilities::getWebformElementsTypes($webform_id);
$allowed_fields = [];
foreach ($available_fields as $key => $available_field) {
$key_parts = explode(',', $key);
if (sizeOf($key_parts) > 1) {
$element_type = $webform_field_types[$key_parts[1]];
if ($key_parts[0] == "1") {
$element_type = $element_type['type'];
}
if (in_array($element_type, $supported_types)) {
$allowed_fields[$key] = $available_field;
}
}
else {
$retval = $this
->filterWebformFields($webform_id, $supported_types, $available_field);
if (!empty($retval)) {
$allowed_fields[$key] = $retval;
}
}
}
return $allowed_fields;
}
}