menu_block_export.admin.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @file
  4. * Provides infrequently used functions and hooks for menu_block_export.
  5. */
  6. /**
  7. * Page callback to export menu blocks in bulk.
  8. */
  9. function menu_block_export_export() {
  10. $blocks = variable_get('menu_block_ids', array());
  11. if (!empty($blocks)) {
  12. $form_state = array(
  13. 'no_redirect' => TRUE,
  14. );
  15. $output = drupal_build_form('menu_block_export_form', $form_state);
  16. if (!empty($form_state['output'])) {
  17. $output = $form_state['output'];
  18. }
  19. return $output;
  20. }
  21. else {
  22. return t('There are no menu blocks to be exported at this time.');
  23. }
  24. }
  25. /**
  26. * Menu callback; export form.
  27. *
  28. * @return
  29. * The export form used by Menu block.
  30. */
  31. function menu_block_export_form($form, &$form_state) {
  32. $form['name'] = array(
  33. '#type' => 'textfield',
  34. '#title' => t('Module name'),
  35. '#default_value' => 'custom',
  36. '#description' => t('Enter the module name to export code to.'),
  37. );
  38. $form['submit'] = array(
  39. '#type' => 'submit',
  40. '#value' => t('Export'),
  41. );
  42. $form['#action'] = url('admin/config/user-interface/menu-block/export/results');
  43. return $form;
  44. }
  45. /**
  46. * Submit callback for menu_block_export_form().
  47. */
  48. function menu_block_export_form_submit(&$form, &$form_state) {
  49. $output = '';
  50. $module = check_plain($form_state['values']['name']);
  51. foreach (variable_get('menu_block_ids', array()) AS $delta) {
  52. $config = menu_block_get_config($delta);
  53. // Use the constant instead of the constant's value.
  54. if ($config['menu_name'] == MENU_TREE__CURRENT_PAGE_MENU) {
  55. $config['menu_name'] = 'MENU_TREE__CURRENT_PAGE_MENU';
  56. }
  57. else {
  58. // If it's not the constant, wrap value in quotes.
  59. $config['menu_name'] = "'" . $config['menu_name'] . "'";
  60. }
  61. $output .= <<<END_OF_CONFIG
  62. '$module-$delta' => array(
  63. 'menu_name' => {$config['menu_name']},
  64. 'parent_mlid' => {$config['parent_mlid']},
  65. 'title_link' => {$config['title_link']},
  66. 'admin_title' => '{$config['admin_title']}',
  67. 'level' => {$config['level']},
  68. 'follow' => {$config['follow']},
  69. 'depth' => {$config['depth']},
  70. 'expanded' => {$config['expanded']},
  71. 'sort' => {$config['sort']},
  72. ),
  73. END_OF_CONFIG;
  74. }
  75. $output = <<<END_OF_CONFIG
  76. /**
  77. * Implements hook_menu_block_blocks().
  78. */
  79. function {$module}_menu_block_blocks() {
  80. // The array key is the block delta used by menu block.
  81. return array($output
  82. );
  83. }
  84. END_OF_CONFIG;
  85. $element = array(
  86. '#type' => 'textarea',
  87. '#title' => t('Use this in your !module.module file:', array('!module' => $module)),
  88. '#value' => $output,
  89. '#rows' => 20,
  90. // Since this isn't a real form, manually add additional required properties.
  91. '#id' => 'menu-block-export-textarea',
  92. '#name' => 'export',
  93. '#required' => FALSE,
  94. '#attributes' => array('style' => 'font-family: monospace;'),
  95. '#title_display' => 'before',
  96. '#parents' => array('dummy'),
  97. );
  98. $form_state['output'] = drupal_render($element);
  99. }