BeerNode.php 3.04 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\migrate_example\Plugin\migrate\source;

5
use Drupal\migrate\Plugin\migrate\source\SqlBase;
6 7 8
use Drupal\migrate\Row;

/**
9
 * Source plugin for beer content.
10 11 12 13 14
 *
 * @MigrateSource(
 *   id = "beer_node"
 * )
 */
15
class BeerNode extends SqlBase {
16 17 18 19 20

  /**
   * {@inheritdoc}
   */
  public function query() {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    // An important point to note is that your query *must* return a single row
    // for each item to be imported. Here we might be tempted to add a join to
    // migrate_example_beer_topic_node in our query, to pull in the
    // relationships to our categories. Doing this would cause the query to
    // return multiple rows for a given node, once per related value, thus
    // processing the same node multiple times, each time with only one of the
    // multiple values that should be imported. To avoid that, we simply query
    // the base node data here, and pull in the relationships in prepareRow()
    // below.
    $fields = [
      'bid',
      'name',
      'body',
      'excerpt',
      'aid',
      'countries',
      'image',
      'image_alt',
      'image_title',
      'image_description',
    ];
42
    $query = $this->select('migrate_example_beer_node', 'b')
43
      ->fields('b', $fields);
44 45 46 47 48 49 50
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
51
    $fields = [
52 53 54 55 56 57 58 59 60 61
      'bid' => $this->t('Beer ID'),
      'name' => $this->t('Name of beer'),
      'body' => $this->t('Full description of the beer'),
      'excerpt' => $this->t('Abstract for this beer'),
      'aid' => $this->t('Account ID of the author'),
      'countries' => $this->t('Countries of origin. Multiple values, delimited by pipe'),
      'image' => $this->t('Image path'),
      'image_alt' => $this->t('Image ALT'),
      'image_title' => $this->t('Image title'),
      'image_description' => $this->t('Image description'),
62 63 64
      // Note that this field is not part of the query above - it is populated
      // by prepareRow() below. You should document all source properties that
      // are available for mapping after prepareRow() is called.
65
      'terms' => $this->t('Applicable styles'),
66
    ];
67 68 69 70 71 72 73 74

    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
75 76
    return [
      'bid' => [
77 78
        'type' => 'integer',
        'alias' => 'b',
79 80
      ],
    ];
81 82
  }

83 84 85
  /**
   * {@inheritdoc}
   */
86
  public function prepareRow(Row $row) {
87 88 89
    // As explained above, we need to pull the style relationships into our
    // source row here, as an array of 'style' values (the unique ID for
    // the beer_term migration).
90
    $terms = $this->select('migrate_example_beer_topic_node', 'bt')
91
      ->fields('bt', ['style'])
92
      ->condition('bid', $row->getSourceProperty('bid'))
93 94
      ->execute()
      ->fetchCol();
95
    $row->setSourceProperty('terms', $terms);
96

97 98
    // As we did for favorite beers in the user migration, we need to explode
    // the multi-value country names.
99 100 101
    if ($value = $row->getSourceProperty('countries')) {
      $row->setSourceProperty('countries', explode('|', $value));
    }
102
    return parent::prepareRow($row);
103 104 105
  }

}