You are here

public function GetServices::snpGetFields in Simple Node Importer 8

1 call to GetServices::snpGetFields()
GetServices::snpGetFieldList in src/Services/GetServices.php

File

src/Services/GetServices.php, line 236

Class

GetServices

Namespace

Drupal\simple_node_importer\Services

Code

public function snpGetFields($fieldsManager, $type, $entity_type = NULL) {
  if ($entity_type == 'node') {
    $defaultFieldArr = [
      'title',
      'body',
      'status',
      'uid',
    ];
  }
  else {
    $defaultFieldArr = [
      'name',
      'mail',
      'status',
      'roles',
      'user_picture',
    ];
  }
  $haystack = 'field_';
  foreach ($fieldsManager as $key => $field) {
    if (in_array($key, $defaultFieldArr) || strpos($key, $haystack) !== FALSE) {
      if ($type == 'csv') {
        if (method_exists($field
          ->getLabel(), 'render')) {
          $fieldsArr[$key] = $field
            ->getLabel()
            ->render();
        }
        else {
          $fieldsArr[$key] = $field
            ->getLabel();
        }
      }
      elseif ($type == 'import') {

        // Fetch the list of required fields.
        if ($fieldsManager[$key]
          ->isRequired()) {
          $fieldsArr['required'][$key] = $key;
        }

        // Fetch the list of multivalued fields.
        if (!in_array($key, $defaultFieldArr)) {
          $fieldStorageConfig = FieldStorageConfig::loadByName($entity_type, $key);
          if ($fieldStorageConfig
            ->getCardinality() === -1 || $fieldStorageConfig
            ->getCardinality() > 1) {
            $fieldsArr['multivalued'][$key] = $key;
          }
        }
      }
      elseif ($type == 'mapping') {
        $fieldsArr[$key] = $field;
      }
    }
  }
  return $fieldsArr;
}