You are here

class D7NodeDeriver in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/src/Plugin/migrate/D7NodeDeriver.php \Drupal\node\Plugin\migrate\D7NodeDeriver

Deriver for Drupal 7 node and node revision migrations based on node types.

Hierarchy

Expanded class hierarchy of D7NodeDeriver

5 string references to 'D7NodeDeriver'
d7_node.yml in core/modules/node/migrations/d7_node.yml
core/modules/node/migrations/d7_node.yml
d7_node_complete.yml in core/modules/node/migrations/d7_node_complete.yml
core/modules/node/migrations/d7_node_complete.yml
d7_node_entity_translation.yml in core/modules/content_translation/migrations/d7_node_entity_translation.yml
core/modules/content_translation/migrations/d7_node_entity_translation.yml
d7_node_revision.yml in core/modules/node/migrations/d7_node_revision.yml
core/modules/node/migrations/d7_node_revision.yml
d7_node_translation.yml in core/modules/content_translation/migrations/d7_node_translation.yml
core/modules/content_translation/migrations/d7_node_translation.yml

File

core/modules/node/src/Plugin/migrate/D7NodeDeriver.php, line 16

Namespace

Drupal\node\Plugin\migrate
View source
class D7NodeDeriver extends DeriverBase implements ContainerDeriverInterface {
  use MigrationDeriverTrait;

  /**
   * The base plugin ID this derivative is for.
   *
   * @var string
   */
  protected $basePluginId;

  /**
   * Whether or not to include translations.
   *
   * @var bool
   */
  protected $includeTranslations;

  /**
   * The migration field discovery service.
   *
   * @var \Drupal\migrate_drupal\FieldDiscoveryInterface
   */
  protected $fieldDiscovery;

  /**
   * D7NodeDeriver constructor.
   *
   * @param string $base_plugin_id
   *   The base plugin ID for the plugin ID.
   * @param bool $translations
   *   Whether or not to include translations.
   * @param \Drupal\migrate_drupal\FieldDiscoveryInterface $field_discovery
   *   The migration field discovery service.
   */
  public function __construct($base_plugin_id, $translations, FieldDiscoveryInterface $field_discovery) {
    $this->basePluginId = $base_plugin_id;
    $this->includeTranslations = $translations;
    $this->fieldDiscovery = $field_discovery;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {

    // Translations don't make sense unless we have content_translation.
    return new static($base_plugin_id, $container
      ->get('module_handler')
      ->moduleExists('content_translation'), $container
      ->get('migrate_drupal.field_discovery'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    if (in_array('translation', $base_plugin_definition['migration_tags']) && !$this->includeTranslations) {

      // Refuse to generate anything.
      return $this->derivatives;
    }
    $node_types = static::getSourcePlugin('d7_node_type');
    try {
      $node_types
        ->checkRequirements();
    } catch (RequirementsException $e) {

      // If the d7_node_type requirements failed, that means we do not have a
      // Drupal source database configured - there is nothing to generate.
      return $this->derivatives;
    }
    try {
      foreach ($node_types as $row) {
        $node_type = $row
          ->getSourceProperty('type');
        $values = $base_plugin_definition;
        $values['label'] = t('@label (@type)', [
          '@label' => $values['label'],
          '@type' => $row
            ->getSourceProperty('name'),
        ]);
        $values['source']['node_type'] = $node_type;
        $values['destination']['default_bundle'] = $node_type;

        // Comment status must be mapped to correct comment type.
        // Comment type migration creates a separate comment type for each
        // node type except for Forum which uses 'comment_forum'.
        $comment_type = 'comment_node_' . $node_type;
        if ($node_type == 'forum') {
          $comment_type = 'comment_forum';
        }
        $nested_key = $comment_type . '/0/status';
        $values['process'][$nested_key] = 'comment';

        // If this migration is based on the d7_node_revision migration or
        // is for translations of nodes, it should explicitly depend on the
        // corresponding d7_node variant.
        if ($base_plugin_definition['id'] == [
          'd7_node_revision',
        ] || in_array('translation', $base_plugin_definition['migration_tags'])) {
          $values['migration_dependencies']['required'][] = 'd7_node:' . $node_type;
        }

        /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
        $migration = \Drupal::service('plugin.manager.migration')
          ->createStubMigration($values);
        $this->fieldDiscovery
          ->addBundleFieldProcesses($migration, 'node', $node_type);
        $this->derivatives[$node_type] = $migration
          ->getPluginDefinition();
      }
    } catch (DatabaseExceptionWrapper $e) {

      // Once we begin iterating the source plugin it is possible that the
      // source tables will not exist. This can happen when the
      // MigrationPluginManager gathers up the migration definitions but we do
      // not actually have a Drupal 7 source database.
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
D7NodeDeriver::$basePluginId protected property The base plugin ID this derivative is for.
D7NodeDeriver::$fieldDiscovery protected property The migration field discovery service.
D7NodeDeriver::$includeTranslations protected property Whether or not to include translations.
D7NodeDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
D7NodeDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
D7NodeDeriver::__construct public function D7NodeDeriver constructor.
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
MigrationDeriverTrait::getSourcePlugin public static function Returns a fully initialized instance of a source plugin.