You are here

public static function GetServices::generateFieldSetValue in Simple Node Importer 8

1 call to GetServices::generateFieldSetValue()
simple_node_importer_form_alter in ./simple_node_importer.module
Implements hook_form_alter().

File

src/Services/GetServices.php, line 826

Class

GetServices

Namespace

Drupal\simple_node_importer\Services

Code

public static function generateFieldSetValue($fieldKey, $fieldVal, $fieldWidget, $entity_type, $bundle) {
  $excludeFieldArr = [
    'type',
    'nid',
    'uid',
    'title',
    'reference',
    'status',
    'name',
    'mail',
    'roles',
  ];
  $flag = TRUE;
  $key = 0;
  if (!in_array($fieldKey, $excludeFieldArr)) {
    $getFieldInfo = GetServices::getFieldInfo($entity_type, $fieldKey, $bundle);
    $fieldType = $getFieldInfo['fieldType'];
    $fieldIsRequired = $getFieldInfo['fieldIsRequired'];
    $fieldCardinality = $getFieldInfo['fieldCardinality'];
    if (empty($fieldVal) && $fieldIsRequired) {
      $fields[] = $fieldKey;
    }
    switch ($fieldType) {
      case 'text_with_summary':

        // code...
        if ($fieldIsRequired && empty($fieldVal)) {
          $fields[] = $fieldKey;
        }
        else {
          $fieldWidget[0]['#default_value'] = $fieldVal;
        }
        break;
      case 'list_float':
      case 'list_integer':
      case 'list_string':
        if (!empty($fieldVal) && ($fieldCardinality == -1 || $fieldCardinality > 1) && is_array($fieldVal)) {
          $flag = 0;
          foreach ($fieldVal as $value) {

            // code...
            if (!empty($value)) {
              $flag = 1;
              $fieldWidget['#default_value'][] = $value;
            }
          }
          if ($fieldIsRequired && $flag == 0) {
            $fields[] = $fieldKey;
          }
        }
        elseif (!empty($fieldVal)) {
          $fieldWidget['#default_value'] = $fieldVal;
        }
        elseif ($fieldIsRequired && empty($fieldVal)) {
          $fields[] = $fieldKey;
        }
        break;
      case 'boolean':

        // code...
        if ($fieldIsRequired && empty($fieldVal)) {
          $fields[] = $fieldKey;
        }
        else {
          $fieldWidget['value']['#default_value'] = $fieldVal;
        }
        break;
      case 'entity_reference':

        // code...
        if ($fieldWidget[0]['target_id']['#selection_settings']['target_bundles']) {
          $target_bundle = key($fieldWidget[0]['target_id']['#selection_settings']['target_bundles']);
        }
        else {
          $target_bundle = '';
        }
        $target_type = $fieldWidget[0]['target_id']['#target_type'];
        if ($target_type == "taxonomy_term") {
          $flag = 0;
          if (is_array($fieldVal) && !empty($fieldVal)) {
            foreach ($fieldVal as $termName) {
              if ($termName) {

                // code...
                $flag = 1;
                $termArray = [
                  'name' => $termName,
                  'vid' => $target_bundle,
                ];
                $taxos_obj = \Drupal::entityManager()
                  ->getStorage('taxonomy_term')
                  ->loadByProperties($termArray);
                $refObject[] = key($taxos_obj);
              }
            }
            if ($flag == 0 && $fieldIsRequired) {
              $fields[] = $fieldKey;
            }
          }
          elseif (!empty($fieldVal)) {

            // code...
            $termArray = [
              'name' => $fieldVal,
              'vid' => $target_bundle,
            ];
            $taxos_obj = \Drupal::entityManager()
              ->getStorage('taxonomy_term')
              ->loadByProperties($termArray);
            $refObject = key($taxos_obj);
          }
          elseif ($fieldIsRequired && empty($refObject)) {
            $fields[] = $fieldKey;
          }
        }
        elseif ($target_type == "user") {
          $flag = 0;
          if (is_array($fieldVal) && !empty($fieldVal)) {
            foreach ($fieldVal as $userData) {
              if (filter_var($userData, FILTER_VALIDATE_EMAIL) && !empty($userData)) {

                // code...
                $user = user_load_by_mail($userData);
                if (!empty($user)) {
                  $userObject[] = $user
                    ->id();
                  $flag = 1;
                }
              }
              elseif (!empty($userData)) {
                $user = user_load_by_name($userData);
                if (!empty($user)) {
                  $userObject[] = $user
                    ->id();
                  $flag = 1;
                }
              }
              else {
                $fields[] = $fieldKey;
              }
            }
            if ($flag == 0 && $fieldIsRequired) {
              $fields[] = $fieldKey;
            }
          }
          elseif (!empty($fieldVal)) {
            if (filter_var($fieldVal, FILTER_VALIDATE_EMAIL)) {

              // code...
              $user = user_load_by_mail($fieldVal);
              if (!empty($user)) {
                $userObject = $user
                  ->id();
              }
            }
            else {
              $user = user_load_by_name($fieldVal);
              if ($user != FALSE) {
                $userObject[] = $user
                  ->id();
              }
              else {
                $fields[] = $fieldKey;
              }
            }
          }
          if ($fieldIsRequired && !empty($fieldVal)) {
            $fields[] = $fieldKey;
          }
        }
        if (!empty($refObject) && ($fieldCardinality == -1 || $fieldCardinality > 1) && $target_type == "taxonomy_term") {
          foreach ($refObject as $refVal) {

            // code...
            $fieldWidget[$key] = $fieldWidget[0];
            $fieldWidget[$key++]['target_id']['#default_value'] = Term::load($refVal);
          }
        }
        elseif (!empty($refObject)) {
          $fieldWidget[0]['target_id']['#default_value'] = Term::load($refObject);
        }
        if (!empty($userObject) && ($fieldCardinality == -1 || $fieldCardinality > 1) && $target_type == "user") {
          foreach ($userObject as $userVal) {

            // code...
            $fieldWidget[$key] = $fieldWidget[0];
            $fieldWidget[$key++]['target_id']['#default_value'] = User::load($userVal);
          }
        }
        elseif (!empty($userObject)) {
          $fieldWidget[0]['target_id']['#default_value'] = User::load($userObject);
        }
        break;
      case 'datetime':

        // code...
        $dateFormat = $fieldWidget[0]['value']['#date_date_format'];
        $timeFormat = $fieldWidget[0]['value']['#date_time_format'];
        if (!empty($fieldVal) && ($fieldCardinality == -1 || $fieldCardinality > 1) && is_array($fieldVal)) {
          $flag = 0;
          foreach ($fieldVal as $date) {
            if (!empty($date)) {
              if (GetServices::validateDateExpression($date) && !empty($dateFormat) && !empty($timeFormat)) {
                $date = date_create($date);
                $dateTime = DrupalDateTime::createFromDateTime($date);
                $fieldWidget[0]['value']['#default_value'] = $dateTime;
                $flag = 1;
              }
              elseif (GetServices::validateDateExpression($date) && !empty($dateFormat) && empty($timeFormat)) {
                $date = date_create($date);
                $dateTime = DrupalDateTime::createFromDateTime($date);
                $fieldWidget[0]['value']['#default_value'] = $dateTime;
                $flag = 1;
              }
              else {
                $fields[] = $fieldKey;
              }
            }
          }
          if ($flag == 0 && $fieldIsRequired) {
            $fields[] = $fieldKey;
          }
        }
        elseif (!empty($fieldVal)) {
          if (GetServices::validateDateExpression($fieldVal) && !empty($dateFormat) && !empty($timeFormat)) {
            $date = date_create($fieldVal);
            $dateTime = DrupalDateTime::createFromDateTime($date);
            $fieldWidget[0]['value']['#default_value'] = $dateTime;
          }
          elseif (GetServices::validateDateExpression($fieldVal) && !empty($dateFormat) && empty($timeFormat)) {
            $date = date_create($fieldVal);
            $dateTime = DrupalDateTime::createFromDateTime($date);
            $fieldWidget[0]['value']['#default_value'] = $dateTime;
          }
          else {
            $fields[] = $fieldKey;
          }
        }
        elseif ($flag == 0 && $fieldIsRequired) {
          $fields[] = $fieldKey;
        }
        break;
      case 'string':
        $flag = 0;
        if (!empty($fieldVal) && ($fieldCardinality == -1 || $fieldCardinality > 1) && is_array($fieldVal)) {
          foreach ($fieldVal as $value) {

            // code...
            if (!empty($value)) {
              $fieldWidget[$key] = $fieldWidget[0];
              $fieldWidget[$key++]['value']['#default_value'][] = $value;
              $flag = 1;
            }
          }
          if ($flag == 0 && $fieldIsRequired) {
            $fields[] = $fieldKey;
          }
        }
        elseif (!empty($fieldVal)) {
          $fieldWidget[0]['value']['#default_value'] = $fieldVal;
        }
        elseif ($fieldIsRequired && empty($fieldVal)) {
          $fields[] = $fieldKey;
        }
        break;
      case 'file':
      case 'image':

        // code...
        $flag = 0;
        if (!empty($fieldVal) && ($fieldCardinality == -1 || $fieldCardinality > 1) && is_array($fieldVal)) {
          foreach ($fieldVal as $file) {

            // code...
            if (!empty($file)) {
              if (filter_var($file, FILTER_VALIDATE_URL)) {
                $fid = GetServices::getImageFid($file);
                if (!empty($fid)) {
                  $fieldWidget[$key] = $fieldWidget[0];
                  $fieldWidget[$key++]['#default_value']['fids'][] = $fid;
                  $flag = 1;
                }
                else {
                  $fields[] = $fieldKey;
                }
              }
              else {
                $fields[] = $fieldKey;
              }
            }
          }
          if ($flag == 0 && $fieldIsRequired) {
            $fields[] = $fieldKey;
          }
        }
        elseif (!empty($fieldVal)) {
          if (filter_var($fieldVal, FILTER_VALIDATE_URL)) {
            $fid = GetServices::getImageFid($fieldVal);
            if (!empty($fid)) {
              $fieldWidget[0]['#default_value']['fids'] = [
                $fid,
              ];
            }
          }
          else {
            $fields[] = $fieldKey;
          }
        }
        elseif ($fieldIsRequired && empty($fieldVal)) {
          $fields[] = $fieldKey;
        }
        break;
      case 'email':
        $flag = 0;
        if (!empty($fieldVal) && ($fieldCardinality == -1 || $fieldCardinality > 1) && is_array($fieldVal)) {
          foreach ($fieldVal as $email) {
            if ($email != '') {
              if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $flag = 1;
                $fieldWidget[$key] = $fieldWidget[0];
                $fieldWidget[$key++]['value']['#default_value'] = $email;
              }
              else {
                $fields[] = $fieldKey;
              }
            }
          }
          if ($flag == 0 && $fieldIsRequired) {
            $fields[] = $fieldKey;
          }
        }
        elseif (!empty($fieldVal)) {
          if (filter_var($fieldVal, FILTER_VALIDATE_EMAIL)) {

            // code..
            $fieldWidget[0]['value']['#default_value'] = $fieldVal;
          }
          else {
            $fields[] = $fieldKey;
          }
        }
        elseif ($fieldIsRequired && empty($fieldVal)) {
          $fields[] = $fieldKey;
        }
        break;
      case 'link':
        $flag = 0;
        if (!empty($fieldVal) && ($fieldCardinality == -1 || $fieldCardinality > 1) && is_array($fieldVal)) {
          foreach ($fieldVal as $link) {
            if (!empty($link) && filter_var($link, FILTER_VALIDATE_URL)) {

              // code..
              $flag = 1;
              $fieldWidget[$key] = $fieldWidget[0];
              $fieldWidget[$key++]['uri']['#default_value'][] = $link;
            }
          }
          if ($flag == 0 && $fieldIsRequired) {
            $fields[] = $fieldKey;
          }
        }
        elseif (!empty($fieldVal)) {
          if (!empty($fieldVal) && filter_var($fieldVal, FILTER_VALIDATE_URL)) {

            // code..
            $fieldWidget[0]['uri']['#default_value'] = $fieldVal;
          }
        }
        elseif ($fieldIsRequired && empty($fieldVal)) {
          $fields[] = $fieldKey;
        }
        break;
    }
  }
  else {
    if ($fieldKey == 'title' && !empty($fieldVal)) {
      $fieldWidget[0]['value']['#default_value'] = $fieldVal;
    }
    elseif ($fieldKey == 'title' && empty($fieldVal)) {
      $fields[] = $fieldKey;
    }

    // For user bundle type.
    if (in_array($fieldKey, [
      'name',
      'mail',
      'roles',
    ])) {
      if ($fieldKey == 'name' && empty($fieldVal)) {
        $fields[] = $fieldKey;
      }
      else {
        $fieldWidget['#default_value'] = $fieldVal;
      }
    }
    if ($fieldKey == 'uid') {
      if (!empty($fieldVal)) {
        $uname = user_load_by_name($fieldVal);
        $umail = user_load_by_mail($fieldVal);
        if ($uname != FALSE || $umail != FALSE) {
          $user = $uname != FALSE ? $uname : $umail;
          $fieldWidget[0]['target_id']['#default_value'] = User::load($user
            ->id());
        }
        else {
          $fields[] = $fieldKey;
        }
      }
      elseif (empty($fieldVal)) {
        $fields[] = $fieldKey;
      }
    }
  }
  if (!empty($fields)) {
    $result['fieldWidget'] = $fieldWidget;
    $result['bugField'] = $fields;
    return $result;
  }
  else {
    return $fieldWidget;
  }
}