comment.install 6.18 KB
Newer Older
1 2
<?php

3 4
/**
 * @file
5
 * Install, update and uninstall functions for the Comment module.
6 7
 */

8
use Drupal\Core\Entity\EntityTypeInterface;
9 10
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
11
use Drupal\field\Entity\FieldStorageConfig;
12

13
/**
14
 * Implements hook_uninstall().
15 16
 */
function comment_uninstall() {
17
  // Remove the comment fields.
18
  $fields = entity_load_multiple_by_properties('field_storage_config', array('type' => 'comment'));
19 20
  foreach ($fields as $field) {
    $field->delete();
21
  }
22

23
  // Remove state setting.
24
  \Drupal::state()->delete('comment.node_comment_statistics_scale');
25 26
}

27
/**
28
 * Implements hook_install().
29
 */
30
function comment_install() {
31
  // By default, maintain entity statistics for comments.
32
  // @see \Drupal\comment\CommentStatisticsInterface
33
  \Drupal::state()->set('comment.maintain_entity_statistics', TRUE);
34 35
}

36
/**
37
 * Implements hook_schema().
38 39
 */
function comment_schema() {
40 41
  $schema['comment_entity_statistics'] = array(
    'description' => 'Maintains statistics of entity and comments posts to show "new" and "updated" flags.',
42
    'fields' => array(
43
      'entity_id' => array(
44 45 46 47
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
48 49 50
        'description' => 'The entity_id of the entity for which the statistics are compiled.',
      ),
      'entity_type' => array(
51
        'type' => 'varchar_ascii',
52 53
        'not null' => TRUE,
        'default' => 'node',
54
        'length' => EntityTypeInterface::ID_MAX_LENGTH,
55 56
        'description' => 'The entity_type of the entity to which this comment is a reply.',
      ),
57
      'field_name' => array(
58
        'type' => 'varchar_ascii',
59
        'not null' => TRUE,
60
        'default' => '',
61
        'length' => FieldStorageConfig::NAME_MAX_LENGTH,
62
        'description' => 'The field_name of the field that was used to add this comment.',
63
      ),
64 65 66 67 68 69
      'cid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {comment}.cid of the last comment.',
      ),
70 71 72 73
      'last_comment_timestamp' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
74
        'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
75 76 77 78 79
      ),
      'last_comment_name' => array(
        'type' => 'varchar',
        'length' => 60,
        'not null' => FALSE,
80
        'description' => 'The name of the latest author to post a comment on this node, from {comment}.name.',
81 82 83
      ),
      'last_comment_uid' => array(
        'type' => 'int',
84
        'unsigned' => TRUE,
85 86
        'not null' => TRUE,
        'default' => 0,
87
        'description' => 'The user ID of the latest author to post a comment on this node, from {comment}.uid.',
88 89 90 91 92 93
      ),
      'comment_count' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
94
        'description' => 'The total number of comments on this entity.',
95 96
      ),
    ),
97
    'primary key' => array('entity_id', 'entity_type', 'field_name'),
98
    'indexes' => array(
99
      'last_comment_timestamp' => array('last_comment_timestamp'),
100
      'comment_count' => array('comment_count'),
101
      'last_comment_uid' => array('last_comment_uid'),
102
    ),
103
    'foreign keys' => array(
104 105 106 107 108 109
      'last_comment_author' => array(
        'table' => 'users',
        'columns' => array(
          'last_comment_uid' => 'uid',
        ),
      ),
110
    ),
111 112 113
  );

  return $schema;
114
}
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

/**
 * @addtogroup updates-8.0.0-rc
 * @{
 */

/**
 * 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.
}

/**
 * @} End of "addtogroup updates-8.0.0-rc".
 */
132 133 134 135 136 137 138

/**
 * Clear caches to fix Comment Views context filter.
 */
function comment_update_8002() {
  // Empty update to cause a cache flush.
}
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

/**
 * @addtogroup updates-8.2.x
 * @{
 */

/**
 * 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.');
  }
}

/**
 * @} End of "addtogroup updates-8.2.x".
 */
182 183 184 185 186 187 188 189 190 191

/**
 * @addtogroup updates-8.3.x
 * @{
 */

/**
 * Update status field.
 */
function comment_update_8300() {
192 193 194 195 196
  $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);
197 198
}

199 200 201 202 203 204 205 206 207 208 209 210
/**
 * 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);
}

211 212 213
/**
 * @} End of "addtogroup updates-8.3.x".
 */