getCommentedEntityTypeId example

/** * {@inheritdoc} */
  public function getItems(ResultRow $values) {
    if (empty($this->loadedCommentedEntities)) {
      $result = $this->view->result;

      $entity_ids_per_type = [];
      foreach ($result as $value) {
        /** @var \Drupal\comment\CommentInterface $comment */
        if ($comment = $this->getEntity($value)) {
          $entity_ids_per_type[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
        }
      }

      foreach ($entity_ids_per_type as $type => $ids) {
        $this->loadedCommentedEntities[$type] = $this->entityTypeManager->getStorage($type)->loadMultiple($ids);
      }
    }

    return parent::getItems($values);
  }

}
/** * Tests the migrated comments. */
  public function testMigration() {
    $comment = Comment::load(1);
    $this->assertSame('The first comment.', $comment->getSubject());
    $this->assertSame('The first comment body.', $comment->comment_body->value);
    $this->assertSame('filtered_html', $comment->comment_body->format);
    $this->assertNull($comment->pid->target_id);
    $this->assertSame('1', $comment->getCommentedEntityId());
    $this->assertSame('node', $comment->getCommentedEntityTypeId());
    $this->assertSame('en', $comment->language()->getId());
    $this->assertSame('comment_node_story', $comment->getTypeId());
    $this->assertSame('203.0.113.1', $comment->getHostname());

    $node = $comment->getCommentedEntity();
    $this->assertInstanceOf(NodeInterface::class$node);
    $this->assertSame('1', $node->id());

    $comment = Comment::load(2);
    $this->assertSame('The response to the second comment.', $comment->subject->value);
    $this->assertSame('3', $comment->pid->target_id);
    
$container->get('entity_type.manager')
    );
  }

  /** * {@inheritdoc} */
  public function getMaxThread(CommentInterface $comment) {
    $query = $this->database->select($this->getDataTable(), 'c')
      ->condition('entity_id', $comment->getCommentedEntityId())
      ->condition('field_name', $comment->getFieldName())
      ->condition('entity_type', $comment->getCommentedEntityTypeId())
      ->condition('default_langcode', 1);
    $query->addExpression('MAX([thread])', 'thread');
    return $query->execute()
      ->fetchField();
  }

  /** * {@inheritdoc} */
  public function getMaxThreadPerThread(CommentInterface $comment) {
    $query = $this->database->select($this->getDataTable(), 'c')
      
$this->currentUser = $current_user;
    $this->renderer = $renderer;
    $this->entityFieldManager = $entity_field_manager ?: \Drupal::service('entity_field.manager');
  }

  /** * {@inheritdoc} */
  public function form(array $form, FormStateInterface $form_state) {
    /** @var \Drupal\comment\CommentInterface $comment */
    $comment = $this->entity;
    $entity = $this->entityTypeManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId());
    $field_name = $comment->getFieldName();
    $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId()$entity->bundle())[$comment->getFieldName()];
    $config = $this->config('user.settings');

    // In several places within this function, we vary $form on:     // - The current user's permissions.     // - Whether the current user is authenticated or anonymous.     // - The 'user.settings' configuration.     // - The comment field's definition.     $form['#cache']['contexts'][] = 'user.permissions';
    $form['#cache']['contexts'][] = 'user.roles:authenticated';
    
$links['comment-edit'] = [
          'title' => t('Edit'),
          'url' => $entity->toUrl('edit-form'),
        ];
      }
      $field_definition = $commented_entity->getFieldDefinition($entity->getFieldName());
      if ($entity->access('create')
        && $field_definition->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED) {
        $links['comment-reply'] = [
          'title' => t('Reply'),
          'url' => Url::fromRoute('comment.reply', [
            'entity_type' => $entity->getCommentedEntityTypeId(),
            'entity' => $entity->getCommentedEntityId(),
            'field_name' => $entity->getFieldName(),
            'pid' => $entity->id(),
          ]),
        ];
      }
      if (!$entity->isPublished() && $entity->access('approve')) {
        $links['comment-approve'] = [
          'title' => t('Approve'),
          'url' => Url::fromRoute('comment.approve', ['comment' => $entity->id()]),
        ];
      }
