paragraphs.admin.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * @file
  4. * Admin functions for the paragraphs module.
  5. */
  6. /**
  7. * Page callback to show the bundle overview page.
  8. *
  9. * @return null|string
  10. * Rendered table of bundles.
  11. *
  12. * @throws Exception
  13. */
  14. function paragraphs_admin_bundle_overview() {
  15. $page = array();
  16. $bundles = paragraphs_bundle_load();
  17. $field_ui = module_exists('field_ui');
  18. $header = array(
  19. t('Bundle name'),
  20. array('data' => t('Operations'), 'colspan' => $field_ui ? '4' : '2')
  21. );
  22. $rows = array();
  23. foreach ($bundles as $bundle) {
  24. $type_url_str = strtr($bundle->bundle, array('_' => '-'));
  25. $row = array(
  26. array(
  27. 'data' => $bundle->name . ' (' . $bundle->bundle . ')',
  28. )
  29. );
  30. if ($field_ui) {
  31. // Manage fields.
  32. $row[] = array(
  33. 'data' => l(t('manage fields'), 'admin/structure/paragraphs/' . $type_url_str . '/fields')
  34. );
  35. // Display fields.
  36. $row[] = array(
  37. 'data' => l(t('manage display'), 'admin/structure/paragraphs/' . $type_url_str . '/display')
  38. );
  39. }
  40. // Manage bundle.
  41. $row[] = array(
  42. 'data' => l(t('edit bundle'), 'admin/structure/paragraphs/' . $type_url_str . '/edit')
  43. );
  44. // Delete bundle.
  45. $row[] = array(
  46. 'data' => l(t('delete bundle'), 'admin/structure/paragraphs/' . $type_url_str . '/delete')
  47. );
  48. $rows[$bundle->bundle] = $row;
  49. }
  50. // Sort rows by bundle.
  51. ksort($rows);
  52. // Render paragraphs bundle table.
  53. $page['paragraphs_bundle_table'] = array(
  54. '#theme' => 'table',
  55. '#header' => $header,
  56. '#rows' => $rows,
  57. '#empty' => t('No paragraph bundles have been defined yet.'),
  58. );
  59. return $page;
  60. }
  61. /**
  62. * Form to create or edit an paragraph bundle.
  63. */
  64. function paragraphs_admin_bundle_form($form, &$form_state, $bundle = NULL) {
  65. if (!isset($bundle) && !$bundle) {
  66. // This is a new bundle
  67. $bundle = new stdClass();
  68. $bundle->name = '';
  69. $bundle->bundle = '';
  70. $bundle->locked = 0;
  71. }
  72. else {
  73. if (!$bundle) {
  74. drupal_set_message(t('Could not load bundle'), 'error');
  75. drupal_goto('admin/structure/paragraphs');
  76. }
  77. }
  78. $form['#paragraphs_bundle'] = $bundle;
  79. $form['name'] = array(
  80. '#title' => t('Name'),
  81. '#type' => 'textfield',
  82. '#default_value' => $bundle->name,
  83. '#description' => t('The human-readable name of this bundle. It is recommended that this name begin with a capital letter and contain only letters, numbers, and spaces. This name must be unique.'),
  84. '#required' => TRUE,
  85. '#size' => 30,
  86. );
  87. if (!$bundle->locked) {
  88. $form['bundle'] = array(
  89. '#type' => 'machine_name',
  90. '#default_value' => $bundle->bundle,
  91. '#maxlength' => 32,
  92. '#disabled' => $bundle->locked,
  93. '#machine_name' => array(
  94. 'exists' => 'paragraphs_bundle_load',
  95. ),
  96. '#description' => t('A unique machine-readable name for this paragraph bundle. It must only contain lowercase letters, numbers, and underscores.'),
  97. );
  98. }
  99. $form['locked'] = array(
  100. '#type' => 'value',
  101. '#value' => $bundle->locked,
  102. );
  103. $form['actions'] = array('#type' => 'actions');
  104. $form['actions']['submit'] = array(
  105. '#type' => 'submit',
  106. '#value' => t('Save Paragraph bundle'),
  107. '#weight' => 40,
  108. );
  109. return $form;
  110. }
  111. /**
  112. * Form validation handler for paragraphs_admin_bundle_form().
  113. *
  114. * @see paragraphs_admin_bundle_form_submit()
  115. */
  116. function paragraphs_admin_bundle_form_validate($form, &$form_state) {
  117. $bundle = new stdClass();
  118. $bundle->name = trim($form_state['values']['name']);
  119. if (!$form_state['values']['locked']) {
  120. $bundle->bundle = trim($form_state['values']['bundle']);
  121. // 'theme' conflicts with theme_node_form().
  122. // '0' is invalid, since elsewhere we check it using empty().
  123. if (in_array($bundle->bundle, array('0', 'theme'))) {
  124. form_set_error('type', t("Invalid machine-readable name. Enter a name other than %invalid.", array('%invalid' => $bundle->bundle)));
  125. }
  126. }
  127. }
  128. /**
  129. * Submit handler for paragraphs_admin_bundle_form().
  130. *
  131. * @see paragraphs_admin_bundle_form()
  132. */
  133. function paragraphs_admin_bundle_form_submit($form, &$form_state) {
  134. $bundle = new stdClass();
  135. if (!$form_state['values']['locked']) {
  136. $bundle->bundle = trim($form_state['values']['bundle']);
  137. }
  138. else {
  139. $bundle->bundle = $form['#paragraphs_bundle']->bundle;
  140. }
  141. $bundle->locked = 1;
  142. $bundle->name = trim($form_state['values']['name']);
  143. $variables = $form_state['values'];
  144. // Remove everything that's been saved already - whatever's left is assumed
  145. // to be a persistent variable.
  146. foreach ($variables as $key => $value) {
  147. if (isset($bundle->$key)) {
  148. unset($variables[$key]);
  149. }
  150. }
  151. unset($variables['form_token'], $variables['op'], $variables['submit'], $variables['delete'], $variables['reset'], $variables['form_id'], $variables['form_build_id']);
  152. $status = paragraphs_bundle_save($bundle);
  153. $t_args = array('%name' => $bundle->name);
  154. if ($status == SAVED_UPDATED) {
  155. drupal_set_message(t('The paragraph bundle %name has been updated.', $t_args));
  156. }
  157. elseif ($status == SAVED_NEW) {
  158. drupal_set_message(t('The paragraph bundle %name has been added.', $t_args));
  159. watchdog('node', 'Added paragraph bundle %name.', $t_args, WATCHDOG_NOTICE, l(t('view'), 'admin/structure/paragraphs'));
  160. }
  161. $form_state['redirect'] = 'admin/structure/paragraphs';
  162. return;
  163. }
  164. /**
  165. * Menu callback; delete a single paragraph bundle
  166. *
  167. * @ingroup forms
  168. */
  169. function paragraphs_admin_bundle_delete_form($form, &$form_state, $bundle) {
  170. if (!$bundle) {
  171. drupal_set_message(t('Could not load bundle'), 'error');
  172. drupal_goto('admin/structure/paragraphs');
  173. }
  174. $form['type'] = array('#type' => 'value', '#value' => $bundle->bundle);
  175. $form['name'] = array('#type' => 'value', '#value' => $bundle->name);
  176. $message = t('Are you sure you want to delete the paragraph bundle %bundle?', array('%bundle' => $bundle->name));
  177. $caption = '<p>' . t('This action cannot be undone. Content using the bundle will be broken.') . '</p>';
  178. return confirm_form($form, filter_xss_admin($message), 'admin/structure/paragraphs', filter_xss_admin($caption), t('Delete'));
  179. }
  180. /**
  181. * Process paragraph bundle delete confirm submissions.
  182. *
  183. * @see paragraphs_admin_bundle_delete_form()
  184. */
  185. function paragraphs_admin_bundle_delete_form_submit($form, &$form_state) {
  186. paragraphs_bundle_delete($form_state['values']['type']);
  187. $t_args = array('%name' => $form_state['values']['name']);
  188. drupal_set_message(t('The paragraph bundle %name has been deleted.', $t_args));
  189. watchdog('node', 'Deleted paragraph bundle %name.', $t_args, WATCHDOG_NOTICE);
  190. $form_state['redirect'] = 'admin/structure/paragraphs';
  191. return;
  192. }
  193. /**
  194. * Helper to get the title of a bundle.
  195. *
  196. * @param $bundle
  197. * The bundle.
  198. */
  199. function paragraphs_bundle_title_callback($bundle) {
  200. return t('Edit Paragraph Bundle !name', array('!name' => $bundle->name));
  201. }