Migrate plugin to get tid

Public

Warning message

We recommend you to assign at least one category to this snippet so you could easily navigate it in the future. Edit your snippet here.

Get raw version
php
  1. <?php
  2. /**
  3.  * @file
  4.  * Contains \Drupal\migrate_in\Plugin\migrate\source\NodeNews.
  5.  */
  6. namespace Drupal\internews_migrate\Plugin\migrate\source;
  7.  
  8. use Drupal\migrate\Row;
  9. use Drupal\node\Plugin\migrate\source\d7\Node;
  10. use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
  11.  
  12. /**
  13.  * Drupal 7 node source from database.
  14.  *
  15.  * @MigrateSource(
  16.  * id = "internews_node_news",
  17.  * source_provider = "node"
  18.  * )
  19.  */
  20. class Internews_Node_News extends FieldableEntity {
  21.  
  22. /**
  23.   * The join options between the node and the node_revisions table.
  24.   */
  25. /**
  26.   * {@inheritdoc}
  27.   */
  28. const JOIN = 'n.vid = nr.vid';
  29. public function query() {
  30. // Select node in its last revision.
  31. $query = $this->select('node_revision', 'nr')
  32. ->fields('n', array(
  33. 'nid',
  34. 'type',
  35. 'language',
  36. 'status',
  37. 'created',
  38. 'changed',
  39. 'comment',
  40. 'promote',
  41. 'sticky',
  42. 'tnid',
  43. 'translate',
  44. ))
  45. ->fields('nr', array(
  46. 'vid',
  47. 'title',
  48. 'log',
  49. 'timestamp',
  50. ));
  51. $query->addField('n', 'uid', 'node_uid');
  52. $query->addField('nr', 'uid', 'revision_uid');
  53. $query->addField('fnt', 'field_news_type_tid', 'taxonomy_term_id');
  54. $query->innerJoin('node', 'n', static::JOIN);
  55. $query->innerJoin('field_data_field_news_type', 'fnt', 'fnt.entity_id = n.nid');
  56. $inTheNews_tid = 161;
  57. if (isset($this->configuration['node_type'])) {
  58. $query->condition('n.type', $this->configuration['node_type']);
  59. }
  60.  
  61. return $query;
  62. }
  63.  
  64. /**
  65.   * {@inheritdoc}
  66.   */
  67. public function prepareRow(Row $row) {
  68. // Get Field API field values.
  69. foreach (array_keys($this->getFields('node', $row->getSourceProperty('type'))) as $field) {
  70. $nid = $row->getSourceProperty('nid');
  71. $vid = $row->getSourceProperty('vid');
  72. $row->setSourceProperty($field, $this->getFieldValues('node', $field, $nid, $vid));
  73. }
  74.  
  75. return parent::prepareRow($row);
  76. }
  77.  
  78. /**
  79.   * {@inheritdoc}
  80.   */
  81. public function fields() {
  82. $fields = array(
  83. 'nid' => $this->t('Node ID'),
  84. 'type' => $this->t('Type'),
  85. 'title' => $this->t('Title'),
  86. 'node_uid' => $this->t('Node authored by (uid)'),
  87. 'revision_uid' => $this->t('Revision authored by (uid)'),
  88. 'created' => $this->t('Created timestamp'),
  89. 'changed' => $this->t('Modified timestamp'),
  90. 'status' => $this->t('Published'),
  91. 'promote' => $this->t('Promoted to front page'),
  92. 'sticky' => $this->t('Sticky at top of lists'),
  93. 'revision' => $this->t('Create new revision'),
  94. 'language' => $this->t('Language (fr, en, ...)'),
  95. 'tnid' => $this->t('The translation set id for this node'),
  96. 'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
  97. );
  98. return $fields;
  99. }
  100.  
  101. /**
  102.   * {@inheritdoc}
  103.   */
  104. public function getIds() {
  105. $ids['nid']['type'] = 'integer';
  106. $ids['nid']['alias'] = 'n';
  107. return $ids;
  108. }
  109.  
  110.  
  111. }