Skip to content

Instantly share code, notes, and snippets.

@BBGuy
Last active December 10, 2021 13:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BBGuy/3a6663cfee360f35eb2c3b5550b2bcf0 to your computer and use it in GitHub Desktop.
Save BBGuy/3a6663cfee360f35eb2c3b5550b2bcf0 to your computer and use it in GitHub Desktop.
Drupal 8, 9 Schemas
<?php
use Drupal\Core\Database\Database;
/**
* Implements hook_schema().
*
* Notes:
* 'not null' = requiered
*/
function d8_my_module_schema() {
$schema['demo_schema_table'] = [
'description' => 'Sample of field types',
'fields' => [
'demo_id' => [
'description' => 'Auto increment ID',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'demo_string' => [
'description' => 'Optional string up to 128 characters',
'type' => 'varchar_ascii',
'not null' => FALSE,
'default' => '',
'length' => 128,
],
'demo_long_string' => [
'description' => 'Long string',
'type' => 'varchar_ascii',
'length' => \Drupal\Core\Entity\EntityTypeInterface::ID_MAX_LENGTH,
'not null' => TRUE,
],
'demo_code' => [
'description' => '3 characters code',
'type' => 'varchar',
'length' => 3,
],
'demo_int' => [
'description' => 'unsigned Intiger',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'unix_timestamp_filed' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp.',
],
'boolean' => [
'description' => 'boolean with a default of TRUE',
'type' => 'int',
'not null' => TRUE,
'default' => 1,
'size' => 'small',
],
'user_id' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The user ID.',
],
'demo_number' => [
'description' => 'Number from 0.00 to 99999999.99',
'type' => 'numeric',
'size' => 'normal',
'not null' => TRUE,
'default' => 0,
'precision' => 10,
'scale' => 2,
],
'data' => [
'description' => 'Serialized data array',
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
],
'primary key' => ['demo_id'],
'indexes' => [
'date' => [
'demo_date',
],
'multi_part' => [
'demo_string',
'demo_date'
],
],
'foreign keys' => [
'refrenced_user' => [
'table' => 'users',
'columns' => [
'user_id' => 'uid',
],
],
],
}
/**
* Update hook example of adding a schema table.
* source: https://www.drupal.org/docs/drupal-apis/update-api/updating-database-schema-andor-data-in-drupal-8
*/
function mymodule_update_8101() {
$spec = [
'description' => 'My description',
'fields' => [
'myfield1' => [
'description' => 'Myfield1 description.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'myfield2' => [
'description' => 'Myfield2 description',
'type' => 'text',
'not null' => TRUE,
],
],
'primary key' => ['myfield1'],
];
$schema = Database::getConnection()->schema();
$schema->createTable('mytable2', $spec);
}
?>
<?php
// examples. for full details see https://api.drupal.org/api/drupal/core%21modules%21views%21views.api.php/function/hook_views_data/9.0.x
// Add an ID field (numeric)
$data['test_tbl']['id'] = [
'title' => t('Table ID'),
'help' => t('The Table ID.'),
'field' => [
'id' => 'numeric',
],
'filter' => [
'id' => 'numeric',
],
'argument' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
];
// Add an Integer timestamp as a date field
$data['test_tbl']['transaction_date'] = [
'title' => t('Date and time'),
'help' => t('The date & time.'),
'field' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
];
// Text field
$data['test_tbl']['text_field'] = [
'title' => t('Text field'),
'help' => t('Text field'),
'field' => [
'id' => 'standard',
],
'argument' => [
'id' => 'string',
],
'filter' => [
'id' => 'string',
],
'sort' => [
'id' => 'standard',
],
];
$data['test_tbl']['boolean_field'] = [
'title' => t('Boolean Field'),
'help' => t('Field holds a Yes/No value.'),
'field' => [
'id' => 'boolean',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'boolean',
'label' => t('import?'),
'type' => 'yes-no',
],
];
// Add a node reference field (numeric)
$data['test_tbl']['node_ref'] = [
'title' => t('Rreferenced Node ID'),
'help' => t('The Node ID to reference.'),
'field' => [
'id' => 'numeric',
],
'filter' => [
'id' => 'numeric',
],
'argument' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
'relationship' => [
'base' => 'node_field_data',
'base field' => 'nid',
'id' => 'standard', // plugin ID to handle the relationship
'label' => t('Node'),
],
];
// Other relationship types
'relationship' => [
'base' => 'taxonomy_term',
'base field' => 'tid',
'label' => t('Term'),
],
'relationship' => [
'base' => 'users_field_data',
'base field' => 'uid',
'id' => 'standard',
'label' => t('User'),
],
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment