LoadEntity.php 5.64 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\migrate_drupal\Plugin\migrate\load\LoadEntity.
6 7 8 9 10 11 12 13
 */

namespace Drupal\migrate_drupal\Plugin\migrate\load;

use Drupal\Component\Utility\String;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\migrate\Entity\MigrationInterface;
14
use Drupal\migrate\Exception\RequirementsException;
15 16 17
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\SourceEntityInterface;
use Drupal\migrate_drupal\Plugin\MigrateLoadInterface;
18
use Drupal\migrate_drupal\Plugin\CckFieldMigrateSourceInterface;
19 20 21 22

/**
 * Base class for entity load plugins.
 *
23 24
 * @ingroup migration
 *
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 * @PluginID("drupal_entity")
 */
class LoadEntity extends PluginBase implements MigrateLoadInterface {

  /**
   * The list of bundles being loaded.
   *
   * @var array
   */
  protected $bundles;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, MigrationInterface $migration) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
    $this->migration = $migration;
    $source_plugin = $this->migration->getSourcePlugin();
    if (!$source_plugin instanceof SourceEntityInterface) {
      throw new MigrateException('Migrations with a load plugin using LoadEntity should have an entity as source.');
    }
    if ($source_plugin->bundleMigrationRequired() && empty($configuration['bundle_migration'])) {
      throw new MigrateException(String::format('Source plugin @plugin requires the bundle_migration key to be set.', array('@plugin' => $source_plugin->getPluginId())));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function load(EntityStorageInterface $storage, $sub_id) {
    $entities = $this->loadMultiple($storage, array($sub_id));
    return isset($entities[$sub_id]) ? $entities[$sub_id] : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function loadMultiple(EntityStorageInterface $storage, array $sub_ids = NULL) {
    if (isset($this->configuration['bundle_migration'])) {
      /** @var \Drupal\migrate\Entity\MigrationInterface $bundle_migration */
      $bundle_migration = $storage->load($this->configuration['bundle_migration']);
      $source_id = array_keys($bundle_migration->getSourcePlugin()->getIds())[0];
      $this->bundles = array();
      foreach ($bundle_migration->getSourcePlugin()->getIterator() as $row) {
        $this->bundles[] = $row[$source_id];
      }
    }
    else {
      // This entity type has no bundles ('user', 'feed', etc).
      $this->bundles = array($this->migration->getSourcePlugin()->entityTypeId());
    }
    $sub_ids_to_load = isset($sub_ids) ? array_intersect($this->bundles, $sub_ids) : $this->bundles;
    $migrations = array();
    foreach ($sub_ids_to_load as $id) {
      $values = $this->migration->toArray();
      $values['id'] = $this->migration->id() . ':' . $id;
      $values['source']['bundle'] = $id;
      /** @var \Drupal\migrate_drupal\Entity\MigrationInterface $migration */
      $migration = $storage->create($values);
84 85
      try {
        $migration->getSourcePlugin()->checkRequirements();
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

        $source_plugin = $migration->getSourcePlugin();
        // Discuss simplifying per field type processing.
        // @see https://www.drupal.org/node/2395993
        if ($source_plugin instanceof CckFieldMigrateSourceInterface) {
          foreach ($source_plugin->fieldData() as $field_name => $data) {
            // Specifically process the link field until core is fixed.
            // @see https://www.drupal.org/node/2235457
            if ($data['type'] == 'link') {
              $migration->process[$field_name] = [
                'plugin' => 'd6_cck_link',
                'source' => [
                  $field_name,
                  $field_name . '_title',
                  $field_name . '_attributes',
                ],
              ];
            }
104 105 106 107 108 109 110 111 112 113
            elseif ($data['type'] === 'filefield') {
              $migration->process[$field_name] = [
                'plugin' => 'd6_cck_file',
                'source' => [
                  $field_name,
                  $field_name . '_list',
                  $field_name . '_data',
                ],
              ];
            }
114
            elseif ($data['type'] === 'text') {
115 116 117 118 119 120
              // The data is stored differently depending on whether we're using
              // db storage.
              $value_key = $data['db_storage'] ? $field_name : "$field_name/value";
              $format_key = $data['db_storage'] ? $field_name . '_format' : "$field_name/format" ;

              $migration->process["$field_name/value"] = $value_key;
121 122
              // See d6_user, signature_format for an example of the YAML that
              // represents this process array.
123
              $migration->process["$field_name/format"] = [
124 125 126
                [
                  'plugin' => 'static_map',
                  'bypass' => TRUE,
127
                  'source' => $format_key,
128 129 130 131 132 133
                  'map' => [0 => NULL]
                ],
                ['plugin' => 'skip_process_on_empty'],
                [
                  'plugin' => 'migration',
                  'migration' => 'd6_filter_format',
134
                  'source' => $format_key,
135 136 137 138
                  'no_stub' => 1,
                ],
              ];
            }
139 140 141 142 143 144 145 146 147
            else {
              $migration->process[$field_name] = $field_name;
            }
          }
        }
        else {
          $fields = array_keys($migration->getSourcePlugin()->fields());
          $migration->process += array_combine($fields, $fields);
        }
148 149
        $migrations[$migration->id()] = $migration;
      }
150 151 152
      catch (RequirementsException $e) {

      }
153 154 155 156 157 158
    }

    return $migrations;
  }

}