ajax_example_node_form_alter.inc

  1. examples
    1. 7 ajax_example/ajax_example_node_form_alter.inc
    2. 8 ajax_example/ajax_example_node_form_alter.inc

This example shows how to use AJAX when altering a node form.

It maintains a table parallel to the node table containing attributes 'example_1' and 'example_2' which are displayed on the node form. 'example_2' is displayed dynamically when example_1 is checked.

Functions & methods

NameDescription
ajax_example_form_node_callbackReturns changed part of the form.
ajax_example_form_node_form_alterImplements hook_form_FORM_ID_alter() for the node form.
ajax_example_node_deleteImplements hook_node_delete().
ajax_example_node_insertImplements hook_node_insert().
ajax_example_node_loadImplements hook_node_load().
ajax_example_node_prepareImplements hook_node_prepare().
ajax_example_node_submitImplements hook_node_submit().
ajax_example_node_updateImplements hook_node_update().

File

ajax_example/ajax_example_node_form_alter.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * This example shows how to use AJAX when altering a node form.
  5. *
  6. * It maintains a table parallel to the node table containing attributes
  7. * 'example_1' and 'example_2' which are displayed on the node form.
  8. * 'example_2' is displayed dynamically when example_1 is checked.
  9. */
  10. /**
  11. * Implements hook_form_FORM_ID_alter() for the node form.
  12. *
  13. * Adds two fields to the node form, second only appears after first is enabled.
  14. */
  15. function ajax_example_form_node_form_alter(&$form, &$form_state, $form_id) {
  16. $node = $form['#node'];
  17. $form['ajax_example_1'] = array(
  18. '#type' => 'checkbox',
  19. '#title' => t('AJAX Example 1'),
  20. '#description' => t('Enable to show second field.'),
  21. '#default_value' => $node->ajax_example['example_1'],
  22. '#ajax' => array(
  23. 'callback' => 'ajax_example_form_node_callback',
  24. 'wrapper' => 'ajax-example-form-node',
  25. 'effect' => 'fade',
  26. )
  27. );
  28. $form['container'] = array(
  29. '#prefix' => '<div id="ajax-example-form-node">',
  30. '#suffix' => '</div>',
  31. );
  32. // If the state values exist and 'ajax_example_1' state value is 1 or
  33. // if the state values don't exist and 'example1' variable is 1 then
  34. // display the ajax_example_2 field.
  35. if (!empty($form_state['values']['ajax_example_1']) && $form_state['values']['ajax_example_1'] == 1
  36. || empty($form_state['values']) && $node->ajax_example['example_1']) {
  37. $form['container']['ajax_example_2'] = array(
  38. '#type' => 'textfield',
  39. '#title' => t('AJAX Example 2'),
  40. '#description' => t('AJAX Example 2'),
  41. '#default_value' => empty($form_state['values']['ajax_example_2']) ?
  42. $node->ajax_example['example_2'] :
  43. $form_state['values']['ajax_example_2'],
  44. );
  45. }
  46. }
  47. /**
  48. * Returns changed part of the form.
  49. *
  50. * @return renderable array
  51. *
  52. * @see ajax_example_form_node_form_alter()
  53. */
  54. function ajax_example_form_node_callback($form, $form_state) {
  55. return $form['container'];
  56. }
  57. /**
  58. * Implements hook_node_submit().
  59. * @see ajax_example_form_node_form_alter()
  60. */
  61. function ajax_example_node_submit($node, $form, &$form_state) {
  62. $values = $form_state['values'];
  63. // Move the new data into the node object.
  64. $node->ajax_example['example_1'] = $values['ajax_example_1'];
  65. // Depending on the state of ajax_example_1; it may not exist.
  66. $node->ajax_example['example_2'] = isset($values['ajax_example_2']) ? $values['ajax_example_2'] : '';
  67. }
  68. /**
  69. * Implements hook_node_prepare().
  70. *
  71. * @see ajax_example_form_node_form_alter()
  72. */
  73. function ajax_example_node_prepare($node) {
  74. if (empty($node->ajax_example)) {
  75. // Set default values, since this only runs when adding a new node.
  76. $node->ajax_example['example_1'] = 0;
  77. $node->ajax_example['example_2'] = '';
  78. }
  79. }
  80. /**
  81. * Implements hook_node_load().
  82. *
  83. * @see ajax_example_form_node_form_alter()
  84. */
  85. function ajax_example_node_load($nodes, $types) {
  86. $result = db_query('SELECT * FROM {ajax_example_node_form_alter} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)))->fetchAllAssoc('nid');
  87. foreach ($nodes as &$node) {
  88. $node->ajax_example['example_1'] = isset($result[$node->nid]->example_1) ?
  89. $result[$node->nid]->example_1 : 0;
  90. $node->ajax_example['example_2'] = isset($result[$node->nid]->example_2) ?
  91. $result[$node->nid]->example_2 : '';
  92. }
  93. }
  94. /**
  95. * Implements hook_node_insert().
  96. *
  97. * @see ajax_example_form_node_form_alter()
  98. */
  99. function ajax_example_node_insert($node) {
  100. db_insert('ajax_example_node_form_alter')
  101. ->fields(array(
  102. 'nid' => $node->nid,
  103. 'example_1' => $node->ajax_example['example_1'],
  104. 'example_2' => $node->ajax_example['example_2'],
  105. ))
  106. ->execute();
  107. }
  108. /**
  109. * Implements hook_node_update().
  110. * @see ajax_example_form_node_form_alter()
  111. */
  112. function ajax_example_node_update($node) {
  113. if (db_select('ajax_example_node_form_alter', 'a')->fields('a')->condition('nid', $node->nid, '=')->execute()->fetchAssoc()) {
  114. db_update('ajax_example_node_form_alter')
  115. ->fields(array(
  116. 'example_1' => $node->ajax_example['example_1'],
  117. 'example_2' => $node->ajax_example['example_2'],
  118. ))
  119. ->condition('nid', $node->nid)
  120. ->execute();
  121. }
  122. else {
  123. // Cleaner than doing it again.
  124. ajax_example_node_insert($node);
  125. }
  126. }
  127. /**
  128. * Implements hook_node_delete().
  129. * @see ajax_example_form_node_form_alter()
  130. */
  131. function ajax_example_node_delete($node) {
  132. db_delete('ajax_example_node_form_alter')
  133. ->condition('nid', $node->nid)
  134. ->execute();
  135. }
Login or register to post comments