Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[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_module = "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     $fields = [
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     if ($this->moduleExists('comment')) {
58       $fields += $this->getCommentFields();
59     }
60     return $fields;
61   }
62
63   /**
64    * Returns the fields containing comment settings for each node type.
65    *
66    * @return string[]
67    *   An associative array of field descriptions, keyed by field.
68    */
69   protected function getCommentFields() {
70     return [
71       'comment' => $this->t('Default comment setting'),
72       'comment_default_mode' => $this->t('Default display mode'),
73       'comment_default_per_page' => $this->t('Default comments per page'),
74       'comment_anonymous' => $this->t('Anonymous commenting'),
75       'comment_subject_field' => $this->t('Comment subject field'),
76       'comment_preview' => $this->t('Preview comment'),
77       'comment_form_location' => $this->t('Location of comment submission form'),
78     ];
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   protected function initializeIterator() {
85     $this->teaserLength = $this->variableGet('teaser_length', 600);
86     $this->nodePreview = $this->variableGet('node_preview', 0);
87     return parent::initializeIterator();
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function prepareRow(Row $row) {
94     $row->setSourceProperty('teaser_length', $this->teaserLength);
95     $row->setSourceProperty('node_preview', $this->nodePreview);
96
97     $type = $row->getSourceProperty('type');
98     $source_options = $this->variableGet('node_options_' . $type, ['promote', 'sticky']);
99     $options = [];
100     foreach (['promote', 'sticky', 'status', 'revision'] as $item) {
101       $options[$item] = in_array($item, $source_options);
102     }
103     $row->setSourceProperty('options', $options);
104
105     // Don't create a body field until we prove that this node type has one.
106     $row->setSourceProperty('create_body', FALSE);
107
108     if ($this->moduleExists('field')) {
109       // Find body field for this node type.
110       $body = $this->select('field_config_instance', 'fci')
111         ->fields('fci', ['data'])
112         ->condition('entity_type', 'node')
113         ->condition('bundle', $row->getSourceProperty('type'))
114         ->condition('field_name', 'body')
115         ->execute()
116         ->fetchAssoc();
117       if ($body) {
118         $row->setSourceProperty('create_body', TRUE);
119         $body['data'] = unserialize($body['data']);
120         $row->setSourceProperty('body_label', $body['data']['label']);
121       }
122     }
123
124     $row->setSourceProperty('display_submitted', $this->variableGet('node_submitted_' . $type, TRUE));
125
126     if ($menu_options = $this->variableGet('menu_options_' . $type, NULL)) {
127       $row->setSourceProperty('available_menus', $menu_options);
128     }
129     if ($parent = $this->variableGet('menu_parent_' . $type, NULL)) {
130       $row->setSourceProperty('parent', $parent . ':');
131     }
132
133     if ($this->moduleExists('comment')) {
134       foreach (array_keys($this->getCommentFields()) as $field) {
135         $row->setSourceProperty($field, $this->variableGet($field . '_' . $type, NULL));
136       }
137     }
138
139     return parent::prepareRow($row);
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function getIds() {
146     $ids['type']['type'] = 'string';
147     return $ids;
148   }
149
150 }