You are here

public static function ConvertNodes::addNewFields in Convert Nodes 8

File

src/ConvertNodes.php, line 219

Class

ConvertNodes
ConvertNodes.

Namespace

Drupal\convert_nodes

Code

public static function addNewFields($nids, $limit, $map_fields, &$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($nids);
  }
  $current_nids = array_slice($nids, $context['sandbox']['current_id'], $limit, TRUE);
  foreach ($current_nids as $key => $nid) {
    $node = Node::load($nid);
    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 ($node->{$map_from}) {

        // Because might be target_id.
        $val_name = $node->{$map_from}
          ->getFieldDefinition()
          ->getFieldStorageDefinition()
          ->getMainPropertyName();
        $value = $node->{$map_from}->{$val_name};

        // 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 = $node->{$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'])) {
            $node
              ->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) {
                $node
                  ->get($field['field'])
                  ->appendItem($value);
              }
              array_unshift($field['value']['target_id'], $first_value);
            }
          }
          else {
            $node
              ->get($field['field'])
              ->setValue($field['value']);
          }
        }
      }
      elseif ($map_to['field'] == 'append_to_body') {
        $body = $node
          ->get('body')
          ->getValue()[0];
        $markup = Markup::create($body['value'] . '<strong>' . $map_to['from_label'] . '</strong><p>' . $value . '</p>');
        $node
          ->get('body')
          ->setValue([
          [
            'value' => $markup,
            'summary' => $body['summary'],
            'format' => $body['format'],
          ],
        ]);
      }
      elseif (!empty($value)) {
        $val_name = $node->{$map_to}['field']
          ->getFieldDefinition()
          ->getFieldStorageDefinition()
          ->getMainPropertyName();
        $node
          ->get($map_to['field'])
          ->setValue([
          [
            $val_name => $value,
          ],
        ]);
      }
    }
    $node
      ->save();
    $context['results'][] = $nid;
    $context['sandbox']['progress']++;
    $context['sandbox']['current_id'] = $key;
    $context['message'] = t('Adding fields for node @node of @total.', [
      '@node' => $key + 1,
      '@total' => $context['sandbox']['max'],
    ]);
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}