3

Why isn't the code saving values in the database?

module_load_include('inc', 'firstform', 'firstform_node_form_alter');

function firstform_menu() {
$items = array();

$items['submit_driven_ajax'] = array(
    'title' => 'send your email',
    'page callback' => 'drupal_get_form',
    'access callback' => TRUE,
    'weight' => 3,
);
return $items;

}

function firstform_intro() {
$markup = t('The AJAX example module provides many examples of AJAX including forms, links, and AJAX commands.');
return $markup;}

function firstform_form($form, &$form_state) {
$form['box'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="box">',
    '#suffix' => '</div>',
);
$form['email'] = array(
    '#type' => 'textfield',
    '#required' => TRUE
    , '#description' => "Please enter your email name.",
    '#ajax' => array(
        'callback' => 'firstform_text_driven_callback',
        'wrapper' => 'box',
        'name' => 'email1',
    ),
);
$form['submit'] = array(
    '#type' => 'submit',
    '#ajax' => array(
        'callback' => 'firstform_submit_driven_callback',
        'wrapper' => 'box',
        'name' => 'submit1',
    ),
    '#value' => t('Submit'),
);

return $form;}

function firstform_submit_driven_callback($form, $form_state) {
$element = $form['box'];
$element['#markup'] = "your email ({$form_state['values']['email']}): " . date('c');
return $element;}

function firstform_text_driven_callback($form, $form_state) {
$element = $form['box'];
$element['#markup'] = "your email ({$form_state['values']['email']}): " .date('c');
return $element;}

 function firstform_block_info() {
$blocks = array();
$blocks['firstform'] = array(
    'info' => t('Form mail'),
    'weight' => 0,
);
return $blocks;}

function firstform_block_view($delta = '') {
module_load_include('inc', 'node', 'node.pages');
switch ($delta) {
    case 'firstform':
        $block['subject'] = t('');
        $form = drupal_render(node_add('firstform'));
        $block['content'] = $form;

        break;
}
return $block;}



  function firstform_node_insert($node){
  db_insert('firstform_node_form_alter')
->fields(array(
  'nid' => $node->nid,
  'email' => $node->firstform['email'],
  'date' => $node->firstform['date'],
))
->execute();}

function firstform_node_update($node) {


 if (db_select('firstform_node_form_alter', 'a')->fields('a')->condition('nid', $node->nid, '=')->execute()->fetchAssoc()) {
db_update('firstform_node_form_alter')
  ->fields(array(
    'email' => $node->firstform['email'],
    'date' => $node->firstform['date'],
  ))
  ->condition('nid', $node->nid)
  ->execute();
  }  else {
firstform_node_insert($node);}}

    function firstform_node_delete($node) {  db_delete('firstform_node_form_alter')
->condition('nid', $node->nid)
->execute();}

When I add firstform.install, the firstform_node_form_alter table is not created.

function firstform_schema() {
 $schema['firstform_node_form_alter'] = array(
'description' => 'Stores email settings for nodes.',
'fields' => array(
  'nid' => array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
    'description' => 'The {node}.nid to store settings.',
  ),'email' => array(
    'type' => 'varchar',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'Node Form Example 1 textfield',
  ),
  'date' => array(
    'type' => 'int',
    'length' => 256,
    'not null' => FALSE,
    'default' => '',
    'description' => 'Node Form date textfield',
  ),
),
'primary key' => array('nid'),
'foreign keys' => array(
  'dnv_node' => array(
    'table' => 'node',
    'columns' => array('nid' => 'nid'),
  ),
),
 );
return $schema;
}
3
  • 3
    You need uninstall and then install module again to make Drupal run firstfom_schema() – Eugene Fidelin Jul 29 '12 at 12:40
  • You can use devel for quick module reinstalling. Or as other option write hook_update and add new table/fields – milkovsky Sep 24 '13 at 21:03
  • You can use drush dre firstform -y to have uninstalling and re-enabling your module all in one command. – leymannx Sep 6 '15 at 8:22
1

hook_schema is called at install and unistall, so make sure your actually installing the module and not just turning it on.

If you've already had your module running on your site, switched it off at /admin/modules and then switched it back on, drupal doesn't recognize this as an uninstall/install event. Rather, you need to fully remove it from the DB by going to admin/modules/uninstall after switching the module off.

1
  • Enabling the module for the first time is treated as an installation by Drupal, so it fires the install and schema hooks. If you've added code to those hooks after the module has already been installed, then the module must be disabled, uninstalled, and then enabled again. That entire process is essentially a "reinstall" of the module. – Lester Peabody Dec 26 '13 at 15:52

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.