better_formats.module

  1. cis7 sites/all/modules/ulmus/better_formats/better_formats.module
  2. cle7 sites/all/modules/ulmus/better_formats/better_formats.module
  3. ecd7 sites/all/modules/ulmus/better_formats/better_formats.module
  4. elmsmedia7 sites/all/modules/ulmus/better_formats/better_formats.module
  5. harmony7 sites/all/modules/ulmus/better_formats/better_formats.module
  6. icor7 sites/all/modules/ulmus/better_formats/better_formats.module
  7. meedjum_blog7 sites/all/modules/ulmus/better_formats/better_formats.module
  8. mooc7 sites/all/modules/ulmus/better_formats/better_formats.module

Enhances Drupal's core text format settings.

Functions

Namesort descending Description
better_formats_element_info_alter Implements of hook_element_info_alter().
better_formats_field_settings_form Build the settings form for Field API fields.
better_formats_filter_process_format Process callback for form elements that have a text format selector attached.
better_formats_form_field_ui_edit_form_submit Submit handler for field instance edit form.
better_formats_form_field_ui_field_edit_form_alter Implements hook_form_FORM_ID_alter().
better_formats_menu Implements hook_menu().
better_formats_permission Implements of hook_perm().
better_formats_text_format_sort Sort text formats by weight.
better_formats_theme Implements hook_theme()
theme_better_formats_field_default_order Theme format default by role on field settings form.

File

