You are here

public static function ConvertBundles::addNewFields in Convert Bundles 8

File

src/ConvertBundles.php, line 228

Class

ConvertBundles
ConvertBundles.

Namespace

Drupal\convert_bundles

Code

public static function addNewFields($entity_type, $ids, $limit, $map_fields, $fields_to, $entities, &$context) {
  if (empty($context['sandbox'])) {

    // Flush cache so we recognize new bundle type before updates.
    drupal_flush_all_caches();
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_id'] = 0;
    $context['sandbox']['max'] = count($ids);
  }
  $current_ids = array_slice($ids, $context['sandbox']['current_id'], $limit, TRUE);
  foreach ($current_ids as $key => $id) {
    $old_entity = $entities[$id];
    $entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->load($id);
    foreach ($map_fields as $map_from => $map_to) {
      if (isset($map_to['field']) && $map_to['field'] == 'remove') {
        continue;
      }
      $value = '';

      // TODO Need to get multiple values.
      if ($old_entity
        ->hasField($map_from)) {

        // Because might be target_id.
        $val_name = $old_entity
          ->get($map_from)
          ->getFieldDefinition()
          ->getFieldStorageDefinition()
          ->getMainPropertyName();
        $value = $old_entity
          ->get($map_from)->{$val_name};
        if ($map_to['field'] != 'append_to_body') {

          // Because datetime/date may need converting
          // TODO date with time did not insert into date only fields
          // need to test if date without time will insert into date with time
          // or better yet, find a better way to do this.
          $from_type = $old_entity
            ->get($map_from)
            ->getFieldDefinition()
            ->getFieldStorageDefinition()
            ->getType();
          $to_type = $fields_to[$map_to['field']];
          if (!empty($to_type) && in_array('datetime', [
            $to_type,
            $from_type,
          ])) {
            $date = new \DateTime($value);
            $value = $date
              ->format('Y-m-d');
          }
        }
      }
      if ($map_from == 'create_new') {
        foreach ($map_to as $field) {
          if (isset($field['value']['target_id'])) {
            $entity
              ->get($field['field'])
              ->setValue($field['value']['target_id'][0]);
            if (count($field['value']['target_id']) > 1) {
              $first_value = array_shift($field['value']['target_id']);
              foreach ($field['value']['target_id'] as $value) {
                $entity
                  ->get($field['field'])
                  ->appendItem($value);
              }
              array_unshift($field['value']['target_id'], $first_value);
            }
          }
          else {
            $entity
              ->get($field['field'])
              ->setValue($field['value']);
          }
        }
      }
      elseif ($map_to['field'] == 'append_to_body') {
        $body = $entity
          ->get('body')
          ->getValue()[0];
        $markup = Markup::create($body['value'] . '<strong>' . $map_to['from_label'] . '</strong><p>' . $value . '</p>');
        $entity
          ->get('body')
          ->setValue([
          [
            'value' => $markup,
            'summary' => $body['summary'],
            'format' => $body['format'],
          ],
        ]);
      }
      elseif (!empty($value)) {
        $entity
          ->set($map_to['field'], $old_entity
          ->get($map_from)
          ->getValue());
      }
    }
    $entity
      ->save();
    $context['results'][] = $id;
    $context['sandbox']['progress']++;
    $context['sandbox']['current_id'] = $key;
    $context['message'] = t('Adding fields for entity @entity of @total.', [
      '@entity' => $key + 1,
      '@total' => $context['sandbox']['max'],
    ]);
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}