class Node

Same name in this branch
  1. 9 core/modules/node/src/Entity/Node.php \Drupal\node\Entity\Node
  2. 9 core/modules/node/src/Plugin/views/field/Node.php \Drupal\node\Plugin\views\field\Node
  3. 9 core/modules/node/src/Plugin/views/wizard/Node.php \Drupal\node\Plugin\views\wizard\Node
  4. 9 core/modules/node/src/Plugin/views/argument_default/Node.php \Drupal\node\Plugin\views\argument_default\Node
  5. 9 core/modules/node/src/Plugin/migrate/source/d6/Node.php \Drupal\node\Plugin\migrate\source\d6\Node
Same name and namespace in other branches
  1. 11.x core/modules/node/src/Entity/Node.php \Drupal\node\Entity\Node
  2. 11.x core/modules/node/src/Plugin/views/field/Node.php \Drupal\node\Plugin\views\field\Node
  3. 11.x core/modules/node/src/Plugin/views/wizard/Node.php \Drupal\node\Plugin\views\wizard\Node
  4. 11.x core/modules/node/src/Plugin/views/argument_default/Node.php \Drupal\node\Plugin\views\argument_default\Node
  5. 11.x core/modules/node/src/Plugin/migrate/source/d6/Node.php \Drupal\node\Plugin\migrate\source\d6\Node
  6. 11.x core/modules/node/src/Plugin/migrate/source/d7/Node.php \Drupal\node\Plugin\migrate\source\d7\Node

Drupal 7 node source from database.

Available configuration keys:

  • node_type: The node_types to get from the source - can be a string or an array. If not declared then nodes of all types will be retrieved.

Examples:


source:
  plugin: d7_node
  node_type: page

In this example nodes of type page are retrieved from the source database.


source:
  plugin: d7_node
  node_type: [page, test]

In this example nodes of type page and test are retrieved from the source database.

For additional configuration keys, refer to the parent classes.

Plugin annotation


@MigrateSource(
  id = "d7_node",
  source_module = "node"
)

Hierarchy

Expanded class hierarchy of Node

See also

\Drupal\migrate\Plugin\migrate\source\SqlBase

\Drupal\migrate\Plugin\migrate\source\SourcePluginBase

1344 string references to 'Node'
AccessPermissionTest::setUp in core/modules/user/tests/src/Kernel/Views/AccessPermissionTest.php
AddFeedTest::testFeedIconEscaping in core/modules/system/tests/src/Kernel/Common/AddFeedTest.php
Checks that special characters are correctly escaped.
AdminMetaTagTest::testMetaTag in core/modules/system/tests/src/Functional/System/AdminMetaTagTest.php
Verify that the meta tag HTML is generated correctly.
AjaxPageStateTest::testDrupalSettingsIsNotLoaded in core/modules/system/tests/src/Functional/Render/AjaxPageStateTest.php
Give ajax_page_state[libraries]=core/drupalSettings to exclude the library.
AjaxPageStateTest::testLibrariesAvailable in core/modules/system/tests/src/Functional/Render/AjaxPageStateTest.php
Default functionality without the param ajax_page_state[libraries].

... See full list

File

core/modules/node/src/Plugin/migrate/source/d7/Node.php, line 50

Namespace

