Hook_schema Drupal 8
Posted : admin On 1/26/2022
A schema is defined by hookschema, which usually lives in a modulename.install file. By implementing hookschema and specifying the tables your module declares, you can easily create and drop these tables on all supported database engines. Implement hookschema Sto rage. Drupal 8 Entity API - Drupalcon Austin. By Wolfgang Ziegler. Made with Slides.com. Drupal 8 Entity API - Drupalcon Austin. Only if you uninstall a module, and subsequently re-install it, the database tables it uses and declares with hookschema are re-created. To uninstall a module, you need to: Uncheck the checkbox shown for the module. Click on Save configuration. Click on the:Uninstall tab. Click on the check box to the left of the module name; Click on the. I have installed a schema for a custom module which creates the table when I enable the module. But when I disable the module, the table still remains. I am using the following code for uninstall. This article is one of Metal Toad's Top 20 Drupal Tips.Enjoy! For a recent project, we were tasked to consume the client's internal data from a custom API. Now, this scenario was lucky for us, the API provides a total item count of about 5000, but when queried with a start date, it provides all revisions of items between then and now.
Creating custom table in drupal 8 is not recommended. Create entity types instead. |
But if you want to Create custom table you can create either by implementing hook_schema or |
else create schema file config/schema/your_custom_module_name.schema.yml in your module |
(https://www.drupal.org/docs/8/api/configuration-api/configuration-schemametadata). |
( https://drupal.stackexchange.com/questions/219580/best-practice-for-creating-table-in-custom-module ) |
1. Implementing hook_schema in .module file (Not recommended). |
/** |
* Implements hook_schema(). |
* |
* @todo to be removed this as this is already declared in inactive.install file(confirm) |
* |
*/ |
function inactive_user_schema() { |
$schema['inactive_users'] = array( |
'description' => 'The base table for inactive_users.', |
'fields' => array( |
'uid' => array( |
'description' => 'The primary identifier for a user.', |
'type' => 'int', |
'unsigned' => TRUE, |
'not null' => TRUE, |
'default' => 0, |
), |
'notified_admin' => array( |
'description' => 'Admin notifier.', |
'type' => 'int', |
'size' => 'tiny', |
'unsigned' => TRUE, |
'not null' => TRUE, |
'default' => 0, |
), |
'notified_user' => array( |
'description' => 'User notifier.', |
'type' => 'int', |
'size' => 'tiny', |
'unsigned' => TRUE, |
'not null' => TRUE, |
'default' => 0, |
), |
'warned_user_block_timestamp' => array( |
'description' => 'Timestamp warning.', |
'type' => 'int', |
'unsigned' => TRUE, |
'not null' => TRUE, |
'default' => 0, |
), |
'notified_user_block' => array( |
'description' => 'User block warning.', |
'type' => 'int', |
'size' => 'tiny', |
'unsigned' => TRUE, |
'not null' => TRUE, |
'default' => 0, |
), |
'notified_admin_block' => array( |
'description' => 'Timestamp warning.', |
'type' => 'int', |
'size' => 'tiny', |
'unsigned' => TRUE, |
'not null' => TRUE, |
'default' => 0, |
), |
'warned_user_delete_timestamp' => array( |
'description' => 'Timestamp warning.', |
'type' => 'int', |
'unsigned' => TRUE, |
'not null' => TRUE, |
'default' => 0, |
), |
'protected' => array( |
'description' => 'Timestamp warning.', |
'type' => 'int', |
'size' => 'tiny', |
'unsigned' => TRUE, |
'not null' => TRUE, |
'default' => 0, |
), |
'inactive_user_notification_flag' => array( |
'description' => 'Inactive user notification flag.', |
'type' => 'int', |
'not null' => TRUE, |
'default' => 1, |
), |
), |
'primary key' => array('uid'), |
); |
$schema['inactive_user_flag'] = array( |
'description' => 'Inactive user flag.', |
'fields' => array( |
'id' => array( |
'description' => 'The primary key to store unique information.', |
'type' => 'serial', |
'not null' => FALSE, |
), |
'user_id' => array( |
'description' => 'Storing inactive user id.', |
'type' => 'int', |
), |
'value' => array( |
'description' => 'Storing inacitve user value.', |
'type' => 'int', |
), |
), |
'primary key' => array('id'), |
); |
return $schema; |
} |
2. other way you have to schema.yml inside config/schema/ follow this link https://www.drupal.org/docs/8/api/configuration-api/configuration-schemametadata |
Drupal 8 Version

Hook_schema Drupal 8 Free

