ajax_example_node_form_alter.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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().
  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']) ? $node->ajax_example['example_2'] : $form_state['values']['ajax_example_2'],
  42. );
  43. }
  44. }
  45. /**
  46. * Returns changed part of the form.
  47. *
  48. * @return array
  49. * Form API array.
  50. *
  51. * @see ajax_example_form_node_form_alter()
  52. */
  53. function ajax_example_form_node_callback($form, $form_state) {
  54. return $form['container'];
  55. }
  56. /**
  57. * Implements hook_node_submit().
  58. * @see ajax_example_form_node_form_alter()
  59. */
  60. function ajax_example_node_submit($node, $form, &$form_state) {
  61. $values = $form_state['values'];
  62. // Move the new data into the node object.
  63. $node->ajax_example['example_1'] = $values['ajax_example_1'];
  64. // Depending on the state of ajax_example_1; it may not exist.
  65. $node->ajax_example['example_2'] = isset($values['ajax_example_2']) ? $values['ajax_example_2'] : '';
  66. }
  67. /**
  68. * Implements hook_node_prepare().
  69. *
  70. * @see ajax_example_form_node_form_alter()
  71. */
  72. function ajax_example_node_prepare($node) {
  73. if (empty($node->ajax_example)) {
  74. // Set default values, since this only runs when adding a new node.
  75. $node->ajax_example['example_1'] = 0;
  76. $node->ajax_example['example_2'] = '';
  77. }
  78. }
  79. /**
  80. * Implements hook_node_load().
  81. *
  82. * @see ajax_example_form_node_form_alter()
  83. */
  84. function ajax_example_node_load($nodes, $types) {
  85. $result = db_query('SELECT * FROM {ajax_example_node_form_alter} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)))->fetchAllAssoc('nid');
  86. foreach ($nodes as &$node) {
  87. $node->ajax_example['example_1']
  88. = isset($result[$node->nid]->example_1) ?
  89. $result[$node->nid]->example_1 : 0;
  90. $node->ajax_example['example_2']
  91. = isset($result[$node->nid]->example_2) ?
  92. $result[$node->nid]->example_2 : '';
  93. }
  94. }
  95. /**
  96. * Implements hook_node_insert().
  97. *
  98. * @see ajax_example_form_node_form_alter()
  99. */
  100. function ajax_example_node_insert($node) {
  101. if (isset($node->ajax_example)) {
  102. db_insert('ajax_example_node_form_alter')
  103. ->fields(array(
  104. 'nid' => $node->nid,
  105. 'example_1' => $node->ajax_example['example_1'],
  106. 'example_2' => $node->ajax_example['example_2'],
  107. ))
  108. ->execute();
  109. }
  110. }
  111. /**
  112. * Implements hook_node_update().
  113. * @see ajax_example_form_node_form_alter()
  114. */
  115. function ajax_example_node_update($node) {
  116. if (db_select('ajax_example_node_form_alter', 'a')->fields('a')->condition('nid', $node->nid, '=')->execute()->fetchAssoc()) {
  117. db_update('ajax_example_node_form_alter')
  118. ->fields(array(
  119. 'example_1' => $node->ajax_example['example_1'],
  120. 'example_2' => $node->ajax_example['example_2'],
  121. ))
  122. ->condition('nid', $node->nid)
  123. ->execute();
  124. }
  125. else {
  126. // Cleaner than doing it again.
  127. ajax_example_node_insert($node);
  128. }
  129. }
  130. /**
  131. * Implements hook_node_delete().
  132. * @see ajax_example_form_node_form_alter()
  133. */
  134. function ajax_example_node_delete($node) {
  135. db_delete('ajax_example_node_form_alter')
  136. ->condition('nid', $node->nid)
  137. ->execute();
  138. }