addBodyField example


  public function save(array $form, FormStateInterface $form_state) {
    $comment_type = $this->entity;
    $status = $comment_type->save();

    $edit_link = $this->entity->toLink($this->t('Edit'), 'edit-form')->toString();
    if ($status == SAVED_UPDATED) {
      $this->messenger()->addStatus($this->t('Comment type %label has been updated.', ['%label' => $comment_type->label()]));
      $this->logger->notice('Comment type %label has been updated.', ['%label' => $comment_type->label(), 'link' => $edit_link]);
    }
    else {
      $this->commentManager->addBodyField($comment_type->id());
      $this->messenger()->addStatus($this->t('Comment type %label has been added.', ['%label' => $comment_type->label()]));
      $this->logger->notice('Comment type %label has been added.', ['%label' => $comment_type->label(), 'link' => $edit_link]);
    }

    $form_state->setRedirectUrl($comment_type->toUrl('collection'));
  }

}

class EntityCommentType extends EntityConfigBase {

  /** * {@inheritdoc} */
  public function import(Row $row, array $old_destination_id_values = []) {
    $entity_ids = parent::import($row$old_destination_id_values);
    \Drupal::service('comment.manager')->addBodyField(reset($entity_ids));
    return $entity_ids;
  }

}

    }
    else {
      $comment_type_storage->create([
        'id' => $comment_type_id,
        'label' => Unicode::ucfirst($comment_type_id),
        'target_entity_type_id' => $entity_type,
        'description' => 'Default comment field',
      ])->save();
    }
    // Add a body field to the comment type.     \Drupal::service('comment.manager')->addBodyField($comment_type_id);

    // Add a comment field to the host entity type. Create the field storage if     // needed.     if (!array_key_exists($field_name$entity_field_manager->getFieldStorageDefinitions($entity_type))) {
      $entity_type_manager->getStorage('field_storage_config')->create([
        'entity_type' => $entity_type,
        'field_name' => $field_name,
        'type' => 'comment',
        'translatable' => TRUE,
        'settings' => [
          'comment_type' => $comment_type_id,
        ],
Home | Imprint | This part of the site doesn't use cookies.