2006-12-13 10:24:58 +00:00
<?php
2009-05-13 19:42:18 +00:00
/**
* @file
2012-01-03 04:36:15 +00:00
* Install, update and uninstall functions for the Comment module.
2009-05-13 19:42:18 +00:00
*/
2018-05-11 22:44:22 +00:00
use Drupal\comment\Entity\Comment;
2014-07-11 10:37:14 +00:00
use Drupal\Core\Entity\EntityTypeInterface;
2016-07-19 13:47:30 +00:00
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
2014-07-18 18:56:27 +00:00
use Drupal\field\Entity\FieldStorageConfig;
2014-06-13 09:34:04 +00:00
2019-05-14 19:54:16 +00:00
/**
* Implements hook_requirements().
*/
function comment_requirements($phase) {
$requirements = [];
if ($phase === 'update' && drupal_get_installed_schema_version('comment') < 8701) {
$has_empty_columns = \Drupal::entityQuery('comment', 'OR')
->condition('entity_type', NULL, 'IS NULL')
->condition('field_name', NULL, 'IS NULL')
->range(0, 1)
->accessCheck(FALSE)
->execute();
if ($has_empty_columns) {
$requirements['comment_update_8701'] = [
'title' => t('Comment required fields update'),
'description' => t('The comment_update_8701() function requires that the %field_1 and %field_2 fields have values for all comment entities. See the <a href=":change_record">change record</a> for more information.', [
'%field_1' => 'entity_type',
'%field_2' => 'field_name',
':change_record' => 'https://www.drupal.org/node/3053046',
]),
'severity' => REQUIREMENT_ERROR,
];
}
}
return $requirements;
}
2009-01-19 01:23:39 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_uninstall().
2009-01-19 01:23:39 +00:00
*/
function comment_uninstall() {
2013-09-27 15:34:47 +00:00
// Remove the comment fields.
2019-04-30 07:41:05 +00:00
$storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
$fields = $storage->loadByProperties(['type' => 'comment']);
$storage->delete($fields);
2012-11-14 10:23:00 +00:00
2013-09-27 15:34:47 +00:00
// Remove state setting.
2013-09-16 03:58:06 +00:00
\Drupal::state()->delete('comment.node_comment_statistics_scale');
2009-01-19 01:23:39 +00:00
}
2006-12-13 10:24:58 +00:00
/**
2013-09-19 16:22:53 +00:00
* Implements hook_install().
2006-12-13 10:24:58 +00:00
*/
2013-09-19 16:22:53 +00:00
function comment_install() {
2013-09-27 15:34:47 +00:00
// By default, maintain entity statistics for comments.
2014-08-13 00:40:09 +00:00
// @see \Drupal\comment\CommentStatisticsInterface
2013-09-27 15:34:47 +00:00
\Drupal::state()->set('comment.maintain_entity_statistics', TRUE);
2007-07-15 10:05:18 +00:00
}
2007-10-05 14:43:26 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_schema().
2007-10-05 14:43:26 +00:00
*/
function comment_schema() {
2017-03-04 01:20:24 +00:00
$schema['comment_entity_statistics'] = [
2013-09-27 15:34:47 +00:00
'description' => 'Maintains statistics of entity and comments posts to show "new" and "updated" flags.',
2017-03-04 01:20:24 +00:00
'fields' => [
'entity_id' => [
2007-12-08 14:06:23 +00:00
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
2013-09-27 15:34:47 +00:00
'description' => 'The entity_id of the entity for which the statistics are compiled.',
2017-03-04 01:20:24 +00:00
],
'entity_type' => [
2015-05-05 16:42:09 +00:00
'type' => 'varchar_ascii',
2013-09-27 15:34:47 +00:00
'not null' => TRUE,
'default' => 'node',
2014-07-11 10:37:14 +00:00
'length' => EntityTypeInterface::ID_MAX_LENGTH,
2013-09-27 15:34:47 +00:00
'description' => 'The entity_type of the entity to which this comment is a reply.',
2017-03-04 01:20:24 +00:00
],
'field_name' => [
2015-05-05 16:42:09 +00:00
'type' => 'varchar_ascii',
2013-09-27 15:34:47 +00:00
'not null' => TRUE,
2014-06-13 09:34:04 +00:00
'default' => '',
2014-07-18 18:56:27 +00:00
'length' => FieldStorageConfig::NAME_MAX_LENGTH,
2014-06-13 09:34:04 +00:00
'description' => 'The field_name of the field that was used to add this comment.',
2017-03-04 01:20:24 +00:00
],
'cid' => [
2009-11-12 06:46:44 +00:00
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The {comment}.cid of the last comment.',
2017-03-04 01:20:24 +00:00
],
'last_comment_timestamp' => [
2007-12-08 14:06:23 +00:00
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2011-08-02 00:23:15 +00:00
'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
2017-03-04 01:20:24 +00:00
],
'last_comment_name' => [
2007-12-08 14:06:23 +00:00
'type' => 'varchar',
'length' => 60,
'not null' => FALSE,
2008-12-03 16:32:22 +00:00
'description' => 'The name of the latest author to post a comment on this node, from {comment}.name.',
2017-03-04 01:20:24 +00:00
],
'last_comment_uid' => [
2007-12-08 14:06:23 +00:00
'type' => 'int',
2012-10-04 20:27:03 +00:00
'unsigned' => TRUE,
2007-12-08 14:06:23 +00:00
'not null' => TRUE,
'default' => 0,
2008-12-03 16:32:22 +00:00
'description' => 'The user ID of the latest author to post a comment on this node, from {comment}.uid.',
2017-03-04 01:20:24 +00:00
],
'comment_count' => [
2007-12-08 14:06:23 +00:00
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
2013-09-27 15:34:47 +00:00
'description' => 'The total number of comments on this entity.',
2017-03-04 01:20:24 +00:00
],
],
'primary key' => ['entity_id', 'entity_type', 'field_name'],
'indexes' => [
'last_comment_timestamp' => ['last_comment_timestamp'],
'comment_count' => ['comment_count'],
'last_comment_uid' => ['last_comment_uid'],
],
'foreign keys' => [
'last_comment_author' => [
2010-08-22 13:55:53 +00:00
'table' => 'users',
2017-03-04 01:20:24 +00:00
'columns' => [
2010-08-22 13:55:53 +00:00
'last_comment_uid' => 'uid',
2017-03-04 01:20:24 +00:00
],
],
],
];
2007-10-05 14:43:26 +00:00
return $schema;
2009-10-10 13:37:11 +00:00
}
2015-11-02 15:17:52 +00:00
/**
* Clear caches to fix Comment entity list builder and operations Views field.
*/
function comment_update_8001() {
// Empty update to cause a cache flush to rebuild comment entity handler
// information, so that comment operation links work.
}
2016-04-04 02:41:53 +00:00
/**
* Clear caches to fix Comment Views context filter.
*/
function comment_update_8002() {
// Empty update to cause a cache flush.
}
2016-07-19 13:47:30 +00:00
/**
* Add the 'view_mode' setting to displays having 'comment_default' formatter.
*/
function comment_update_8200() {
$config_factory = \Drupal::configFactory();
$displays = [];
// Iterate on all entity view displays.
foreach ($config_factory->listAll('core.entity_view_display.') as $name) {
$changed = FALSE;
$display = $config_factory->getEditable($name);
$components = $display->get('content') ?: [];
foreach ($components as $field_name => $component) {
if (isset($component['type']) && ($component['type'] === 'comment_default')) {
if (empty($display->get("content.{$field_name}.settings.view_mode"))) {
$display->set("content.{$field_name}.settings.view_mode", 'default');
$displays[] = $display->get('id');
$changed = TRUE;
}
}
}
if ($changed) {
$display->save(TRUE);
}
}
if ($displays) {
return new PluralTranslatableMarkup(count($displays), '1 entity display updated: @displays.', '@count entity displays updated: @displays.', ['@displays' => implode(', ', $displays)]);
}
else {
return new TranslatableMarkup('No entity view display updated.');
}
}
2016-10-20 12:33:59 +00:00
/**
* Update status field.
*/
function comment_update_8300() {
2017-02-06 11:39:49 +00:00
$entity_definition_update_manager = \Drupal::service('entity.definition_update_manager');
$field_definition = $entity_definition_update_manager->getFieldStorageDefinition('status', 'comment');
$field_definition->setDescription(new TranslatableMarkup('A boolean indicating the published state.'))
->setRevisionable(TRUE);
$entity_definition_update_manager->updateFieldStorageDefinition($field_definition);
2016-10-20 12:33:59 +00:00
}
Issue #2789315 by amateescu, timmillwood, claudiu.cristea, sandervd, GroovyCarrot, catch, Wim Leers, Berdir, pfrenssen, twistor, xjm: Create EntityPublishedInterface and use for Node and Comment
2016-12-02 22:18:44 +00:00
/**
* Set the 'published' entity key.
*/
function comment_update_8301() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $definition_update_manager->getEntityType('comment');
$keys = $entity_type->getKeys();
$keys['published'] = 'status';
$entity_type->set('entity_keys', $keys);
$definition_update_manager->updateEntityType($entity_type);
}
2017-07-20 15:10:48 +00:00
/**
* Update the status field.
*/
function comment_update_8400() {
// The status field was promoted to an entity key in comment_update_8301(),
// which makes it NOT NULL in the default SQL storage, which means its storage
// definition needs to be updated as well.
$entity_definition_update_manager = \Drupal::service('entity.definition_update_manager');
$entity_definition_update_manager->updateFieldStorageDefinition($entity_definition_update_manager->getFieldStorageDefinition('status', 'comment'));
}
2018-05-11 22:44:22 +00:00
/**
* Configure the comment hostname base field to use a default value callback.
*/
function comment_update_8600() {
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
/** @var \Drupal\Core\Field\BaseFieldDefinition $field_storage_definition */
$field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition('hostname', 'comment');
$field_storage_definition->setDefaultValueCallback(Comment::class . '::getDefaultHostname');
$entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
}
2018-09-12 21:27:17 +00:00
/**
* Set the 'owner' entity key and update the field.
*/
2018-09-13 11:20:44 +00:00
function comment_update_8700() {
2018-09-12 21:27:17 +00:00
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $definition_update_manager->getEntityType('comment');
$keys = $entity_type->getKeys();
$keys['owner'] = 'uid';
$entity_type->set('entity_keys', $keys);
$definition_update_manager->updateEntityType($entity_type);
$definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'comment'));
}
2018-11-26 12:21:16 +00:00
/**
* Make the 'entity_type' and 'field_name' comment fields required.
*/
function comment_update_8701() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$field_definition = $definition_update_manager->getFieldStorageDefinition('entity_type', 'comment');
$field_definition->setRequired(TRUE);
$definition_update_manager->updateFieldStorageDefinition($field_definition);
$field_definition = $definition_update_manager->getFieldStorageDefinition('field_name', 'comment');
$field_definition->setRequired(TRUE);
$definition_update_manager->updateFieldStorageDefinition($field_definition);
}