Drupal\node\Plugin\migrate\source\d7
View source
class Node extends FieldableEntity {
  
  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;
  
  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_type_manager);
    $this->moduleHandler = $module_handler;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    return new static($configuration, $plugin_id, $plugin_definition, $migration, $container->get('state'), $container->get('entity_type.manager'), $container->get('module_handler'));
  }
  
  /**
   * The join options between the node and the node_revisions table.
   */
  const JOIN = '[n].[vid] = [nr].[vid]';
  
  /**
   * {@inheritdoc}
   */
  public function query() {
    // Select node in its last revision.
    $query = $this->select('node_revision', 'nr')
      ->fields('n', [
      'nid',
      'type',
      'language',
      'status',
      'created',
      'changed',
      'comment',
      'promote',
      'sticky',
      'tnid',
      'translate',
    ])
      ->fields('nr', [
      'vid',
      'title',
      'log',
      'timestamp',
    ]);
    $query->addField('n', 'uid', 'node_uid');
    $query->addField('nr', 'uid', 'revision_uid');
    $query->innerJoin('node', 'n', static::JOIN);
    // If the content_translation module is enabled, get the source langcode
    // to fill the content_translation_source field.
    if ($this->moduleHandler
      ->moduleExists('content_translation')) {
      $query->leftJoin('node', 'nt', '[n].[tnid] = [nt].[nid]');
      $query->addField('nt', 'language', 'source_langcode');
    }
    $this->handleTranslations($query);
    if (isset($this->configuration['node_type'])) {
      $query->condition('n.type', (array) $this->configuration['node_type'], 'IN');
    }
    return $query;
  }
  
  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    $nid = $row->getSourceProperty('nid');
    $vid = $row->getSourceProperty('vid');
    $type = $row->getSourceProperty('type');
    // If this entity was translated using Entity Translation, we need to get
    // its source language to get the field values in the right language.
    // The translations will be migrated by the d7_node_entity_translation
    // migration.
    $entity_translatable = $this->isEntityTranslatable('node') && (int) $this->variableGet('language_content_type_' . $type, 0) === 4;
    $source_language = $this->getEntityTranslationSourceLanguage('node', $nid);
    $language = $entity_translatable && $source_language ? $source_language : $row->getSourceProperty('language');
    // If this is using d7_node_complete source plugin and this is a node
    // using entity translation then set the language of this revision to the
    // entity translation language.
    if ($row->getSourceProperty('etr_created')) {
      $language = $row->getSourceProperty('language');
    }
    // Get Field API field values.
    foreach ($this->getFields('node', $type) as $field_name => $field) {
      // Ensure we're using the right language if the entity and the field are
      // translatable.
      $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
      $row->setSourceProperty($field_name, $this->getFieldValues('node', $field_name, $nid, $vid, $field_language));
    }
    // Make sure we always have a translation set.
    if ($row->getSourceProperty('tnid') == 0) {
      $row->setSourceProperty('tnid', $row->getSourceProperty('nid'));
    }
    // If the node title was replaced by a real field using the Drupal 7 Title
    // module, use the field value instead of the node title.
    if ($this->moduleExists('title')) {
      $title_field = $row->getSourceProperty('title_field');
      if (isset($title_field[0]['value'])) {
        $row->setSourceProperty('title', $title_field[0]['value']);
      }
    }
    return parent::prepareRow($row);
  }
  
  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields = [
      'nid' => $this->t('Node ID'),
      'type' => $this->t('Type'),
      'title' => $this->t('Title'),
      'node_uid' => $this->t('Node authored by (uid)'),
      'revision_uid' => $this->t('Revision authored by (uid)'),
      'created' => $this->t('Created timestamp'),
      'changed' => $this->t('Modified timestamp'),
      'status' => $this->t('Published'),
      'promote' => $this->t('Promoted to front page'),
      'sticky' => $this->t('Sticky at top of lists'),
      'revision' => $this->t('Create new revision'),
      'language' => $this->t('Language (fr, en, ...)'),
      'tnid' => $this->t('The translation set id for this node'),
      'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
    ];
    return $fields;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getIds() {
    $ids['nid']['type'] = 'integer';
    $ids['nid']['alias'] = 'n';
    return $ids;
  }
  
  /**
   * Adapt our query for translations.
   *
   * @param \Drupal\Core\Database\Query\SelectInterface $query
   *   The generated query.
   */
  protected function handleTranslations(SelectInterface $query) {
    // Check whether or not we want translations.
    if (empty($this->configuration['translations'])) {
      // No translations: Yield untranslated nodes, or default translations.
      $query->where('[n].[tnid] = 0 OR [n].[tnid] = [n].[nid]');
    }
    else {
      // Translations: Yield only non-default translations.
      $query->where('[n].[tnid] <> 0 AND [n].[tnid] <> [n].[nid]');
    }
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.