Can I add to an existing Entity schema from my own custom module

by Deejay   Last Updated March 26, 2015 07:24 AM

I'm extending a the Quiz module; by having taking extra data regarding the test . For this I have a custom module that redirects using a custom form .

It all works fine but as its only a single value I'd like to just add it to the existing 'Quiz result' entity. I know how to do this by hacking the 'contrib' module OPEN 'quiz.install' and adding it to the $schema['quiz_node_results'] array.

Once its added here I can then see it in Views as needed. But obviously this solution isn't good enough. Is there a way to add to an existing Entity schema from my own custom module; rather making a separate table in SQL for this info.

I've tried hook_schema_alter as well, but can't seem to get that to work either.

Here's what I had in my my_module.install file

 function my_module_schema_alter() {
  $schema = array();
  // Create the quiz node results table
  $schema['quiz_node_results']['fields']['time_taken'] = array(
    'type' => 'int',
    'size' => '10',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'Stores the user inputted estimated time.',
  );
}


function my_module_install() {
  $ret = array();
  $schema['quiz_node_results'] = array();
  rcni_saq_extended_schema_alter($schema);
   foreach ($schema['quiz_node_results']['fields'] as $name => $spec) {
    db_add_field($ret, 'quiz_node_results', $name, $spec);
  }
}

Many thanks for any help.

Tags : entities


Answers 2


Additional to the above answer the main thing to remember when testing the code you're writing here is that your module doesn't just have to disabled but 'uninstalled' EVERY time you run your code. I emphasise the 'everytime' as I was running breakpoints in my code>

In the following example I'd made a Hello World module that has a schema for a 'hello world' table and this code extends that in a .install file

/**
 * Implements hook_install().
 */
function hello_worlds_install() {
  $schema['helloworld'] = array();
  hello_worlds_schema_alter($schema);
    foreach($schema['helloworld']['fields'] as $field => $spec) {
     if (db_field_exists('helloworld', $field)) {
      watchdog('system', 'Module install: Attempt to recreate field: "%field",   when it already exists.', array('%field' => $field), WATCHDOG_WARNING);
    }
    else {
      db_add_field('helloworld', $field, $spec);
    }
  }
}

/**
 * Implements hook_uninstall().
 */
function hello_worlds_uninstall() {
  $schema['helloworld'] = array();
  hello_worlds_schema_alter($schema);
  foreach($schema['helloworld']['fields'] as $field => $specs) {
    db_drop_field('helloworld', $field);
  }
}

/**
 * Implements hook_schema_alter().
 *
 * Other modules, such as i18n_helloworld also modify the helloworld database table.
 */
function hello_worlds_schema_alter(&$schema) {
  if (isset($schema['helloworld'])) {
    $schema['helloworld']['fields']['notes'] = array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => 'none',
      'description' => 'String containing the notes',
     );
    $schema['helloworld']['fields']['background_image'] = array(
       'type' => 'varchar',
       'length' => 255,
       'not null' => TRUE,
       'default' => 'none',
       'description' => 'String containing the background image name for the helloworld',
    );

    return $schema;
  }
 }
Deejay
Deejay
March 25, 2015 11:17 AM

your my_module_schema_alter is missing a schema parameter.

fixed:

function my_module_schema_alter(&$schema) {
  $schema = array();
  // Create the quiz node results table
  $schema['quiz_node_results']['fields']['time_taken'] = array(
    'type' => 'int',
    'size' => '10',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'Stores the user inputted estimated time.',
  );
}
Andre Baumeier
Andre Baumeier
March 25, 2015 12:50 PM

Related Questions



How to create a custom template for entities?

Updated April 07, 2015 03:03 AM


Drupal 8 Custom Entities from a UI?

Updated July 14, 2015 15:03 PM

Content type subtypes with no bundles or clientside

Updated April 10, 2015 19:03 PM