Chris@0: getStorage('comment_type'); Chris@0: if ($comment_type = $comment_type_storage->load($comment_type_id)) { Chris@0: if ($comment_type->getTargetEntityTypeId() !== $entity_type) { Chris@0: throw new \InvalidArgumentException("The given comment type id $comment_type_id can only be used with the $entity_type entity type"); Chris@0: } Chris@0: } Chris@0: else { Chris@0: $comment_type_storage->create([ Chris@0: 'id' => $comment_type_id, Chris@0: 'label' => Unicode::ucfirst($comment_type_id), Chris@0: 'target_entity_type_id' => $entity_type, Chris@0: 'description' => 'Default comment field', Chris@0: ])->save(); Chris@0: } Chris@0: // Add a body field to the comment type. Chris@0: \Drupal::service('comment.manager')->addBodyField($comment_type_id); Chris@0: Chris@0: // Add a comment field to the host entity type. Create the field storage if Chris@0: // needed. Chris@0: if (!array_key_exists($field_name, $entity_manager->getFieldStorageDefinitions($entity_type))) { Chris@0: $entity_manager->getStorage('field_storage_config')->create([ Chris@0: 'entity_type' => $entity_type, Chris@0: 'field_name' => $field_name, Chris@0: 'type' => 'comment', Chris@0: 'translatable' => TRUE, Chris@0: 'settings' => [ Chris@0: 'comment_type' => $comment_type_id, Chris@0: ], Chris@0: ])->save(); Chris@0: } Chris@0: // Create the field if needed, and configure its form and view displays. Chris@0: if (!array_key_exists($field_name, $entity_manager->getFieldDefinitions($entity_type, $bundle))) { Chris@0: $entity_manager->getStorage('field_config')->create([ Chris@0: 'label' => 'Comments', Chris@0: 'description' => '', Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => $entity_type, Chris@0: 'bundle' => $bundle, Chris@0: 'required' => 1, Chris@0: 'default_value' => [ Chris@0: [ Chris@0: 'status' => $default_value, Chris@0: 'cid' => 0, Chris@0: 'last_comment_name' => '', Chris@0: 'last_comment_timestamp' => 0, Chris@0: 'last_comment_uid' => 0, Chris@0: ], Chris@0: ], Chris@0: ])->save(); Chris@0: Chris@0: // Entity form displays: assign widget settings for the 'default' form Chris@0: // mode, and hide the field in all other form modes. Chris@0: entity_get_form_display($entity_type, $bundle, 'default') Chris@0: ->setComponent($field_name, [ Chris@0: 'type' => 'comment_default', Chris@0: 'weight' => 20, Chris@0: ]) Chris@0: ->save(); Chris@18: foreach ($entity_display_repository->getFormModes($entity_type) as $id => $form_mode) { Chris@0: $display = entity_get_form_display($entity_type, $bundle, $id); Chris@0: // Only update existing displays. Chris@0: if ($display && !$display->isNew()) { Chris@0: $display->removeComponent($field_name)->save(); Chris@0: } Chris@0: } Chris@0: Chris@0: // Entity view displays: assign widget settings for the 'default' view Chris@0: // mode, and hide the field in all other view modes. Chris@0: entity_get_display($entity_type, $bundle, 'default') Chris@0: ->setComponent($field_name, [ Chris@0: 'label' => 'above', Chris@0: 'type' => 'comment_default', Chris@0: 'weight' => 20, Chris@0: 'settings' => ['view_mode' => $comment_view_mode], Chris@0: ]) Chris@0: ->save(); Chris@18: foreach ($entity_display_repository->getViewModes($entity_type) as $id => $view_mode) { Chris@0: $display = entity_get_display($entity_type, $bundle, $id); Chris@0: // Only update existing displays. Chris@0: if ($display && !$display->isNew()) { Chris@0: $display->removeComponent($field_name)->save(); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: }