BeerUser.php 1.53 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\migrate_example\Plugin\migrate\source\BeerUser.
 */

namespace Drupal\migrate_example\Plugin\migrate\source;

10
use Drupal\migrate\Plugin\migrate\source\SqlBase;
11 12 13 14 15 16 17 18 19
use Drupal\migrate\Row;

/**
 * Drupal 6 user source from database.
 *
 * @MigrateSource(
 *   id = "beer_user"
 * )
 */
20
class BeerUser extends SqlBase {
21 22 23 24 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

  /**
   * {@inheritdoc}
   */
  public function query() {
    return $this->select('migrate_example_beer_account', 'mea')
      ->fields('mea', array('aid', 'status', 'posted', 'name', 'nickname',
                            'password', 'mail', 'sex', 'beers'));
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields = array(
      'aid' => $this->t('Account ID'),
      'status' => $this->t('Blocked/Allowed'),
      'posted' => $this->t('Registered date'),
      'name' => $this->t('Account name (for login)'),
      'nickname' => $this->t('Account name (for display)'),
      'password' => $this->t('Account password (raw)'),
      'mail' => $this->t('Account email'),
      'sex' => $this->t('Gender'),
      'beers' => $this->t('Favorite beers, pipe-separated'),
    );

    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    return array(
      'aid' => array(
        'type' => 'integer',
        'alias' => 'mea',
      ),
    );
  }

  public function prepareRow(Row $row) {
    if ($value = $row->getSourceProperty('beers')) {
      $row->setSourceProperty('beers', explode('|', $value));
    }
    return parent::prepareRow($row);
  }

}