// Historically, comments were always linked to 'node' entities, but that is     // no longer the case, as the 'node' module might not even be enabled.     // Comments can now be linked to any entity and they can also be referenced     // by other entities, so we won't have a single table to join to. That     // actually means that we can no longer optimize the query on those cases.     // However, the most common case remains to be comment replies, and in this     // case, we can get the host entity type if the 'entity' value is present     // and perform the extra joins and alterations needed.     $comment = $this->getConfiguration()['entity'];
    if ($comment instanceof CommentInterface) {
      $host_entity_type_id = $comment->getCommentedEntityTypeId();

      /** @var \Drupal\Core\Entity\EntityTypeInterface $host_entity_type */
      $host_entity_type = $this->entityTypeManager->getDefinition($host_entity_type_id);
      $host_entity_field_data_table = $host_entity_type->getDataTable();

      // Not all entities have a data table, so check first.       if ($host_entity_field_data_table) {
        $id_key = $host_entity_type->getKey('id');

        // The Comment module doesn't implement per-comment access, so it         // checks instead that the user has access to the host entity.
/** * {@inheritdoc} */
  protected function getUrlInfo(ResultRow $row) {
    /** @var \Drupal\comment\CommentInterface $comment */
    $comment = $this->getEntity($row);
    if (!$comment) {
      return NULL;
    }
    return Url::fromRoute('comment.reply', [
      'entity_type' => $comment->getCommentedEntityTypeId(),
      'entity' => $comment->getCommentedEntityId(),
      'field_name' => $comment->getFieldName(),
      'pid' => $comment->id(),
    ]);
  }

  /** * {@inheritdoc} */
  protected function getDefaultLabel() {
    return $this->t('Reply');
  }

  public function update(CommentInterface $comment) {
    // Allow bulk updates and inserts to temporarily disable the maintenance of     // the {comment_entity_statistics} table.     if (!$this->state->get('comment.maintain_entity_statistics')) {
      return;
    }

    $query = $this->database->select('comment_field_data', 'c');
    $query->addExpression('COUNT([cid])');
    $count = $query->condition('c.entity_id', $comment->getCommentedEntityId())
      ->condition('c.entity_type', $comment->getCommentedEntityTypeId())
      ->condition('c.field_name', $comment->getFieldName())
      ->condition('c.status', CommentInterface::PUBLISHED)
      ->condition('default_langcode', 1)
      ->execute()
      ->fetchField();

    if ($count > 0) {
      // Comments exist.       $last_reply = $this->database->select('comment_field_data', 'c')
        ->fields('c', ['cid', 'name', 'changed', 'uid'])
        ->condition('c.entity_id', $comment->getCommentedEntityId())
        
/** @var \Drupal\comment\CommentInterface[] $comments */
    $comments = $this->commentStorage->loadMultiple($cids);

    // Build a table listing the appropriate comments.     $options = [];
    $destination = $this->getDestinationArray();

    $commented_entity_ids = [];
    $commented_entities = [];

    foreach ($comments as $comment) {
      $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
    }

    foreach ($commented_entity_ids as $entity_type => $ids) {
      $commented_entities[$entity_type] = $this->entityTypeManager
        ->getStorage($entity_type)
        ->loadMultiple($ids);
    }

    foreach ($comments as $comment) {
      /** @var \Drupal\Core\Entity\EntityInterface $commented_entity */
      $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()];
      
$this->assertSame('TNG for the win!', $comment->getSubject());
    $this->assertSame('TNG is better than DS9.', $comment->comment_body->value);
    $this->assertSame('en', $comment->language()->getId());

    // Tests that the commented entity is correctly migrated when the comment     // was posted to a node translation.     $comment = Comment::load(3);
    $this->assertInstanceOf(Comment::class$comment);
    $this->assertSame('Comment to IS translation', $comment->getSubject());
    $this->assertSame('This is a comment to an Icelandic translation.', $comment->comment_body->value);
    $this->assertSame('2', $comment->getCommentedEntityId());
    $this->assertSame('node', $comment->getCommentedEntityTypeId());
    $this->assertSame('is', $comment->language()->getId());

    $node = $comment->getCommentedEntity();
    $this->assertInstanceOf(NodeInterface::class$node);
    $this->assertSame('2', $node->id());

    // Tests a comment migrated from Drupal 6 to Drupal 7 that did not have a     // language.     $comment = Comment::load(4);
    $this->assertInstanceOf(Comment::class$comment);
    $this->assertSame('Comment without language', $comment->getSubject());
    
Home | Imprint | This part of the site doesn't use cookies.