public function GetServices::checkFieldWidget in Simple Node Importer 8
Checks the widget type of each field.
File
- src/
Services/ GetServices.php, line 279
Class
Namespace
Drupal\simple_node_importer\ServicesCode
public function checkFieldWidget($field_names, $data, $node, $entity_type) {
$excludeFieldArr = [
'name',
'mail',
'status',
'roles',
'nid',
'type',
'uid',
'title',
];
$flag = TRUE;
foreach ($field_names as $field_machine_name) {
if (!in_array($field_machine_name, $excludeFieldArr)) {
$field_info = FieldStorageConfig::loadByName($entity_type, $field_machine_name);
$entityManager = \Drupal::service('entity_field.manager');
$field_definition = $entityManager
->getFieldDefinitions($entity_type, $data['type']);
$fieldStorageDefinition = $entityManager
->getFieldStorageDefinitions($entity_type, $data['type']);
$fieldProperties = $field_definition[$field_machine_name];
$fieldType = $field_info
->getType();
$fieldIsRequired = $fieldProperties
->isRequired();
if ($fieldType == 'entity_reference') {
$fieldSetting = $field_info
->getSetting('target_type');
}
elseif ($fieldType == 'datetime') {
$fieldSetting = $field_info
->getSetting('datetime_type');
}
else {
$fieldSetting = NULL;
}
$dataValidated = $this
->getFieldValidation($fieldType, $data[$field_machine_name], $fieldIsRequired);
if ($dataValidated) {
switch ($fieldType) {
case 'email':
$node[$field_machine_name] = $this
->buildNodeData($data[$field_machine_name], $fieldType);
break;
case 'image':
case 'file':
$node[$field_machine_name] = $this
->buildNodeData($data[$field_machine_name], $fieldType);
break;
case 'entity_reference':
if (!empty($data[$field_machine_name])) {
$preparedData = $this
->prepareEntityReferenceFieldData($field_definition, $field_machine_name, $data, $node, $fieldSetting);
if ($preparedData === FALSE) {
$flag = FALSE;
break;
}
else {
$node[$field_machine_name] = $preparedData;
}
}
break;
case 'text':
case 'string':
case 'string_long':
case 'text_long':
case 'text_with_summary':
$node[$field_machine_name] = $this
->buildNodeData($data[$field_machine_name], $fieldType);
break;
case 'boolean':
$node[$field_machine_name] = $data[$field_machine_name] == 1 ? $data[$field_machine_name] : (strtolower($data[$field_machine_name]) == 'y' ? 1 : 0);
break;
case 'datetime':
$dateValue = $this
->buildNodeData($data[$field_machine_name], $fieldType, $fieldSetting);
if ($dateValue) {
$node[$field_machine_name] = $dateValue;
}
elseif ($dateValue === FALSE) {
$flag = FALSE;
break;
}
break;
case 'number_integer':
case 'number_float':
case 'link':
$node[$field_machine_name] = $this
->buildNodeData($data[$field_machine_name], $fieldType, $fieldSetting);
break;
case 'list_text':
case 'list_string':
case 'list_float':
case 'list_integer':
$allowed_values = options_allowed_values($fieldStorageDefinition[$field_machine_name]);
if (is_array($data[$field_machine_name])) {
foreach ($data[$field_machine_name] as $k => $field_value) {
$key_value = array_search($field_value, $allowed_values, TRUE);
if ($key_value) {
$node[$field_machine_name][$k]['value'] = $key_value;
}
else {
$flag = FALSE;
break;
}
}
}
else {
$key_value = array_search(strtolower($data[$field_machine_name]), array_map('strtolower', $allowed_values));
if ($key_value) {
$node[$field_machine_name][0]['value'] = $key_value;
}
elseif (!empty($data[$field_machine_name])) {
$flag = FALSE;
break;
}
}
break;
}
// End of switch case.
}
else {
$flag = FALSE;
break;
}
}
// end of 1st if
}
// end of foreach
if ($flag === FALSE) {
$node = [];
$node['result'] = $data;
return $node;
}
else {
return $node;
}
}