sites/all/modules/ulmus/better_formats/better_formats.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Enhances Drupal's core text format settings.
  5. */
  6. /**
  7. * Implements of hook_perm().
  8. */
  9. function better_formats_permission() {
  10. $entities = entity_get_info();
  11. $perms = array(
  12. 'show format tips' => array(
  13. 'title' => t('Show format tips'),
  14. 'description' => t('Toggle display of format description help.'),
  15. ),
  16. 'show more format tips link' => array(
  17. 'title' => t('Show more format tips link'),
  18. 'description' => t('Toggle display of the "More information about text formats" link.'),
  19. ),
  20. );
  21. foreach ($entities as $type => $info) {
  22. if ($info['fieldable']) {
  23. $perms['show format selection for ' . $type] = array(
  24. 'title' => t('Show format selection for @entitys', array('@entity' => $type)),
  25. );
  26. }
  27. }
  28. return $perms;
  29. }
  30. /**
  31. * Implements hook_menu().
  32. */
  33. function better_formats_menu() {
  34. $items = array();
  35. $items['admin/config/content/formats/settings'] = array(
  36. 'title' => 'Settings',
  37. 'description' => 'Manage text formats',
  38. 'page callback' => 'drupal_get_form',
  39. 'page arguments' => array('better_formats_admin_settings_form'),
  40. 'access arguments' => array('administer filters'),
  41. 'type' => MENU_LOCAL_TASK,
  42. 'weight' => 3,
  43. 'file' => 'better_formats.admin_settings.inc',
  44. );
  45. /*
  46. $items['admin/config/content/formats/defaults'] = array(
  47. 'title' => 'Defaults',
  48. 'description' => 'Manage text formats',
  49. 'page callback' => 'drupal_get_form',
  50. 'page arguments' => array('better_formats_defaults_admin_form'),
  51. 'access arguments' => array('administer filters'),
  52. 'type' => MENU_LOCAL_TASK,
  53. 'weight' => 2,
  54. 'file' => 'better_formats.admin_defaults.inc',
  55. );
  56. */
  57. return $items;
  58. }
  59. /**
  60. * Implements of hook_element_info_alter().
  61. */
  62. function better_formats_element_info_alter(&$type) {
  63. // Our process callback must run immediately after filter_process_format().
  64. $filter_process_format_location = array_search('filter_process_format', $type['text_format']['#process']);
  65. $replacement = array('filter_process_format', 'better_formats_filter_process_format');
  66. array_splice($type['text_format']['#process'], $filter_process_format_location, 1, $replacement);
  67. }
  68. /**
  69. * Process callback for form elements that have a text format selector attached.
  70. *
  71. * This callback runs after filter_process_format() and performs additional
  72. * modifications to the form element.
  73. *
  74. * @see filter_process_format()
  75. */
  76. function better_formats_filter_process_format($element) {
  77. // Before we make any modifications to the element, record whether or not
  78. // filter_process_format() has determined that (for security reasons) the
  79. // user is not allowed to make any changes to this field. (This will happen
  80. // if the user does not have permission to use the currently-assigned text
  81. // format.)
  82. $access_denied_for_security = isset($element['format']['#access']) && !$element['format']['#access'];
  83. // Now hide several parts of the element for cosmetic reasons (depending on
  84. // the permissions of the current user).
  85. $show_selection = TRUE;
  86. if (isset($element['#entity_type'])) {
  87. $show_selection = user_access('show format selection for ' . $element['#entity_type']);
  88. }
  89. $show_tips = user_access('show format tips');
  90. $show_tips_link = user_access('show more format tips link');
  91. if (!$show_selection) {
  92. $element['format']['format']['#access'] = FALSE;
  93. }
  94. if (!$show_tips) {
  95. $element['format']['guidelines']['#access'] = FALSE;
  96. }
  97. if (!$show_tips_link) {
  98. $element['format']['help']['#access'] = FALSE;
  99. }
  100. // If the element represents a field attached to an entity, we may need to
  101. // adjust the allowed text format options. However, we don't want to touch
  102. // this if filter_process_format() has determined that (for security reasons)
  103. // the user is not allowed to make any changes; in that case, Drupal core
  104. // will hide the format selector and force the field to be saved with its
  105. // current values, and we should not do anything to alter that process.
  106. if (isset($element['#entity_type']) && !$access_denied_for_security) {
  107. $instance_info = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  108. $bf = isset($instance_info['settings']['better_formats']) ? $instance_info['settings']['better_formats'] : NULL;
  109. // Need to only do this on create forms.
  110. if (!empty($element['#entity']) && !empty($element['#entity_type'])) {
  111. list($eid, $vid, $bundle) = entity_extract_ids($element['#entity_type'], $element['#entity']);
  112. if (empty($eid) && isset($bf) && !empty($bf['default_order_toggle']) && !empty($bf['default_order_wrapper']['formats'])) {
  113. $order = $bf['default_order_wrapper']['formats'];
  114. uasort($order, 'better_formats_text_format_sort');
  115. $options = array();
  116. foreach ($order as $id => $weight) {
  117. if (isset($element['format']['format']['#options'][$id])) {
  118. $options[$id] = $element['format']['format']['#options'][$id];
  119. }
  120. }
  121. $element['format']['format']['#options'] = $options;
  122. $element['format']['format']['#default_value'] = array_shift(array_keys($options));
  123. }
  124. }
  125. if (isset($bf) && !empty($bf['allowed_formats_toggle']) && !empty($bf['allowed_formats'])) {
  126. // Filter the list of available formats to those allowed on this field.
  127. $allowed_fields = array_filter($bf['allowed_formats']);
  128. $options = &$element['format']['format']['#options'];
  129. $options = array_intersect_key($options, $allowed_fields);
  130. // If there is only one allowed format, deny access to the text format
  131. // selector for cosmetic reasons, just like filter_process_format() does.
  132. if (count($options) == 1) {
  133. $element['format']['format']['#access'] = FALSE;
  134. $show_selection = FALSE;
  135. }
  136. // If there are no allowed formats, we need to deny access to the entire
  137. // field, since it doesn't make sense to add or edit content that does
  138. // not have a text format.
  139. if (empty($options)) {
  140. $element['#access'] = FALSE;
  141. }
  142. // Otherwise, if the current default format is no longer one of the
  143. // allowed options, a new default format must be assigned.
  144. elseif (!isset($options[$element['format']['format']['#default_value']])) {
  145. // If there is no text in the field, it is safe to automatically assign
  146. // a new default format. We pick the first available option to be
  147. // consistent with what filter_default_format() does.
  148. if (!isset($element['value']['#default_value']) || $element['value']['#default_value']==='') {
  149. $formats = array_keys($options);
  150. $element['format']['format']['#default_value'] = reset($formats);
  151. }
  152. // Otherwise, it is unsafe to automatically assign a new default format
  153. // (since this will display the content in a way that was not
  154. // originally intended and might be dangerous, e.g. if the content
  155. // contains an attempted XSS attack). A human must explicitly decide
  156. // which new format to assign, so we force the field to be required but
  157. // with no default value, similar to what filter_process_format() does.
  158. // Although filter_process_format() limits this functionality to users
  159. // with the 'administer filters' permission, we can allow it for any
  160. // user here since we know that the user already has permission to use
  161. // the current format; thus, there is no danger of exposing unformatted
  162. // text (for example, raw PHP code) that they are otherwise not allowed
  163. // to see.
  164. else {
  165. $element['format']['format']['#required'] = TRUE;
  166. $element['format']['format']['#default_value'] = NULL;
  167. // Force access to the format selector (it may have been denied
  168. // previously for cosmetic reasons).
  169. $element['format']['#access'] = TRUE;
  170. $element['format']['format']['#access'] = TRUE;
  171. }
  172. }
  173. }
  174. }
  175. // If the user is not supposed to see the text format selector, hide all
  176. // guidelines except those associated with the default format. We need to do
  177. // this at the end, since the above code may have altered the default format.
  178. if (!$show_selection && isset($element['format']['format']['#default_value'])) {
  179. foreach (element_children($element['format']['guidelines']) as $format) {
  180. if ($format != $element['format']['format']['#default_value']) {
  181. $element['format']['guidelines'][$format]['#access'] = FALSE;
  182. }
  183. }
  184. }
  185. // Hide the entire text format fieldset if the user is not supposed to see
  186. // anything inside it.
  187. if (!$show_selection && !$show_tips && !$show_tips_link) {
  188. $element['format']['#type'] = 'container';
  189. }
  190. return $element;
  191. }
  192. /**
  193. * Sort text formats by weight.
  194. */
  195. function better_formats_text_format_sort($a, $b) {
  196. return $a['weight'] > $b['weight'];
  197. }
  198. /**
  199. * Implements hook_form_FORM_ID_alter().
  200. */
  201. function better_formats_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  202. $settings = $form['#instance']['settings'];
  203. // Only alter fields with text processing and if admin has chosen.
  204. $text_processing = isset($settings['text_processing']);
  205. if ($text_processing && variable_get('better_formats_per_field_core', 0)) {
  206. // Add a submit handler to save default values on empty fields.
  207. $form['#submit'][] = 'better_formats_form_field_ui_edit_form_submit';
  208. }
  209. // If the field is a format-using text field, allow the admin to configure
  210. // which formats are allowed here.
  211. if ($text_processing) {
  212. // We have to set an explicit weight here so that we can put the allowed
  213. // formats list after it.
  214. $form['instance']['settings']['text_processing']['#weight'] = -3;
  215. $bf_settings = isset($settings['better_formats']) ? $settings['better_formats'] : array();
  216. // Add in our formats table
  217. $form['instance']['settings'] += better_formats_field_settings_form($bf_settings);
  218. }
  219. }
  220. /**
  221. * Build the settings form for Field API fields.
  222. *
  223. * @param $bf_form
  224. * The existing better formats settings form from the form element.
  225. */
  226. function better_formats_field_settings_form($bf_form = array()) {
  227. $formats = filter_formats();
  228. $form = array();
  229. $form['better_formats'] = array(
  230. '#tree' => TRUE,
  231. '#type' => 'fieldset',
  232. '#title' => t('Text Formats'),
  233. '#weight' => -2,
  234. '#states' => array(
  235. 'visible' => array(
  236. ':input[name="instance[settings][text_processing]"]' => array('value' => '1'),
  237. ),
  238. ),
  239. );
  240. foreach ($formats as $format_id => $format) {
  241. $allowed_options[$format_id] = $format->name;
  242. }
  243. $allowed_toggle_default = isset($bf_form['allowed_formats_toggle']) ? $bf_form['allowed_formats_toggle'] : FALSE;
  244. $allowed_defaults = isset($bf_form['allowed_formats']) ? $bf_form['allowed_formats'] : array();
  245. if (empty($allowed_defaults)) {
  246. $allowed_defaults = array_keys($allowed_options);
  247. }
  248. $form['better_formats']['allowed_formats_toggle'] = array(
  249. '#type' => 'checkbox',
  250. '#title' => t('Limit allowed text formats'),
  251. '#description' => t('Check the allowed formats below. If checked available text formats can be chosen.'),
  252. '#weight' => 1,
  253. '#default_value' => $allowed_toggle_default,
  254. );
  255. $form['better_formats']['allowed_formats'] = array(
  256. '#type' => 'checkboxes',
  257. '#title' => t('Allowed formats'),
  258. '#options' => $allowed_options,
  259. '#description' => t('Select the text formats allowed for this field. Note that not all of these may appear on the form if a user does not have permission to use them. <strong>Warning:</strong> This affects existing content which may leave you unable to edit some fields. If that happens you must allow the format that field was saved in here.'),
  260. '#weight' => 2,
  261. '#default_value' => $allowed_defaults,
  262. '#states' => array(
  263. 'visible' => array(
  264. ':input[name="instance[settings][text_processing]"]' => array('value' => '1'),
  265. ':input[name="instance[settings][better_formats][allowed_formats_toggle]"]' => array('checked' => TRUE),
  266. ),
  267. ),
  268. );
  269. $order_toggle_default = isset($bf_form['default_order_toggle']) ? $bf_form['default_order_toggle'] : FALSE;
  270. $form['better_formats']['default_order_toggle'] = array(
  271. '#type' => 'checkbox',
  272. '#title' => t('Override default order'),
  273. '#description' => t('Override the gloabl order that will determine the default text format a user will get <strong>only on entity creation</strong>.'),
  274. '#weight' => 3,
  275. '#default_value' => $order_toggle_default,
  276. );
  277. $form['better_formats']['default_order_wrapper'] = array(
  278. //'#tree' => TRUE,
  279. '#type' => 'container',
  280. '#theme' => 'better_formats_field_default_order',
  281. '#weight' => 4,
  282. '#states' => array(
  283. 'visible' => array(
  284. ':input[name="instance[settings][text_processing]"]' => array('value' => '1'),
  285. ':input[name="instance[settings][better_formats][default_order_toggle]"]' => array('checked' => TRUE),
  286. ),
  287. ),
  288. );
  289. foreach ($formats as $key => $format) {
  290. $default = isset($bf_form['default_order_wrapper']['formats'][$key]) ? $bf_form['default_order_wrapper']['formats'][$key] : NULL;
  291. $rows[$key]['name'] = array('#markup' => $format->name);
  292. $rows[$key]['weight'] = array(
  293. '#type' => 'weight',
  294. '#default_value' => isset($default['weight']) ? $default['weight'] : $format->weight,
  295. '#delta' => 50,
  296. );
  297. $rows[$key]['#weight'] = isset($default['weight']) ? $default['weight'] : $format->weight;
  298. }
  299. $form['better_formats']['default_order_wrapper']['formats'] = $rows;
  300. return $form;
  301. }
  302. /**
  303. * Submit handler for field instance edit form.
  304. *
  305. * Copied and slightly modifed from field_ui_field_edit_form_submit().
  306. * @see field_ui_field_edit_form_submit()
  307. */
  308. function better_formats_form_field_ui_edit_form_submit($form, &$form_state) {
  309. $instance = $form_state['values']['instance'];
  310. $field = $form_state['values']['field'];
  311. // Only act on fields that have text processing enabled.
  312. if ($instance['settings']['text_processing'] == 1) {
  313. // Update any field settings that have changed.
  314. $field_source = field_info_field($instance['field_name']);
  315. $field = array_merge($field_source, $field);
  316. field_update_field($field);
  317. // Handle the default value.
  318. if (isset($form['instance']['default_value_widget'])) {
  319. $element = $form['instance']['default_value_widget'];
  320. // Extract field values.
  321. $items = array();
  322. field_default_extract_form_values(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state);
  323. // Commenting out the below line to not remove emtpy fields.
  324. //field_default_submit(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state);
  325. $instance['default_value'] = $items ? $items : NULL;
  326. }
  327. // Retrieve the stored instance settings to merge with the incoming values.
  328. $instance_source = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  329. $instance = array_merge($instance_source, $instance);
  330. field_update_instance($instance);
  331. }
  332. // Messaging and redirect removed as this is added in the default handler.
  333. }
  334. /**
  335. * Implements hook_theme()
  336. */
  337. function better_formats_theme() {
  338. return array(
  339. 'better_formats_field_default_order' => array(
  340. 'render element' => 'form',
  341. )
  342. );
  343. }
  344. /**
  345. * Theme format default by role on field settings form.
  346. */
  347. function theme_better_formats_field_default_order(&$variables) {
  348. $form = $variables['form'];
  349. $rows = array();
  350. $output = drupal_render($form['override']);
  351. if (is_array($form)) {
  352. $order_form =& $form['formats'];
  353. $order_form['#sorted'] = FALSE;
  354. foreach (element_children($order_form, TRUE) as $name) {
  355. $order_form[$name]['weight']['#attributes']['class'][] = 'format-order-weight';
  356. $rows[] = array(
  357. 'data' => array(
  358. drupal_render($order_form[$name]['name']),
  359. drupal_render($order_form[$name]['weight']),
  360. ),
  361. 'class' => array('draggable'),
  362. );
  363. }
  364. }
  365. $header = array(
  366. t('Format'),
  367. t('Weight'),
  368. );
  369. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'format-order')));
  370. drupal_add_tabledrag('format-order', 'order', 'sibling', 'format-order-weight', NULL, NULL, TRUE);
  371. return $output;
  372. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.