Version 1
[yaffs-website] / web / core / modules / node / src / Plugin / migrate / source / d7 / NodeType.php
1 <?php
2
3 namespace Drupal\node\Plugin\migrate\source\d7;
4
5 use Drupal\migrate\Row;
6 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
7
8 /**
9  * Drupal 7 Node types source from database.
10  *
11  * @MigrateSource(
12  *   id = "d7_node_type",
13  *   source_provider = "node"
14  * )
15  */
16 class NodeType extends DrupalSqlBase {
17
18   /**
19    * The teaser length
20    *
21    * @var int
22    */
23   protected $teaserLength;
24
25   /**
26    * Node preview optional / required.
27    *
28    * @var int
29    */
30   protected $nodePreview;
31
32   /**
33    * {@inheritdoc}
34    */
35   public function query() {
36     return $this->select('node_type', 't')->fields('t');
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function fields() {
43     return [
44       'type' => $this->t('Machine name of the node type.'),
45       'name' => $this->t('Human name of the node type.'),
46       'description' => $this->t('Description of the node type.'),
47       'help' => $this->t('Help text for the node type.'),
48       'title_label' => $this->t('Title label.'),
49       'disabled' => $this->t('Flag indicating the node type is enable'),
50       'base' => $this->t('base node.'),
51       'custom' => $this->t('Flag.'),
52       'modified' => $this->t('Flag.'),
53       'locked' => $this->t('Flag.'),
54       'orig_type' => $this->t('The original type.'),
55       'teaser_length' => $this->t('Teaser length'),
56     ];
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   protected function initializeIterator() {
63     $this->teaserLength = $this->variableGet('teaser_length', 600);
64     $this->nodePreview = $this->variableGet('node_preview', 0);
65     return parent::initializeIterator();
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function prepareRow(Row $row) {
72     $row->setSourceProperty('teaser_length', $this->teaserLength);
73     $row->setSourceProperty('node_preview', $this->nodePreview);
74
75     $type = $row->getSourceProperty('type');
76     $source_options = $this->variableGet('node_options_' . $type, ['promote', 'sticky']);
77     $options = [];
78     foreach (['promote', 'sticky', 'status', 'revision'] as $item) {
79       $options[$item] = in_array($item, $source_options);
80     }
81     $row->setSourceProperty('options', $options);
82
83     // Don't create a body field until we prove that this node type has one.
84     $row->setSourceProperty('create_body', FALSE);
85
86     if ($this->moduleExists('field')) {
87       // Find body field for this node type.
88       $body = $this->select('field_config_instance', 'fci')
89         ->fields('fci', ['data'])
90         ->condition('entity_type', 'node')
91         ->condition('bundle', $row->getSourceProperty('type'))
92         ->condition('field_name', 'body')
93         ->execute()
94         ->fetchAssoc();
95       if ($body) {
96         $row->setSourceProperty('create_body', TRUE);
97         $body['data'] = unserialize($body['data']);
98         $row->setSourceProperty('body_label', $body['data']['label']);
99       }
100     }
101
102     $row->setSourceProperty('display_submitted', $this->variableGet('node_submitted_' . $type, TRUE));
103
104     if ($menu_options = $this->variableGet('menu_options_' . $type, NULL)) {
105       $row->setSourceProperty('available_menus', $menu_options);
106     }
107     if ($parent = $this->variableGet('menu_parent_' . $type, NULL)) {
108       $row->setSourceProperty('parent', $parent . ':');
109     }
110     return parent::prepareRow($row);
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function getIds() {
117     $ids['type']['type'] = 'string';
118     return $ids;
119   }
120
121 }