tac_lite_create.module 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @file
  4. * Use tac_lite to control which terms can be used when creating new
  5. * content. Unlike tac_lite.module, which relies on Drupal's node_access
  6. * features, this module uses hook_form_alter to change which terms are
  7. * presented by the taxonomy module.
  8. *
  9. * Original implementation by markDrupal http://drupal.org/user/93970.
  10. */
  11. /**
  12. * Implementation of hook_form_alter().
  13. */
  14. function tac_lite_create_form_alter(&$form, $form_state, $form_id) {
  15. if (strpos($form_id, 'node_form') !== FALSE) {
  16. global $user;
  17. if (user_access('administer tac_lite')) {
  18. // no need to restrict based on update permission in this cases
  19. return;
  20. }
  21. // the vocabularies containing protected info.
  22. $vids = variable_get('tac_lite_categories', array(0));
  23. // the terms this user is allowed to edit
  24. $tids = array();
  25. for ($i = 1; $i <= variable_get('tac_lite_schemes', 1); $i++) {
  26. $config = _tac_lite_config($i);
  27. if ((isset($config['tac_lite_create']) && $config['tac_lite_create']) ||
  28. in_array('grant_update', $config['perms'])) {
  29. $tids = array_merge($tids, _tac_lite_user_tids($user, $i));
  30. }
  31. }
  32. // Go through all of the fields in the system to find the ones we want use
  33. $term_fields = array();
  34. $info = field_info_fields();
  35. foreach($info as $key => $value){
  36. // First check for taxonomy_term_reference fields.
  37. if ($value['type'] == 'taxonomy_term_reference') {
  38. // Then check to see if they are associated with this node type (entity bundle).
  39. if (isset($value['bundles']['node'])) {
  40. if (in_array($form['#bundle'], $value['bundles']['node'])) {
  41. // Add an entry to our term_fields in the form of field name => vocabulary machine name
  42. $term_fields[$key] = $value['settings']['allowed_values'][0]['vocabulary'];
  43. }
  44. }
  45. }
  46. }
  47. // Now that we have the names of the fields, go through each one in the form.
  48. foreach($term_fields as $field_name => $vocab_name) {
  49. // Get the language key so we can find the correct element in the field.
  50. $field_language = $form[$field_name]['#language'];
  51. // Avoid PHP errors
  52. if (empty($form[$field_name]) || empty($form[$field_name][$field_language])) {
  53. continue;
  54. }
  55. // Skip auto complete fields as they are not supported.
  56. if ($form[$field_name][$field_language]['#type'] != 'select' && $form[$field_name][$field_language]['#type'] != 'radios'){
  57. continue;
  58. }
  59. // Get the vocabulary info so we can get the vid.
  60. $v = taxonomy_vocabulary_machine_name_load($vocab_name);
  61. // We only want to act on this field if it is tied to a vocabulary we are set to control.
  62. if(!in_array($v->vid, $vids)) {
  63. continue;
  64. }
  65. // Go through each option for this field.
  66. foreach ($form[$field_name][$field_language]['#options'] as $term_id => $term_name) {
  67. // Skip the "<none>" option (or anything like it).
  68. if (!is_numeric($term_id)) {
  69. continue;
  70. }
  71. // Is this option not in the list of terms this user can create with?
  72. if (!in_array($term_id, $tids)) {
  73. // The term is not the default value, and user is not allowed to create with it.
  74. // HELP: What if it IS the default value? Do we not unset it?
  75. if ($term_id != $form[$field_name][$field_language]['#default_value']) {
  76. // Unset the option to it doesn't appear.
  77. // TODO: It would be a nice feature to have tids that are already selected.
  78. // go into a value element so we could merge then when saving the node.
  79. unset($form[$field_name][$field_language]['#options'][$term_id]);
  80. }
  81. }
  82. }
  83. // If there are no options left, but this field is required then throw a 404.
  84. if (count($form[$field_name][$field_language]['#options']) == 0 && !empty($form[$field_name]['#required'])) {
  85. drupal_set_message(t('You have no permissions to add content to the required %name vocabulary. Please contact the site administrator if you believe you should have permission to add content.', array('%name' => $form[$field_name]['#title'])));
  86. drupal_access_denied();
  87. exit();
  88. }
  89. // Don't show if the only option left is <none>.
  90. if (empty($form[$field_name][$field_language]['#options']) ||
  91. (count($form[$field_name][$field_language]['#options']) == 1 && isset($form[$field_name][$field_language]['#options']['_none']))) {
  92. $form[$field_name]['#access'] = FALSE;
  93. }
  94. // HELP: Is size deprecated?
  95. /*
  96. if (isset($form[$field_name]['#size']) && $form[$field_name]['#size'] > count($form['taxonomy'][$field_language]['#options'])) {
  97. $form[$field_name]['#size'] = count($form[$field_name][$field_language]['#options']);
  98. }
  99. */
  100. }
  101. }
  102. elseif ($form_id == 'tac_lite_admin_scheme_form') {
  103. $config = $form['#tac_lite_config'];
  104. $scheme = $form_state['build_info']['args'][0];
  105. if (!empty($form['tac_lite_config_scheme_' . $scheme])) {
  106. $form['tac_lite_config_scheme_' . $scheme]['tac_lite_create'] = array(
  107. '#type' => 'checkbox',
  108. '#title' => 'Visibility on create and edit forms',
  109. '#default_value' => isset($config['tac_lite_create']) ? $config['tac_lite_create'] : FALSE,
  110. '#description' => t('Show terms when creating content. This <strong>does not</strong> control which users can create a given content type. This <strong>does</strong> control which terms appear on the node edit forms. <br/>Note that schemes granting <em>update</em> permission (above) imply <em>visibility on forms</em> as well.'),
  111. );
  112. }
  113. }
  114. }