diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index bb203bf..b9d84e4 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -661,11 +661,17 @@ function template_preprocess_comment(&$variables) { else { $uri = $comment->permalink(); $attributes = $uri->getOption('attributes') ?: []; - $attributes += ['class' => ['permalink'], 'rel' => 'bookmark']; + $attributes += ['class' => ['permalink']]; + if (CommentType::load($comment->bundle())->getNofollow()) { + $attributes += ['rel' => 'bookmark nofollow']; + } + else { + $attributes += ['rel' => 'bookmark']; + } $uri->setOption('attributes', $attributes); $variables['title'] = \Drupal::l($comment->getSubject(), $uri); - $variables['permalink'] = \Drupal::l(t('Permalink'), $comment->permalink()); + $variables['permalink'] = \Drupal::l(t('Permalink'), $uri); } $variables['submitted'] = t('Submitted by @username on @datetime', ['@username' => $variables['author'], '@datetime' => $variables['created']]); @@ -690,7 +696,13 @@ function template_preprocess_comment(&$variables) { } $permalink_uri_parent = $comment_parent->permalink(); $attributes = $permalink_uri_parent->getOption('attributes') ?: []; - $attributes += ['class' => ['permalink'], 'rel' => 'bookmark']; + $attributes += ['class' => ['permalink']]; + if (CommentType::load($comment_parent->bundle())->getNofollow()) { + $attributes += ['rel' => 'bookmark nofollow']; + } + else { + $attributes += ['rel' => 'bookmark']; + } $permalink_uri_parent->setOption('attributes', $attributes); $variables['parent_title'] = \Drupal::l($comment_parent->getSubject(), $permalink_uri_parent); $variables['parent_permalink'] = \Drupal::l(t('Parent permalink'), $permalink_uri_parent); diff --git a/core/modules/comment/config/schema/comment.schema.yml b/core/modules/comment/config/schema/comment.schema.yml index 045e9ad..a344f10 100644 --- a/core/modules/comment/config/schema/comment.schema.yml +++ b/core/modules/comment/config/schema/comment.schema.yml @@ -58,6 +58,9 @@ comment.type.*: description: type: text label: 'Description' + nofollow: + type: boolean + label: 'Rel nofollow' field.storage_settings.comment: type: mapping diff --git a/core/modules/comment/src/CommentTypeForm.php b/core/modules/comment/src/CommentTypeForm.php index 0ac5b44..64033b7 100644 --- a/core/modules/comment/src/CommentTypeForm.php +++ b/core/modules/comment/src/CommentTypeForm.php @@ -122,6 +122,13 @@ public function form(array $form, FormStateInterface $form_state) { ]; } + $form['nofollow'] = [ + '#type' => 'checkbox', + '#title' => t('Nofollow'), + '#default_value' => $comment_type->getNofollow(), + '#description' => t('Add nofollow to rel attribute of comment type.'), + ]; + if ($this->moduleHandler->moduleExists('content_translation')) { $form['language'] = [ '#type' => 'details', diff --git a/core/modules/comment/src/CommentTypeInterface.php b/core/modules/comment/src/CommentTypeInterface.php index acde96c..4653576 100644 --- a/core/modules/comment/src/CommentTypeInterface.php +++ b/core/modules/comment/src/CommentTypeInterface.php @@ -35,4 +35,11 @@ public function setDescription($description); */ public function getTargetEntityTypeId(); + /** + * Gets the setting of rel's nofollow for this comment type. + * + * @return bool + * TRUE if the comment type has the rel set to nofollow, FALSE otherwise. + */ + public function getNofollow(); } diff --git a/core/modules/comment/src/Entity/CommentType.php b/core/modules/comment/src/Entity/CommentType.php index a773c58..62009db 100644 --- a/core/modules/comment/src/Entity/CommentType.php +++ b/core/modules/comment/src/Entity/CommentType.php @@ -44,6 +44,7 @@ * "label", * "target_entity_type_id", * "description", + * "nofollow", * } * ) */ @@ -78,6 +79,13 @@ class CommentType extends ConfigEntityBundleBase implements CommentTypeInterface protected $target_entity_type_id; /** + * Whether the comment type has rel set to nofollow. + * + * @var bool + */ + protected $nofollow = TRUE; + + /** * {@inheritdoc} */ public function getDescription() { @@ -99,4 +107,11 @@ public function getTargetEntityTypeId() { return $this->target_entity_type_id; } + /** + * {@inheritdoc} + */ + public function getNofollow() { + return $this->nofollow; + } + } diff --git a/core/modules/comment/src/Tests/CommentTestBase.php b/core/modules/comment/src/Tests/CommentTestBase.php index 3b923ff..7210065 100644 --- a/core/modules/comment/src/Tests/CommentTestBase.php +++ b/core/modules/comment/src/Tests/CommentTestBase.php @@ -401,6 +401,7 @@ protected function createCommentType($label) { 'label' => $label, 'description' => '', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ]); $bundle->save(); return $bundle; diff --git a/core/modules/comment/src/Tests/CommentTestTrait.php b/core/modules/comment/src/Tests/CommentTestTrait.php index 038a3df..739aace 100644 --- a/core/modules/comment/src/Tests/CommentTestTrait.php +++ b/core/modules/comment/src/Tests/CommentTestTrait.php @@ -47,6 +47,7 @@ public function addDefaultCommentField($entity_type, $bundle, $field_name = 'com 'id' => $comment_type_id, 'label' => Unicode::ucfirst($comment_type_id), 'target_entity_type_id' => $entity_type, + 'nofollow' => TRUE, 'description' => 'Default comment field', ])->save(); } diff --git a/core/modules/comment/tests/src/Functional/CommentFieldsTest.php b/core/modules/comment/tests/src/Functional/CommentFieldsTest.php index 2df9973..dc5aa2f 100644 --- a/core/modules/comment/tests/src/Functional/CommentFieldsTest.php +++ b/core/modules/comment/tests/src/Functional/CommentFieldsTest.php @@ -168,6 +168,7 @@ public function testCommentFieldCreate() { 'label' => 'user_comment_type', 'description' => '', 'target_entity_type_id' => 'user', + 'nofollow' => TRUE, ]); $bundle->save(); diff --git a/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php b/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php index 5889d69..19dc593 100644 --- a/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php +++ b/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php @@ -56,6 +56,7 @@ protected function setUp() { 'label' => 'Comment settings', 'description' => 'Comment settings', 'target_entity_type_id' => 'entity_test', + 'nofollow' => TRUE, ])->save(); // Create comment field on entity_test bundle. $this->addDefaultCommentField('entity_test', 'entity_test'); @@ -411,6 +412,7 @@ public function testCommentFunctionality() { 'label' => 'Foobar', 'description' => '', 'target_entity_type_id' => 'entity_test', + 'nofollow' => TRUE, ]); $bundle->save(); diff --git a/core/modules/comment/tests/src/Functional/CommentTestBase.php b/core/modules/comment/tests/src/Functional/CommentTestBase.php index 7cadcbf..e00126c 100644 --- a/core/modules/comment/tests/src/Functional/CommentTestBase.php +++ b/core/modules/comment/tests/src/Functional/CommentTestBase.php @@ -395,6 +395,7 @@ protected function createCommentType($label) { 'label' => $label, 'description' => '', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ]); $bundle->save(); return $bundle; diff --git a/core/modules/comment/tests/src/Kernel/CommentBundlesTest.php b/core/modules/comment/tests/src/Kernel/CommentBundlesTest.php index d907233..de1c84a 100644 --- a/core/modules/comment/tests/src/Kernel/CommentBundlesTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentBundlesTest.php @@ -51,6 +51,7 @@ protected function setUp() { 'id' => 'comment_on_' . $id, 'label' => 'Comment on ' . $label, 'target_entity_type_id' => $id, + 'nofollow' => TRUE, ])->save(); } } diff --git a/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php b/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php index 1e0fd72..0d7c377 100644 --- a/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php @@ -99,6 +99,7 @@ public function testAccessToAdministrativeFields() { 'label' => 'Default comments', 'description' => 'Default comment field', 'target_entity_type_id' => 'entity_test', + 'nofollow' => TRUE, ]); $comment_type->save(); diff --git a/core/modules/comment/tests/src/Kernel/CommentStringIdEntitiesTest.php b/core/modules/comment/tests/src/Kernel/CommentStringIdEntitiesTest.php index adbe1b0..2f6519c 100644 --- a/core/modules/comment/tests/src/Kernel/CommentStringIdEntitiesTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentStringIdEntitiesTest.php @@ -45,6 +45,7 @@ public function testCommentFieldNonStringId() { 'label' => 'foo', 'description' => '', 'target_entity_type_id' => 'entity_test_string_id', + 'nofollow' => TRUE, ]); $bundle->save(); $field_storage = FieldStorageConfig::create([ diff --git a/core/modules/comment/tests/src/Kernel/Migrate/MigrateCommentStubTest.php b/core/modules/comment/tests/src/Kernel/Migrate/MigrateCommentStubTest.php index 0b256ea..cf3744f 100644 --- a/core/modules/comment/tests/src/Kernel/Migrate/MigrateCommentStubTest.php +++ b/core/modules/comment/tests/src/Kernel/Migrate/MigrateCommentStubTest.php @@ -48,6 +48,7 @@ protected function setUp() { 'id' => 'testcommenttype', 'label' => 'Test comment type', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ])->save(); } diff --git a/core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php b/core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php index 86e8949..f9c553d 100644 --- a/core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php +++ b/core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php @@ -87,6 +87,7 @@ protected function setUp($import_test_views = TRUE) { 'label' => 'Default comments', 'target_entity_type_id' => 'entity_test', 'description' => 'Default comment field', + 'nofollow' => TRUE, ])->save(); // Create a commented entity. $entity = EntityTest::create(); diff --git a/core/modules/rest/tests/src/Functional/EntityResource/CommentType/CommentTypeResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/CommentType/CommentTypeResourceTestBase.php index 9d88211..968998a 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/CommentType/CommentTypeResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/CommentType/CommentTypeResourceTestBase.php @@ -44,6 +44,7 @@ protected function createEntity() { 'label' => 'Camelids', 'description' => 'Camelids are large, strictly herbivorous animals with slender necks and long legs.', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ]); $camelids->save(); @@ -63,6 +64,7 @@ protected function getExpectedNormalizedEntity() { 'langcode' => 'en', 'status' => TRUE, 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, 'uuid' => $this->entity->uuid(), ]; }