You are here

function ContentExport::savingEntity in Content Export YAML 8

Save all Entity

1 call to ContentExport::savingEntity()
ContentExport::importEntity in src/ContentExport.php
Convert YAML to OBJECT

File

src/ContentExport.php, line 215

Class

ContentExport
Created by PhpStorm. User: USER Date: 11/13/18 Time: 2:04 PM

Namespace

Drupal\content_export_yaml

Code

function savingEntity($enity_clone, $entity) {
  $id_label = \Drupal::entityTypeManager()
    ->getDefinition($entity)
    ->getKey('id');
  $key_label = \Drupal::entityTypeManager()
    ->getDefinition($entity)
    ->getKey('label');
  $bundle_label = \Drupal::entityTypeManager()
    ->getDefinition($entity)
    ->getKey('bundle');
  if ($entity == "user" && $enity_clone
    ->id() == 1) {
    \Drupal::messenger()
      ->addMessage(t($entity . " root user " . $enity_clone
      ->label() . " uid=" . $enity_clone
      ->id() . " can not update "), 'status');
    return FALSE;
  }
  if ($bundle_label == "") {
    $filter = [
      $id_label => $enity_clone
        ->id(),
    ];
  }
  else {
    $filter = [
      $id_label => $enity_clone
        ->id(),
      $bundle_label => $enity_clone
        ->bundle(),
      $key_label => $enity_clone
        ->label(),
    ];
  }
  $entity_list = \Drupal::entityTypeManager()
    ->getStorage($entity)
    ->loadByProperties($filter);
  if (!empty($entity_list)) {
    $status = $enity_clone
      ->save();
    if ($status == 2) {
      if ($bundle_label == "") {
        $bundle_label = $entity;
      }
      else {
        $bundle_label = $enity_clone
          ->bundle();
      }
      \Drupal::messenger()
        ->addMessage(t($bundle_label . " with " . $id_label . "=" . $enity_clone
        ->id() . " update "), 'status');
    }
  }
  else {
    $enity_clone->{$id_label} = NULL;

    // Also handle modules that attach a UUID to the node.
    $enity_clone->uuid = \Drupal::service('uuid')
      ->generate();

    // Anyonmymous users don't have a name.
    $enity_clone->created = time();

    //$enity_clone->uid = 0;
    $status = $enity_clone
      ->save();
    if ($status == 1) {
      if ($bundle_label == "") {
        $bundle_label = $entity;
      }
      else {
        $bundle_label = $enity_clone
          ->bundle();
      }
      \Drupal::messenger()
        ->addMessage(t($bundle_label . " with " . $id_label . "=" . $enity_clone
        ->id() . " created "), 'status');
    }
  }
  return $status;
}