You are here

public function GetServices::prepareEntityReferenceFieldData in Simple Node Importer 8

1 call to GetServices::prepareEntityReferenceFieldData()
GetServices::checkFieldWidget in src/Services/GetServices.php
Checks the widget type of each field.

File

src/Services/GetServices.php, line 514

Class

GetServices

Namespace

Drupal\simple_node_importer\Services

Code

public function prepareEntityReferenceFieldData($field_definition, $field_machine_name, $data, $node, $fieldSetting) {
  $flag = TRUE;
  $dataRow = [];
  if ($fieldSetting == 'taxonomy_term') {
    $handler_settings = $field_definition[$field_machine_name]
      ->getSetting('handler_settings');
    $target_bundles = $handler_settings['target_bundles'];
    $vocabulary_name = is_array($target_bundles) && count($target_bundles) > 1 ? $target_bundles : key($target_bundles);
    $allw_term = \Drupal::config('simple_node_importer.settings')
      ->get('simple_node_importer_allow_add_term');

    // Code for taxonomy data handling.
    if (is_array($vocabulary_name) && count($vocabulary_name) > 1 || empty($data[$field_machine_name])) {
      return $flag = FALSE;
    }
    else {
      if (is_array($data[$field_machine_name])) {
        foreach ($data[$field_machine_name] as $k => $term_name) {
          if ($term_name) {
            $termArray = [
              'name' => $term_name,
              'vid' => $vocabulary_name,
            ];
            $taxos_obj = \Drupal::entityManager()
              ->getStorage('taxonomy_term')
              ->loadByProperties($termArray);
            $termKey = key($taxos_obj);
            if (!$taxos_obj && $allw_term) {
              $term = Term::create([
                'vid' => $vocabulary_name,
                'name' => $term_name,
              ]);
              $term
                ->enforceIsNew();
              $term
                ->save();
              $dataRow[$k]['target_id'] = $term
                ->id();
            }
            else {
              $termObj = $taxos_obj[$termKey];
              $tid = $termObj
                ->id();
              $dataRow[$k]['target_id'] = $tid;
            }
          }
        }
      }
      else {
        $termArray = [
          'name' => $data[$field_machine_name],
          'vid' => $vocabulary_name,
        ];
        $taxos_obj = \Drupal::entityManager()
          ->getStorage('taxonomy_term')
          ->loadByProperties($termArray);
        $termKey = key($taxos_obj);
        if (!$taxos_obj && $allw_term) {
          $term = Term::create([
            'vid' => $vocabulary_name,
            'name' => $data[$field_machine_name],
          ]);
          $term
            ->enforceIsNew();
          $term
            ->save();
          $dataRow[0]['target_id'] = $term
            ->id();
        }
        else {
          $termObj = $taxos_obj[$termKey];
          $tid = $termObj
            ->id();
          $dataRow[0]['target_id'] = $tid;
        }
      }
    }
    return $dataRow;
  }
  if ($fieldSetting == 'user') {
    $userEmail = $data[$field_machine_name];
    if (is_array($userEmail)) {
      foreach ($userEmail as $email) {
        if ($email) {
          $flag = $this
            ->getFieldValidation('email', $email);
          if ($flag) {
            $user = $this
              ->getUserByEmail($email, 'content_validate');
            if ($user && !is_int($user)) {
              $dataRow[] = $user
                ->id();
            }
            else {
              return $flag = FALSE;
            }
          }
          else {
            $uid = $this
              ->getUserByUsername($email, 'content_validate');
            if ($uid) {
              $dataRow[] = $uid;
            }
            else {
              return $flag = FALSE;
            }
          }
        }
      }
    }
    elseif ($userEmail) {
      $flag = $this
        ->getFieldValidation('email', $userEmail);
      if ($flag) {
        $user = $this
          ->getUserByEmail($userEmail, 'content_validate');
        if ($user && !is_int($user)) {
          $dataRow = $user
            ->id();
        }
        else {
          return $flag = FALSE;
        }
      }
      else {
        $uid = $this
          ->getUserByUsername($userEmail, 'content_validate');
        if ($uid) {
          $dataRow = $uid;
        }
        else {
          return $flag = FALSE;
        }
      }
    }
    return $dataRow;
  }
}