Enable comments on assets and logs.

This commit is contained in:
Michael Stenta 2023-04-05 11:58:30 -04:00
parent 4da5ba6ce4
commit 8a5483d1d7
10 changed files with 270 additions and 0 deletions

View File

@ -0,0 +1,10 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_comment
id: asset_comment
label: 'Asset comment'
target_entity_type_id: asset
description: ''

View File

@ -0,0 +1,10 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_comment
id: log_comment
label: 'Log comment'
target_entity_type_id: log
description: ''

View File

@ -0,0 +1,31 @@
langcode: en
status: true
dependencies:
config:
- comment.type.asset_comment
- field.field.comment.asset_comment.comment_body
enforced:
module:
- farm_comment
module:
- text
id: comment.asset_comment.default
targetEntityType: comment
bundle: asset_comment
mode: default
content:
author:
weight: 0
region: content
settings: { }
third_party_settings: { }
comment_body:
type: text_textarea
weight: 1
region: content
settings:
rows: 5
placeholder: ''
third_party_settings: { }
hidden:
subject: true

View File

@ -0,0 +1,31 @@
langcode: en
status: true
dependencies:
config:
- comment.type.log_comment
- field.field.comment.log_comment.comment_body
enforced:
module:
- farm_comment
module:
- text
id: comment.log_comment.default
targetEntityType: comment
bundle: log_comment
mode: default
content:
author:
weight: 0
region: content
settings: { }
third_party_settings: { }
comment_body:
type: text_textarea
weight: 1
region: content
settings:
rows: 5
placeholder: ''
third_party_settings: { }
hidden:
subject: true

View File

@ -0,0 +1,29 @@
langcode: en
status: true
dependencies:
config:
- comment.type.asset_comment
- field.field.comment.asset_comment.comment_body
enforced:
module:
- farm_comment
module:
- text
id: comment.asset_comment.default
targetEntityType: comment
bundle: asset_comment
mode: default
content:
comment_body:
type: text_default
label: hidden
settings: { }
third_party_settings: { }
weight: 0
region: content
links:
settings: { }
third_party_settings: { }
weight: 1
region: content
hidden: { }

View File

@ -0,0 +1,29 @@
langcode: en
status: true
dependencies:
config:
- comment.type.log_comment
- field.field.comment.log_comment.comment_body
enforced:
module:
- farm_comment
module:
- text
id: comment.log_comment.default
targetEntityType: comment
bundle: log_comment
mode: default
content:
comment_body:
type: text_default
label: hidden
settings: { }
third_party_settings: { }
weight: 0
region: content
links:
settings: { }
third_party_settings: { }
weight: 1
region: content
hidden: { }

View File

@ -0,0 +1,23 @@
langcode: en
status: true
dependencies:
config:
- comment.type.asset_comment
- field.storage.comment.comment_body
enforced:
module:
- farm_comment
module:
- text
id: comment.asset_comment.comment_body
field_name: comment_body
entity_type: comment
bundle: asset_comment
label: Comment
description: ''
required: true
translatable: true
default_value: { }
default_value_callback: ''
settings: { }
field_type: text_long

View File

@ -0,0 +1,23 @@
langcode: en
status: true
dependencies:
config:
- comment.type.log_comment
- field.storage.comment.comment_body
enforced:
module:
- farm_comment
module:
- text
id: comment.log_comment.comment_body
field_name: comment_body
entity_type: comment
bundle: log_comment
label: Comment
description: ''
required: true
translatable: true
default_value: { }
default_value_callback: ''
settings: { }
field_type: text_long

View File

@ -5,3 +5,5 @@ package: farmOS
core_version_requirement: ^9
dependencies:
- drupal:comment
- farm:asset
- log:log

View File

@ -0,0 +1,82 @@
<?php
/**
* @file
* Contains farm_comment.module.
*/
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\entity\BundleFieldDefinition;
/**
* Implements hook_entity_base_field_info().
*/
function farm_comment_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
// Add comment base field to assets and logs.
if (in_array($entity_type->id(), ['asset', 'log'])) {
$fields['comment'] = farm_comment_base_field_definition($entity_type->id());
}
return $fields;
}
/**
* Helper function for generating a standard comment base field definition.
*
* @param string $entity_type
* The entity type.
*
* @return \Drupal\Core\Field\BaseFieldDefinition
* Returns a comment base field definition.
*/
function farm_comment_base_field_definition(string $entity_type) {
// Create a new comment field definition.
// We use BundleFieldDefinition instead of BaseFieldDefinition to force Drupal
// to create a separate database table for this field. Otherwise, if it is
// added to the base table then the comment field default value is always 0
// (CommentItemInterface::HIDDEN) instead of 2 (CommentItemInterface::OPEN),
// because the Drupal\comment\Plugin\Field\FieldType\CommentItem::schema()
// default is 0.
$field = BundleFieldDefinition::create('comment');
// Set the field label.
$field->setLabel(t('Comments'));
// We assume that the comment type is "[entity-type]_comment".
$field->setSetting('comment_type', $entity_type . '_comment');
// A default value must be set for comment fields.
// Enable comments on entities by default.
$default_value = [
[
'status' => CommentItemInterface::OPEN,
'cid' => 0,
'last_comment_timestamp' => 0,
'last_comment_name' => '',
'last_comment_uid' => 0,
'comment_count' => 0,
],
];
$field->setDefaultValue($default_value);
// Build form display settings.
$field->setDisplayOptions('form', [
'type' => 'comment_default',
'weight' => 1000,
]);
// Build view display settings.
// Display comments on the bottom of entity view displays by default, with the
// field label above them.
$field->setDisplayOptions('view', [
'label' => 'above',
'type' => 'comment_default',
'weight' => 1000,
]);
return $field;
}