You are here

protected function FieldPermissionsCommentTest::addCommentField in Field Permissions 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/FieldPermissionsCommentTest.php \Drupal\Tests\field_permissions\Functional\FieldPermissionsCommentTest::addCommentField()

Configure a comment field on the article node type.

1 call to FieldPermissionsCommentTest::addCommentField()
FieldPermissionsCommentTest::testFieldPermissionComment in tests/src/Functional/FieldPermissionsCommentTest.php
Test field permissions on comments.

File

tests/src/Functional/FieldPermissionsCommentTest.php, line 82

Class

FieldPermissionsCommentTest
Test field permissions with the comment module and fields.

Namespace

Drupal\Tests\field_permissions\Functional

Code

protected function addCommentField() {
  $entity_manager = \Drupal::entityTypeManager();
  $bundle = 'article';
  $comment_type_storage = $entity_manager
    ->getStorage('comment_type');
  $comment_type_id = 'comment';
  $entity_type = 'node';
  $field_name = 'comment';
  $comment_type_storage
    ->create([
    'id' => $comment_type_id,
    'label' => 'Comment',
    'target_entity_type_id' => $entity_type,
    'description' => 'Default comment field',
  ])
    ->save();
  $entity_manager
    ->getStorage('field_storage_config')
    ->create([
    'entity_type' => $entity_type,
    'field_name' => $field_name,
    'type' => 'comment',
    'settings' => [
      'comment_type' => $comment_type_id,
    ],
  ])
    ->save();
  $entity_manager
    ->getStorage('field_config')
    ->create([
    'label' => 'Comments',
    'description' => '',
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'required' => 1,
    'default_value' => [
      [
        'status' => 2,
        'cid' => 0,
        'last_comment_name' => '',
        'last_comment_timestamp' => 0,
        'last_comment_uid' => 0,
      ],
    ],
  ])
    ->save();

  // Entity form displays: assign widget settings for the 'default' form
  // mode, and hide the field in all other form modes.
  $this->entityDisplayRepository
    ->getFormDisplay($entity_type, $bundle, 'default')
    ->setComponent($field_name, [
    'type' => 'comment_default',
    'weight' => 20,
  ])
    ->save();

  // Entity view displays: assign widget settings for the 'default' view
  // mode, and hide the field in all other view modes.
  $this->entityDisplayRepository
    ->getViewDisplay($entity_type, $bundle)
    ->setComponent($field_name, [
    'label' => 'above',
    'type' => 'comment_default',
    'weight' => 20,
  ])
    ->save();
  $field = $entity_manager
    ->getStorage('field_config')
    ->create([
    'label' => 'Comment',
    'bundle' => $comment_type_id,
    'required' => TRUE,
    'field_storage' => FieldStorageConfig::loadByName('comment', $this->fieldName),
  ]);
  $field
    ->save();

  // Assign widget settings for the 'default' form mode.
  $this->entityDisplayRepository
    ->getFormDisplay('comment', $comment_type_id, 'default')
    ->setComponent($this->fieldName, [
    'type' => 'text_textarea',
  ])
    ->save();

  // Assign display settings for the 'default' view mode.
  $this->entityDisplayRepository
    ->getViewDisplay('comment', $comment_type_id)
    ->setComponent($this->fieldName, [
    'label' => 'hidden',
    'type' => 'text_default',
    'weight' => 0,
  ])
    ->save();
}