You are here

Course.php in Course 3.x

File

modules/course_migrate/src/Plugin/migrate/source/Course.php
View source
<?php

namespace Drupal\course_migrate\Plugin\migrate\source;

use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;

/**
 * Drupal 7 course source from database.
 *
 * @MigrateSource(
 *   id = "d7_course",
 *   core = {7},
 *   source_module = "course",
 *   destination_module = "course"
 * )
 */
class Course extends FieldableEntity {

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    $ids = [];
    $ids['nid']['type'] = 'integer';
    $ids['nid']['alias'] = 'n';
    return $ids;
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this
      ->select('course_node', 'c');
    $query
      ->join('node', 'n', 'n.nid = c.nid');
    $query
      ->fields('c');
    $query
      ->fields('n', [
      'nid',
      'vid',
      'title',
      'uid',
      'created',
      'changed',
      'type',
      'status',
    ]);
    if (isset($this->configuration['bundle'])) {
      $query
        ->condition('n.type', (array) $this->configuration['bundle'], 'IN');
    }
    return $query;
  }
  public function fields() {
    return [
      'nid' => 'Node ID',
      'title' => 'Title',
      'type' => 'Course type',
      'outline' => 'Outline type',
      'credits' => 'Credits',
      'open' => 'Open',
      'close' => 'Close',
      'duration' => 'Duration',
      'external_id' => 'External ID',
      'enrollment_type' => 'Enrollment type for new enrollments',
      'relationships' => 'Relationships data',
      'created' => 'Created',
      'changed' => 'Changed',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    $nid = $row
      ->getSourceProperty('nid');
    $vid = $row
      ->getSourceProperty('vid');
    $type = $row
      ->getSourceProperty('type');

    // Get Field API field values.
    foreach ($this
      ->getFields('node', $type) as $field_name => $field) {
      $row
        ->setSourceProperty($field_name, $this
        ->getFieldValues('node', $field_name, $nid, $vid));
    }
    return parent::prepareRow($row);
  }

}

Classes

Namesort descending Description
Course Drupal 7 course source from database.