3

Since hook_schema_alter() was removed from Drupal 8, how do I alter any existing schema defined from another module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) in my .install file, but no new field or column gets added into my existing table. Is there any other way to achieve this?

This is the code for the first module (a.install).

function a_schema() {
  $schema['my_table'] = array(
    'fields' => array(
       'type' => array(
          'description' => 'registration type',
          'type' => 'varchar',
          'length' => 255,
        ),
     ),
  );
}

This is the code for the second module (b.install).

/**
 * Implements  hook_update_N().
 */
    function b_update_8201() {
     $spec = array(
        'type' => 'varchar',
        'description' => "New Col",
        'length' => 20,
        'not null' => FALSE,
      ); 
     $schema = Database::getConnection()->schema();
     $schema->addField('my_table', 'newcol', $spec);
     return $schema;
    }
  • What type of schema are you attempting to alter? Can you specify the module? hook_schema_alter was removed because it was not a consistent way of doing things in Drupal 8, and there may be a way to do this depending on what you are attempting to alter. – mradcliffe Nov 21 '16 at 13:40
  • There is a custom module a, In a.install file, there is hook_schema. I want to add one more field by using custom module b. I have edited code. – Sugandh Khanna Nov 21 '16 at 13:52
5

hook_schema_alter() was not used for adding fields to the schema, it was used for reporting alterations to the schema by your module. This was so that functions such as drupal_get_schema() knew the alterations you had made. These functions have all been removed from Drupal 8 however, so there is no need to report your changes in hook_schema_alter().

You can add fields to another schema using:

\Drupal::service('database')->schema()->addField()

Documentation: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Schema.php/function/Schema%3A%3AaddField/8.2.x

  • api.drupal.org's been acting up lately. Could you fill-in the appropriate parameters for addField($table, $field, $spec, $keys_new)? – mradcliffe Nov 21 '16 at 14:14
  • It's acting up for me too - just reload the page a couple of times and it will eventually load. – Jaypan Nov 21 '16 at 14:28
  • What would be $spec and $keys_new parameter here? Any example where it is implemented? And can I write this in hook_install in mymodule.install file? – Sugandh Khanna Nov 22 '16 at 6:19
  • 1
    $spec is the Array representation of the schema element. Example ['type' => 'int', 'length' => 15]. $keys is an array that will contain the primary key. See the documentation on table definitions: api.drupal.org/api/drupal/… – Jaypan Nov 22 '16 at 6:32
1

For D7 it's like this, utilizing db_change_field:

/*
 * Implements hook_install()
 */
function mymodule_install() {

  db_change_field('table_name', 'field_name', 'field_name', array(
    'description'   => 'Some description',
    'type'          => 'text',
    'not null'      => TRUE,
    'size'          => 'big',
  ));

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.