diff --git a/core/modules/comment/src/Plugin/migrate/source/d6/Comment.php b/core/modules/comment/src/Plugin/migrate/source/d6/Comment.php index 5d291db..458170b 100644 --- a/core/modules/comment/src/Plugin/migrate/source/d6/Comment.php +++ b/core/modules/comment/src/Plugin/migrate/source/d6/Comment.php @@ -33,6 +33,18 @@ public function query() { * {@inheritdoc} */ public function prepareRow(Row $row) { + // This is a backward compatibility layer for the deprecated migrate source + // plugins d6_comment_variable and d6_comment_variable_per_comment_type. + if ($this->variableGet('comment_subject_field_' . $row->getSourceProperty('type'), 1)) { + // Comment subject visible. + $row->setSourceProperty('field_name', 'comment'); + $row->setSourceProperty('comment_type', 'comment'); + } + else { + $row->setSourceProperty('field_name', 'comment_no_subject'); + $row->setSourceProperty('comment_type', 'comment_no_subject'); + } + // In D6, status=0 means published, while in D8 means the opposite. // See https://www.drupal.org/node/237636. $row->setSourceProperty('status', !$row->getSourceProperty('status')); diff --git a/core/modules/comment/src/Plugin/migrate/source/d6/CommentVariable.php b/core/modules/comment/src/Plugin/migrate/source/d6/CommentVariable.php new file mode 100644 index 0000000..2cf51ed --- /dev/null +++ b/core/modules/comment/src/Plugin/migrate/source/d6/CommentVariable.php @@ -0,0 +1,114 @@ +getCommentVariables()); + } + + /** + * {@inheritdoc} + */ + public function count() { + return count($this->getCommentVariables()); + } + + /** + * Retrieves the values of the comment variables grouped by node type. + * + * @return array + */ + protected function getCommentVariables() { + $comment_prefixes = array_keys($this->commentPrefixes()); + $variables = []; + $node_types = $this->select('node_type', 'nt') + ->fields('nt', ['type']) + ->execute() + ->fetchCol(); + foreach ($node_types as $node_type) { + foreach ($comment_prefixes as $prefix) { + $variables[] = $prefix . '_' . $node_type; + } + } + $return = []; + $values = $this->select('variable', 'v') + ->fields('v', ['name', 'value']) + ->condition('name', $variables, 'IN') + ->execute() + ->fetchAllKeyed(); + foreach ($node_types as $node_type) { + foreach ($comment_prefixes as $prefix) { + $name = $prefix . '_' . $node_type; + if (isset($values[$name])) { + $return[$node_type][$prefix] = unserialize($values[$name]); + } + } + } + // The return key will not be used so move it inside the row. This could + // not be done sooner because otherwise empty rows would be created with + // just the node type in it. + foreach ($return as $node_type => $data) { + $return[$node_type]['node_type'] = $node_type; + $return[$node_type]['comment_type'] = empty($data['comment_subject_field']) ? + 'comment_no_subject' : 'comment'; + } + return $return; + } + + /** + * {@inheritdoc} + */ + public function fields() { + return $this->commentPrefixes() + [ + 'node_type' => $this->t('The node type'), + 'comment_type' => $this->t('The comment type'), + ]; + } + + /** + * Comment related data for fields. + */ + protected function commentPrefixes() { + return [ + 'comment' => $this->t('Default comment setting'), + 'comment_default_mode' => $this->t('Default display mode'), + 'comment_default_order' => $this->t('Default display order'), + 'comment_default_per_page' => $this->t('Default comments per page'), + 'comment_controls' => $this->t('Comment controls'), + 'comment_anonymous' => $this->t('Anonymous commenting'), + 'comment_subject_field' => $this->t('Comment subject field'), + 'comment_preview' => $this->t('Preview comment'), + 'comment_form_location' => $this->t('Location of comment submission form'), + ]; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['node_type']['type'] = 'string'; + return $ids; + } + +} diff --git a/core/modules/comment/src/Plugin/migrate/source/d6/CommentVariablePerCommentType.php b/core/modules/comment/src/Plugin/migrate/source/d6/CommentVariablePerCommentType.php new file mode 100644 index 0000000..bfdb39c --- /dev/null +++ b/core/modules/comment/src/Plugin/migrate/source/d6/CommentVariablePerCommentType.php @@ -0,0 +1,68 @@ + $data) { + // Only 2 comment types depending on subject field visibility. + if (!empty($data['comment_subject_field'])) { + // Default label and description should be set in migration. + $return['comment'] = [ + 'comment_type' => 'comment', + 'label' => $this->t('Default comments'), + 'description' => $this->t('Allows commenting on content') + ]; + } + else { + // Provide a special comment type with hidden subject field. + $return['comment_no_subject'] = [ + 'comment_type' => 'comment_no_subject', + 'label' => $this->t('Comments without subject field'), + 'description' => $this->t('Allows commenting on content, comments without subject field') + ]; + } + } + return $return; + } + + /** + * {@inheritdoc} + */ + public function fields() { + return [ + 'comment_type' => $this->t('The comment type'), + 'label' => $this->t('The comment type label'), + 'description' => $this->t('The comment type description'), + ]; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['comment_type']['type'] = 'string'; + return $ids; + } + +} diff --git a/core/modules/comment/src/Plugin/migrate/source/d7/CommentType.php b/core/modules/comment/src/Plugin/migrate/source/d7/CommentType.php new file mode 100644 index 0000000..6890c6d --- /dev/null +++ b/core/modules/comment/src/Plugin/migrate/source/d7/CommentType.php @@ -0,0 +1,103 @@ +select('field_config_instance', 'fci') + ->distinct() + ->fields('fci', ['bundle']) + ->condition('fci.entity_type', 'comment'); + } + + /** + * {@inheritdoc} + */ + protected function initializeIterator() { + $this->nodeTypes = $this->select('node_type', 'nt') + ->fields('nt', ['type', 'name']) + ->execute() + ->fetchAllKeyed(); + + return parent::initializeIterator(); + } + + /** + * {@inheritdoc} + */ + public function prepareRow(Row $row) { + $node_type = substr($row->getSourceProperty('bundle'), 13); + $row->setSourceProperty('node_type', $node_type); + + $row->setSourceProperty('default_mode', $this->variableGet("comment_default_mode_$node_type", 1)); + $row->setSourceProperty('per_page', $this->variableGet("comment_default_per_page_$node_type", 50)); + $row->setSourceProperty('anonymous', $this->variableGet("comment_anonymous_$node_type", FALSE)); + $row->setSourceProperty('form_location', $this->variableGet("comment_form_location_$node_type", CommentItemInterface::FORM_BELOW)); + $row->setSourceProperty('preview', $this->variableGet("comment_preview_$node_type", TRUE)); + $row->setSourceProperty('subject', $this->variableGet("comment_subject_field_$node_type", TRUE)); + + $label = $this->t('@node_type comment', [ + '@node_type' => $this->nodeTypes[$node_type], + ]); + $row->setSourceProperty('label', $label); + + return parent::prepareRow($row); + } + + /** + * {@inheritdoc} + */ + public function fields() { + return [ + 'label' => $this->t('The label of the comment type.'), + 'bundle' => $this->t('Bundle ID of the comment type.'), + 'node_type' => $this->t('The node type to which this comment type is attached.'), + 'default_mode' => $this->t('Default comment mode.'), + 'per_page' => $this->t('Number of comments visible per page.'), + 'anonymous' => $this->t('Whether anonymous comments are allowed.'), + 'form_location' => $this->t('Location of the comment form.'), + 'preview' => $this->t('Whether previews are enabled for the comment type.'), + 'subject' => $this->t('Whether a subject field is enabled for the comment type.'), + ]; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + return [ + 'bundle' => [ + 'type' => 'string', + ], + ]; + } + +} diff --git a/core/modules/comment/tests/src/Kernel/Plugin/migrate/source/d6/CommentVariablePerCommentTypeTest.php b/core/modules/comment/tests/src/Kernel/Plugin/migrate/source/d6/CommentVariablePerCommentTypeTest.php new file mode 100644 index 0000000..dd1b1fd --- /dev/null +++ b/core/modules/comment/tests/src/Kernel/Plugin/migrate/source/d6/CommentVariablePerCommentTypeTest.php @@ -0,0 +1,62 @@ + 'page', + ], + [ + 'type' => 'story', + ], + ]; + + $tests[0]['source_data']['variable'] = [ + [ + 'name' => 'comment_subject_field_page', + 'value' => serialize(1), + ], + [ + 'name' => 'comment_subject_field_story', + 'value' => serialize(0), + ], + ]; + + // The expected results. + // Each result will also include a label and description, but those are + // static values set by the source plugin and don't need to be asserted. + $tests[0]['expected_data'] = [ + [ + 'comment_type' => 'comment', + ], + [ + 'comment_type' => 'comment_no_subject', + ], + ]; + + return $tests; + } + +} diff --git a/core/modules/comment/tests/src/Kernel/Plugin/migrate/source/d6/CommentVariableTest.php b/core/modules/comment/tests/src/Kernel/Plugin/migrate/source/d6/CommentVariableTest.php new file mode 100644 index 0000000..a84c776 --- /dev/null +++ b/core/modules/comment/tests/src/Kernel/Plugin/migrate/source/d6/CommentVariableTest.php @@ -0,0 +1,92 @@ + 'page', + ], + ]; + + $tests[0]['source_data']['variable'] = [ + [ + 'name' => 'comment_page', + 'value' => serialize(1), + ], + [ + 'name' => 'comment_default_mode_page', + 'value' => serialize(1), + ], + [ + 'name' => 'comment_default_order_page', + 'value' => serialize(1), + ], + [ + 'name' => 'comment_default_per_page_page', + 'value' => serialize(50), + ], + [ + 'name' => 'comment_controls_page', + 'value' => serialize(1), + ], + [ + 'name' => 'comment_anonymous_page', + 'value' => serialize(1), + ], + [ + 'name' => 'comment_subject_field_page', + 'value' => serialize(1), + ], + [ + 'name' => 'comment_preview_page', + 'value' => serialize(1), + ], + [ + 'name' => 'comment_form_location_page', + 'value' => serialize(1), + ], + ]; + + // The expected results. + $tests[0]['expected_data'] = [ + [ + 'comment' => '1', + 'comment_default_mode' => '1', + 'comment_default_order' => '1', + 'comment_default_per_page' => '50', + 'comment_controls' => '1', + 'comment_anonymous' => '1', + 'comment_subject_field' => '1', + 'comment_preview' => '1', + 'comment_form_location' => '1', + 'node_type' => 'page', + 'comment_type' => 'comment', + ], + ]; + + return $tests; + } + +} diff --git a/core/modules/comment/tests/src/Kernel/Plugin/migrate/source/d7/CommentTypeTest.php b/core/modules/comment/tests/src/Kernel/Plugin/migrate/source/d7/CommentTypeTest.php new file mode 100644 index 0000000..11f3db5 --- /dev/null +++ b/core/modules/comment/tests/src/Kernel/Plugin/migrate/source/d7/CommentTypeTest.php @@ -0,0 +1,99 @@ + 'article', + 'name' => 'Article', + 'base' => 'node_content', + 'module' => 'node', + 'description' => 'Use articles for time-sensitive content like news, press releases or blog posts.', + 'help' => 'Help text for articles', + 'has_title' => '1', + 'title_label' => 'Title', + 'custom' => '1', + 'modified' => '1', + 'locked' => '0', + 'disabled' => '0', + 'orig_type' => 'article', + ], + ]; + $tests[0]['source_data']['field_config_instance'] = [ + [ + 'id' => '14', + 'field_id' => '1', + 'field_name' => 'comment_body', + 'entity_type' => 'comment', + 'bundle' => 'comment_node_article', + 'data' => 'a:0:{}', + 'deleted' => '0', + ], + ]; + $tests[0]['source_data']['variable'] = [ + [ + 'name' => 'comment_default_mode_article', + 'value' => serialize(1), + ], + [ + 'name' => 'comment_per_page_article', + 'value' => serialize(50), + ], + [ + 'name' => 'comment_anonymous_article', + 'value' => serialize(0), + ], + [ + 'name' => 'comment_form_location_article', + 'value' => serialize(1), + ], + [ + 'name' => 'comment_preview_article', + 'value' => serialize(0), + ], + [ + 'name' => 'comment_subject_article', + 'value' => serialize(1), + ], + ]; + + // The expected results. + $tests[0]['expected_data'] = [ + [ + 'bundle' => 'comment_node_article', + 'node_type' => 'article', + 'default_mode' => '1', + 'per_page' => '50', + 'anonymous' => '0', + 'form_location' => '1', + 'preview' => '0', + 'subject' => '1', + 'label' => 'Article comment', + ], + ]; + return $tests; + } + +}