function CommentManager::addBodyField

Same name in other branches
  1. 9 core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager::addBodyField()
  2. 8.9.x core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager::addBodyField()
  3. 11.x core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager::addBodyField()

File

core/modules/comment/src/CommentManager.php, line 121

Class

CommentManager
Comment manager contains common functions to manage comment fields.

Namespace

Drupal\comment

Code

public function addBodyField($comment_type_id) {
    if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) {
        // Attaches the body field by default.
        $field = $this->entityTypeManager
            ->getStorage('field_config')
            ->create([
            'label' => 'Comment',
            'bundle' => $comment_type_id,
            'required' => TRUE,
            'field_storage' => FieldStorageConfig::loadByName('comment', 'comment_body'),
        ]);
        $field->save();
        // Assign widget settings for the default form mode.
        $this->entityDisplayRepository
            ->getFormDisplay('comment', $comment_type_id)
            ->setComponent('comment_body', [
            'type' => 'text_textarea',
        ])
            ->save();
        // Assign display settings for the default view mode.
        $this->entityDisplayRepository
            ->getViewDisplay('comment', $comment_type_id)
            ->setComponent('comment_body', [
            'label' => 'hidden',
            'type' => 'text_default',
            'weight' => 0,
        ])
            ->save();
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.