Example systems settings form

Public

Get raw version
php
  1. /**
  2.  * Implementation of hook_menu().
  3.  */
  4. function annotate_menu() {
  5. $items['admin/config/annotate'] = array(
  6. ...
  7. );
  8.  
  9. $items['admin/config/annotate/settings'] = array(
  10. 'title' => 'Annotation settings',
  11. 'description' => 'Change how annotations behave.',
  12. 'page callback' => 'drupal_get_form',
  13. 'page arguments' => array('annotate_admin_settings'),
  14. 'access arguments' => array('administer site configuration'),
  15. 'type' => MENU_NORMAL_ITEM,
  16. 'file' => 'annotate.admin.inc',
  17. );
  18.  
  19. return $items;
  20. }
  21.  
  22.  
  23.  
  24. #annotate.admin.inc
  25.  
  26. // $Id$
  27.  
  28. /**
  29.  * @file
  30.  * Administration page callbacks for the annotate module.
  31.  */
  32.  
  33. /**
  34.  * Form builder. Configure annotations.
  35.  *
  36.  * @ingroup forms
  37.  * @see system_settings_form().
  38.  */
  39. function annotate_admin_settings() {
  40.  
  41. // Get an array of node types with internal names as keys and
  42. // "friendly names" as values. E.g.,
  43. // array('page' => 'Page', 'story' => 'Story')
  44.  
  45. $types = node_type_get_types();
  46. foreach($types as $node_type) {
  47. $options[$node_type->type] = $node_type->name;
  48. }
  49.  
  50. $form['annotate_node_types'] = array(
  51. '#type' => 'checkboxes',
  52. '#title' => t('Users may annotate these content types'),
  53. '#options' => $options,
  54. '#default_value' => variable_get('annotate_node_types', array('page')),
  55. '#description' => t('A text field will be available on these content types to
  56. make user-specific notes.'),
  57. );
  58.  
  59. $form['annotate_deletion'] = array(
  60. '#type' => 'radios',
  61. '#title' => t('Annotations will be deleted'),
  62. '#description' => t('Select a method for deleting annotations.'),
  63. '#options' => array(
  64. t('Never'),
  65. t('Randomly'),
  66. t('After 30 days')
  67. ),
  68. '#default_value' => variable_get('annotate_deletion', 0) // Default to Never
  69. );
  70.  
  71. $form['annotate_limit_per_node'] = array(
  72. '#type' => 'textfield',
  73. '#title' => t('Annotations per node'),
  74. '#description' => t('Enter the maximum number of annotations allowed per
  75. node (0 for no limit).'),
  76. '#default_value' => variable_get('annotate_limit_per_node', 1),
  77. '#size' => 3
  78. );
  79.  
  80.  
  81.  
  82.  
  83. return system_settings_form($form, TRUE);
  84.  
  85. }
  86.  
  87. /**
  88.  * annotate_admin_settings_validate
  89.  *
  90.  * Update content types to reflect changes to the annotation configuration options selected by the site administrator.
  91.  * If the administrator checked a box for a content type that did not previously have an annotation field then add it to the
  92.  * content type. If a content type is unchecked by the administrator and that content type had an annotation field, then
  93.  * remove that field from the content type.
  94.  */
  95. function annotate_admin_settings_validate($form, $form_state) {
  96.  
  97. $limit = $form_state['values']['annotate_limit_per_node'];
  98. if (!is_numeric($limit)) {
  99. form_set_error('annotate_limit_per_node', t('Please enter a number.'));
  100. return;
  101. }
  102.  
  103. // loop through each of the content type checkboxes shown on the form
  104. foreach ($form_state['values']['annotate_node_types'] as $key => $value) {
  105.  
  106. // if the check box for a content type is unchecked, look to see whether this content type has the
  107. // annotation field attached to it using the field_info_instance function. If it does then we
  108. // need to remove the annotation field as the administrator has unchecked the box.
  109. if (!$value) {
  110. $instance = field_info_instance('node', 'annotation', $key);
  111. if (!empty($instance)) {
  112. field_delete_instance($instance);
  113. watchdog("Annotation", "Deleted annotation field from content type: ".$key);
  114. } // end of annotation existed, need to remove it
  115. } else {
  116. // if the check box is checked for a content type look to see whether the field is associated with that
  117. // content type. If not then add the annotation field to the content type
  118. $instance = field_info_instance('node', 'annotation', $key);
  119. if (empty($instance)) {
  120. $instance = array(
  121. 'field_name' => 'annotation',
  122. 'entity_type' => 'node',
  123. 'bundle' => $key,
  124. 'label' => t('Annotation'),
  125. 'widget_type' => 'text_textarea_with_summary',
  126. 'settings' => array('display_summary' => TRUE),
  127. 'display' => array(
  128. 'default' => array(
  129. 'type' => 'text_default',
  130. ),
  131. 'teaser' => array(
  132. 'type' => 'text_summary_or_trimmed',
  133. ),
  134. ),
  135. );
  136.  
  137. $instance = field_create_instance($instance);
  138.  
  139. watchdog("Annotation", "Added annotation field to content type: ".$key);
  140. }
  141. }
  142.  
  143. }
  144.  
  145.  
  146. }