message_thread-8.x-1.x-dev/message_thread.install
message_thread.install
<?php /** * @file * Message Thread installer. */ use Drupal\Core\Database\Database; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\field\Entity\FieldStorageConfig; /** * Implements hook_install(). */ function message_thread_install() { // By default, maintain entity statistics for messages. // @see \Drupal\message_thread\MessageStatisticsInterface \Drupal::state()->set('message.maintain_entity_statistics', TRUE); } /** * Implements hook_schema(). */ function message_thread_schema() { $schema['message_thread_statistics'] = [ 'description' => 'Maintains statistics of message threads and messages to show "new" and "updated" flags.', 'fields' => [ 'entity_id' => [ 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => 'The entity_id of the entity for which the statistics are compiled.', ], 'entity_type' => [ 'type' => 'varchar_ascii', 'not null' => TRUE, 'default' => 'message_thread', 'length' => EntityTypeInterface::ID_MAX_LENGTH, 'description' => 'The entity_type of the entity to which this message is a reply.', ], 'field_name' => [ 'type' => 'varchar_ascii', 'not null' => TRUE, 'default' => '', 'length' => FieldStorageConfig::NAME_MAX_LENGTH, 'description' => 'The field_name of the field that was used to add this message.', ], 'mid' => [ 'type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => 'The {message}.mid of the last message.', ], 'last_message_timestamp' => [ 'type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => 'The Unix timestamp of the last message that was posted within this entity, from {message}.changed.', ], 'last_message_name' => [ 'type' => 'varchar', 'length' => 60, 'not null' => FALSE, 'description' => 'The name of the latest author to post a message on this message thread, from {message}.name.', ], 'last_message_uid' => [ 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => 'The user ID of the latest author to post a message on this message thread, from {message}.uid.', ], 'message_count' => [ 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => 'The total number of messages on this entity.', ], ], 'primary key' => ['entity_id', 'entity_type', 'field_name'], 'indexes' => [ 'last_message_timestamp' => ['last_message_timestamp'], 'message_count' => ['message_count'], 'last_message_uid' => ['last_message_uid'], ], 'foreign keys' => [ 'last_message_author' => [ 'table' => 'users', 'columns' => [ 'last_message_uid' => 'uid', ], ], ], ]; return $schema; } /** * Implements hook_uninstall(). * * Remove the index table. */ function message_thread_uninstall() { $schema = \Drupal::database()->schema(); $schema->dropTable('message_thread_index'); } /** * Implements hook_update_N(). * * Add new table for statistics. */ function message_thread_update_8002() { $schema = \Drupal::database()->schema(); if ($schema->tableExists('message_thread_statistics')) { return; } $schema_manager = Database::getConnection()->schema(); $schema = drupal_get_module_schema('message_thread'); $schema_manager->createTable('message_thread_statistics', $schema['message_thread_statistics']); } /** * Implements hook_update_N(). * * Turn on statistics. */ function message_thread_update_8003() { \Drupal::state()->set('message.maintain_entity_statistics', TRUE); } /** * Implements hook_update_N(). * * Add new field definitions. */ function message_thread_update_8001() { $entity_definition_update_manager = \Drupal::service('entity.definition_update_manager'); $field_definition = $entity_definition_update_manager->getFieldStorageDefinition('uid', 'message_thread'); $field_definition->setSetting( 'handler', 'default' ); $field_definition->setDisplayOptions('view', [ 'label' => 'above', 'type' => 'author', 'weight' => -3, ]); $field_definition->setDisplayOptions('form', [ 'type' => 'entity_reference_autocomplete', 'settings' => [ 'match_operator' => 'CONTAINS', 'size' => 60, 'autocomplete_type' => 'tags', 'placeholder' => '', ], 'weight' => -3, ]); $field_definition->setDisplayConfigurable('form', TRUE); $field_definition->setDisplayConfigurable('view', TRUE); $entity_definition_update_manager->updateFieldStorageDefinition($field_definition); $field_definition = $entity_definition_update_manager->getFieldStorageDefinition('created', 'message_thread'); $field_definition->setDisplayOptions('view', [ 'label' => 'hidden', 'type' => 'timestamp', 'weight' => 0, ]); $field_definition->setDisplayOptions('form', [ 'type' => 'datetime_timestamp', 'weight' => 10, ]); $field_definition->setDisplayConfigurable('form', TRUE); $field_definition->setDisplayConfigurable('view', TRUE); $entity_definition_update_manager->updateFieldStorageDefinition($field_definition); $entity_definition_update_manager->applyUpdates(); }