7

I tried to add a comment field in the Basefieldsdefinitions of my custom entity but in my view the add comment form doesn't appear:

Here's the basefielddefinition

 $fields['comments'] = BaseFieldDefinition::create('comment')
  ->setLabel(t('Kommentare'))
  ->setDescription(t('Kommentare.'))

  ->setSettings(
    array(
      'default_mode'=> 1,
      'per_page'=>50,
      'anonymous'=> 0,
      'form_location'=>1,
      'preview'=> 1,
      'comment_type'=>'image_comment',
      'locked'=>false,

      ))
->setDefaultValue(
        array(
            'status'=>2,
            'cid'=>0,
            'last_comment_timestamp'=> 0,
            'last_comment_name'=> null,
            'last_comment_uid'=> 0,
            'comment_count'=> 0,
)
)
->setDisplayOptions('form', array(
 'type' => 'comment_default',
 'settings' => array(
   'form_location' => 1,
   'default_mode'=> 1,
   'per_page'=>50,
   'anonymous'=> 0,
   'form_location'=>1,
   'preview'=> 1,
   'comment_type'=>'image_comment',
   'locked'=>false,

 ),
 'weight' => 1,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
1
+25

In order to any changes on BaseFieldDefinition effects, you should uninstall/install your module. your syntax seems Ok and I only guest you did some changes after your module( entity) installed.

1
  • I've already uninstall installed it several times.
    – cirtrus
    Nov 8 '16 at 9:29
0

use this to create the field in your custum module:

$fields['comments'] =  BaseFieldDefinition::create('comment')
      ->setLabel(t('comment'))
      ->setDescription(t('Add new comment'))
      ->setSettings(
        array(
          'default_mode'=> 1,
          'per_page'=>50,
          'anonymous'=> 0,
          'form_location'=>1,
          'preview'=> 1,
          'comment_type'=>'your_comment_type',
          'locked'=>false,
          'status'=>2
        ))
      ->setDefaultValue(
        array(
          'last_comment_timestamp'=> 0,
          'last_comment_name'=> null,
          'last_comment_uid'=> 0,
          'comment_count'=> 0
        )
      )
      ->setDisplayOptions('form', array(
        'type' => 'comment',
        'settings' => array(
          'form_location' => 1,
          'default_mode'=> 1,
          'per_page'=>50,
          'anonymous'=> 0,
          'form_location'=>1,
          'preview'=> 1,
          'locked'=>false
        ),
        'weight' => 1,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

you can get the form in your module.page.inc and send it in your template:

$form = \Drupal::service('comment.lazy_builders')->renderForm(entity_type_id,entity_id,'comments',comment_type);
$variables['yourVarialbeInTemplate']=$form;
0

I have tried with BaseFieldDefinition ,to create comment field for one of custom Entity using hook_update_N. But it is not creating Field Storage. So create Comment storage first and added comment field to entity.

// Create storage defention for comment field for entity.
  $field_storage_config_storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
  $field_storage_config_storage->create([
    'entity_type' => $entity_type_id,
    'field_name' => $field_name,
    'type' => 'comment',
    'translatable' => TRUE,
    'settings' => [
      'comment_type' => $comment_type_id,
    ],
  ])->save();

  $field_config_storage = \Drupal::entityTypeManager()->getStorage('field_config');
  $default_value = CommentItemInterface::OPEN;
  $field_config_storage->create([
    'label' => 'Comments',
    'description' => '',
    'field_name' => $field_name,
    'entity_type' => $entity_type_id,
    'bundle' => $entity_type_id,
    'required' => 0,
    'default_value' => [
      [
        'status' => $default_value,
        '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.
  entity_get_form_display($entity_type_id, $entity_type_id, 'default')
    ->setComponent($field_name, [
      'type' => 'comment_default',
      'weight' => 20,
    ])
    ->save();
0

One thing I noticed that may help someone. If you have a custom entity, and you have instances of those entities already in existence. And if you add a comment field the 'status' field will be set to "Closed" for all existing entities. Take for example:

    $fields['comment'] = BaseFieldDefinition::create('comment')
      ->setLabel(t('Task comments'))
      ->setSetting('comment_type', 'task_comments')
      ->setDefaultValue([
        'status' => CommentItemInterface::OPEN,
      ])
      ->setDisplayOptions('view', [
        'type' => 'comment_default',
        'settings' => ['view_mode' => 'default'],
      ]);

Even though the setDefaultValue for status is set to open. Any existing entities may be set to "closed". So you may need some sort of script to go back through the existing entities to turn comments to "open" on the field. Not sure how to do that since my solution was to delete my custom entities and start over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.