1

I want to load entity form for comment entity type.

I can load entity form for node (entity type = node and bundle = article).

But the entity type = comment and bundle = comment.

Here is the code of loading entity:

  $values = [];
    $definition = \Drupal::entityManager()->getDefinition("node");
    $bundle_key = $definition->get('entity_keys')['bundle'];
    $values['entity_type'] = "node";
    $values[$bundle_key] = "article";
    $entity = \Drupal::entityManager()->getStorage($definition->get('id'))
      ->create($values);
   return \Drupal::service('entity.form_builder')->getForm($entity);

=> This example works well.

But if i replace entity type with comment and bundle with comment, not work :(.

        $values = [];
        $definition = \Drupal::entityManager()->getDefinition("comment");
        $bundle_key = $definition->get('entity_keys')['bundle'];
        $values['entity_type'] = "comment";
        $values[$bundle_key] = "comment";
        $entity = \Drupal::entityManager()->getStorage($definition->get('id'))
          ->create($values);
       return \Drupal::service('entity.form_builder')->getForm($entity);
  • See \Drupal\Core\Entity\EntityFormBuilder::getForm() – user21641 Nov 17 '15 at 14:04
  • I used the EntityFormBuilder injected in the service 'entity.form_builder',but, my question how to load entity form comment. – Anis Taktak Nov 17 '15 at 16:05
  • Maybe this $values[$bundle_key] = "comment"; ....doesn't comments have bundles? Sorry, I haven't touched comments in D8 yet so I'm not sure. – user21641 Nov 17 '15 at 16:19
0

it also needs the field name I think, check CommentDefaultFormatter::viewElements

https://api.drupal.org/api/drupal/core%21modules%21comment%21src%21Plugin%21Field%21FieldFormatter%21CommentDefaultFormatter.php/class/CommentDefaultFormatter/8

It uses a lazy builder so it can be built after render caching

$output['comment_form'] = [ '#lazy_builder' => ['comment.lazy_builders:renderForm', [ $entity->getEntityTypeId(), $entity->id(), $field_name, $this->getFieldSetting('comment_type'), ]], '#create_placeholder' => TRUE, ];

0

Yes, I found the answer to the question:

The entity type comment must be related to another entity and the other entity must have a field ( type = comment).

So here is the code for the entity form for comment related to the entity user:

$values = [];
$definition = \Drupal::entityTypeManager()->getDefinition("comment");
$bundle_key = $definition->get('entity_keys')['bundle'];
$values['entity_type'] = "user";
$values['field_name'] = "field_comments";
// comment type = comment_user(for entity type : user)
$values[$bundle_key] = "comment_user";
// user/1
$values['entity_id'] = array(
  'target_id' => 1,
);
$entity = \Drupal::entityTypeManager()->getStorage($definition->get('id'))
  ->create($values);


 return \Drupal::service('entity.form_builder')->getForm($entity);
0

I had the same problem. I got the solution.

For Node :

function d8_points_receipt_form_submit($form, FormStateInterface $form_state) {
    $user_input = $form_state->getUserInput();
    $selected = array_filter($user_input['node_bulk_form']);
    $connection = Database::getConnection();
      if($form_state->getValue('action') == "node_publish_action") {
        foreach ($selected as $bulk_form_key) {
          $key_json_value = base64_decode($bulk_form_key);
          $key_ids = json_decode($key_json_value);
          $node_ids = array_pop($key_ids);
          $node_storage = \Drupal::entityTypeManager()->getStorage('node');
          $node = $node_storage->load($node_ids);
          $node_creator_id = $node->getOwnerId();
          $result = $connection->insert('d8_points_new_table')
          ->fields([
          'uid' => $node_creator_id,
          'title' => $node->get('title')->value,            
          'info_type' => 'receipt',
          'action_points' => $node->get('field_point_for_receipt_upload')->value,
          ])
            ->execute();
          }
        } 
  }

For Comment :

function d8_points_comment_form_submit($form, FormStateInterface $form_state) {
    drupal_set_message('come on');
    $user_input = $form_state->getUserInput();
    $selected = array_filter($user_input['comment_bulk_form']);
    $connection = Database::getConnection();
      if($form_state->getValue('action') == "comment_publish_action") {
        drupal_set_message('cnd ok come on');
        foreach ($selected as $bulk_form_key) {
          $key_json_value = base64_decode($bulk_form_key);
          $key_ids = json_decode($key_json_value);
          $comment_ids = array_pop($key_ids);
          $comment = \Drupal::entityManager()->getStorage('comment')->load($comment_ids);
          $comment_creator_id = $comment->getOwnerId();
          $result = $connection->insert('d8_points_new_table')
          ->fields([
          'uid' => $comment_creator_id,
          'title' => $comment->get('subject')->value,            
          'info_type' => 'comment',
          'action_points' => $comment->get('field_point_for_comment')->value,
          ])
            ->execute();
          }
        } 
  }

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.