0

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.

  • what exactly did you try with hook_schema_alter? that usually does work, it has to go into the .install file of your module though, not the .module one. – Andre Baumeier Mar 25 '15 at 12:21
1

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.',
  );
}
  • Alter hooks don't return anything. You are getting confused between hook_schema_alter() and hook_schema(). Note to future readers: this answer is wrong. – kiamlaluno Mar 26 '15 at 9:10
  • fixed it, yes I confused them. – Andre Baumeier Mar 26 '15 at 10:16
0

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;
  }
 }

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.