diff --git a/custom_breadcrumbs.admin.inc b/custom_breadcrumbs.admin.inc index 5082952..6863a3a 100644 --- a/custom_breadcrumbs.admin.inc +++ b/custom_breadcrumbs.admin.inc @@ -11,20 +11,25 @@ function custom_breadcrumbs_page() { $breadcrumbs = _custom_breadcrumbs_load_all_breadcrumbs(TRUE); - $header = array(t('Node type'), ''); + $header = array(t('Node type'), '', ''); $rows = array(); foreach ($breadcrumbs as $breadcrumb) { $row = array(); $row[] = $breadcrumb->node_type . (!empty($breadcrumb->visibility_php) ? ' ' . t('with PHP snippet') : ''); - $row[] = l(t('edit'), 'admin/structure/custom_breadcrumbs/edit/' . $breadcrumb->bid); + if (empty($breadcrumb->disabled)) { + $row[] = l(t('edit'), 'admin/structure/custom_breadcrumbs/edit/' . $breadcrumb->name); + } else { + $row[] = l(t('enable'), 'admin/structure/custom_breadcrumbs/enable/' . $breadcrumb->name); + } + $row[] = l(t('export'), 'admin/structure/custom_breadcrumbs/export/' . $breadcrumb->name); $rows[] = $row; } if (count($rows) == 0) { - $rows[] = array(array('data' => t('No custom breadcrumbs have been defined.'), 'colspan' => 2)); + $rows[] = array(array('data' => t('No custom breadcrumbs have been defined.'), 'colspan' => 3)); } - $rows[] = array(array('data' => l(t('Add a new custom breadcrumb'), 'admin/structure/custom_breadcrumbs/add'), 'colspan' => 2)); + $rows[] = array(array('data' => l(t('Add a new custom breadcrumb'), 'admin/structure/custom_breadcrumbs/add'), 'colspan' => 3)); $build = array(); $build['breadcrumb_table'] = array('#markup' => theme('table', array('header' => $header, 'rows' => $rows))); @@ -36,13 +41,15 @@ function custom_breadcrumbs_page() { * Displays an edit form for a custom breadcrumb record. */ function custom_breadcrumbs_form($form, &$form_state) { - $bid = arg(4); - if (isset($bid)) { - $breadcrumb = _custom_breadcrumbs_load_breadcrumb($bid); - $form['bid'] = array( - '#type' => 'hidden', - '#value' => $bid, - ); + $name = arg(4); + if (isset($name)) { + $breadcrumb = _custom_breadcrumbs_load_breadcrumb($name); + if (!empty($breadcrumb->bid)) { + $form['bid'] = array( + '#type' => 'hidden', + '#value' => $breadcrumb->bid, + ); + } } $options = array(); @@ -60,6 +67,20 @@ function custom_breadcrumbs_form($form, &$form_state) { '#default_value' => isset($breadcrumb->node_type) ? $breadcrumb->node_type : NULL, ); + $form['name'] = array( + '#type' => 'machine_name', + '#title' => t('Machine name'), + '#required' => TRUE, + '#description' => t('The machine name for exporting.'), + '#default_value' => isset($breadcrumb->name) ? $breadcrumb->name : NULL, + '#maxlength' => 255, + '#machine_name' => array( + 'exists' => 'custom_breadcrumbs_machine_name_exists', + 'source' => array('node_type'), + ), + '#disabled' => isset($breadcrumb->name), + ); + $form['visibility_php'] = array( '#type' => 'textarea', '#title' => t('Breadcrumb visibility'), @@ -110,17 +131,36 @@ function custom_breadcrumbs_form($form, &$form_state) { '#type' => 'submit', '#value' => t('Submit'), ); - if ($bid) { - $form['buttons']['delete'] = array( - '#type' => 'submit', - '#value' => t('Delete'), - '#submit' => array('custom_breadcrumbs_form_delete'), - ); + + if ($name) { + if (!empty($breadcrumb->export_type) && $breadcrumb->export_type == EXPORT_IN_CODE) { + $form['buttons']['disable'] = array( + '#type' => 'submit', + '#value' => t('Disable'), + '#submit' => array('custom_breadcrumbs_form_disable'), + ); + } else { + $form['buttons']['delete'] = array( + '#type' => 'submit', + '#value' => t('Delete'), + '#submit' => array('custom_breadcrumbs_form_delete'), + ); + } } return $form; } +/** + * + * Returns whether a machine name already exists. + * + * @param string $value + */ +function custom_breadcrumbs_machine_name_exists($value) { + return custom_breadcrumbs_obj_load($value); +} + function custom_breadcrumbs_form_validate($form, &$form_state) { $path_count = count(explode("\n", trim($form_state['values']['paths']))); $title_count = count(explode("\n", trim($form_state['values']['titles']))); @@ -137,6 +177,69 @@ function custom_breadcrumbs_form_submit($form, &$form_state) { } function custom_breadcrumbs_form_delete($form, &$form_state) { - _custom_breadcrumbs_delete_breadcrumb($form_state['values']['bid']); + _custom_breadcrumbs_delete_breadcrumb($form_state['values']['name']); + $form_state['redirect'] = 'admin/structure/custom_breadcrumbs'; +} + +function custom_breadcrumbs_form_disable($form, &$form_state) { + _custom_breadcrumbs_disable_breadcrumb($form_state['values']['name']); $form_state['redirect'] = 'admin/structure/custom_breadcrumbs'; } + +/** + * + * Enable a featured object + * @param unknown_type $form + * @param unknown_type $form_state + * @param unknown_type $machine_name + */ +function custom_breadcrumbs_enable_obj($form, &$form_state, $machine_name) { + $obj = custom_breadcrumbs_obj_load($machine_name); + if ($obj) { + ctools_export_set_object_status($obj, false); + } + drupal_goto('admin/structure/custom_breadcrumbs'); +} + +/** +* Export a myobj and display it in a form. +*/ +function custom_breadcrumbs_export_obj($form, &$form_state, $machine_name) { + $obj = custom_breadcrumbs_obj_load($machine_name); + if (!$obj) { + drupal_goto('admin/structure/custom_breadcrumbs'); + } + drupal_set_title(check_plain($obj->name)); + $code = custom_breadcrumbs_obj_export($obj); + $lines = substr_count($code, "\n"); + + $form['export'] = array( + '#title' => t('Export data'), + '#type' => 'textarea', + '#value' => $code, + '#rows' => $lines, + '#description' => t('Copy the export text and paste it custom breadcrumbs using the import function.'), + ); + return $form; +} + +/** +* Load a single obj. +*/ +function custom_breadcrumbs_obj_load($machine_name) { + ctools_include('export'); + $result = ctools_export_load_object('custom_breadcrumb', 'names', array($machine_name)); + if (isset($result[$machine_name])) { + return $result[$machine_name]; + } + return false; +} + +/** +* Export a myobj. +*/ +function custom_breadcrumbs_obj_export($obj, $indent = '') { + ctools_include('export'); + $output = ctools_export_object('custom_breadcrumb', $obj, $indent); + return $output; +} diff --git a/custom_breadcrumbs.info b/custom_breadcrumbs.info index 45c7b80..c5dfbfe 100644 --- a/custom_breadcrumbs.info +++ b/custom_breadcrumbs.info @@ -1,6 +1,7 @@ name = Custom Breadcrumbs description = Allows administrators to define custom breadcrumb trails for node types. core = 7.x +dependencies[] = ctools files[] = custom_breadcrumbs.module files[] = custom_breadcrumbs.admin.inc files[] = custom_breadcrumbs.install diff --git a/custom_breadcrumbs.install b/custom_breadcrumbs.install index 279470f..4989726 100644 --- a/custom_breadcrumbs.install +++ b/custom_breadcrumbs.install @@ -11,23 +11,40 @@ function custom_breadcrumbs_schema() { $schema['custom_breadcrumb'] = array( 'description' => 'Stores custom breadcrumb trail overrides.', + 'export' => array( + 'key' => 'name', + 'primary key' => 'bid', + 'identifier' => 'custom_breadcrumb', + 'default hook' => 'default_custom_breadcrumbs_preset', + 'api' => array( + 'owner' => 'custom_breadcrumbs', + 'api' => 'default_custom_breadcrumbs_presets', + 'minimum_version' => 1, + 'current_version' => 1, + ), + ), 'fields' => array( + 'name' => array( + 'type' => 'varchar', + 'length' => '255', + 'default' => '', + 'not null' => TRUE, + 'description' => 'Unique ID for presets. Used to identify them programmatically.', + ), 'bid' => array( 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Unique identifier for the {custom_breadcrumb}.', + 'no export' => TRUE, // Do not export database-only keys. ), 'titles' => array( - 'type' => 'varchar', - 'length' => 255, + 'type' => 'text', 'not null' => TRUE, - 'default' => '', 'description' => 'A return-delimited list of titles for the breadcrumb links.', ), 'paths' => array( - 'type' => 'varchar', - 'length' => 255, + 'type' => 'text', 'not null' => FALSE, 'description' => 'A return-delimited list of url paths for the breadcrumb links.', ), @@ -45,6 +62,9 @@ function custom_breadcrumbs_schema() { 'description' => 'Node types the {custom_breadcrumb} should apply to.', ), ), + 'unique keys' => array( + 'name' => array('name') + ), 'primary key' => array('bid'), ); return $schema; @@ -59,3 +79,29 @@ function custom_breadcrumbs_update_6001() { function custom_breadcrumbs_update_6101() { db_drop_field('custom_breadcrumb', 'set_active_menu'); } + +// Larger text fields & Features export +function custom_breadcrumbs_update_6102() { + db_change_field('custom_breadcrumb', 'titles', 'titles', array('type' => 'text', 'not null' => TRUE)); + db_change_field('custom_breadcrumb', 'paths', 'paths', array('type' => 'text', 'not null' => FALSE)); + + db_add_field( + 'custom_breadcrumb', + 'name', + array( + 'type' => 'varchar', + 'length' => '255', + 'default' => '', + 'not null' => TRUE, + 'description' => 'Unique ID for presets. Used to identify them programmatically.' + ), + array('name' => array('name')) + ); + + $ret[] = update_sql("UPDATE {custom_breadcrumb} SET name = bid"); + + // rebuild the schema cache + drupal_get_schema('custom_breadcrumb', true); + + return $ret; +} diff --git a/custom_breadcrumbs.module b/custom_breadcrumbs.module index 244bcbb..fe8b384 100644 --- a/custom_breadcrumbs.module +++ b/custom_breadcrumbs.module @@ -64,6 +64,26 @@ function custom_breadcrumbs_menu() { 'file' => 'custom_breadcrumbs.admin.inc', ); + $items['admin/structure/custom_breadcrumbs/export'] = array( + 'title' => 'Export custom breadcrumb', + 'type' => MENU_CALLBACK, + 'page callback' => 'drupal_get_form', + 'page arguments' => array('custom_breadcrumbs_export_obj'), + 'access callback' => 'user_access', + 'access arguments' => array('administer custom breadcrumbs'), + 'file' => 'custom_breadcrumbs.admin.inc', + ); + + $items['admin/structure/custom_breadcrumbs/enable'] = array( + 'title' => 'Enable custom breadcrumb', + 'type' => MENU_CALLBACK, + 'page callback' => 'drupal_get_form', + 'page arguments' => array('custom_breadcrumbs_enable_obj'), + 'access callback' => 'user_access', + 'access arguments' => array('administer custom breadcrumbs'), + 'file' => 'custom_breadcrumbs.admin.inc', + ); + return $items; } @@ -109,15 +129,39 @@ function custom_breadcrumbs_node_view($node, $build_mode) { } } -function _custom_breadcrumbs_load_breadcrumb($bid) { - $result = db_query('SELECT * FROM {custom_breadcrumb} WHERE bid = :bid', array(':bid' => $bid)); - $breadcrumb = $result->fetchObject(); - return $breadcrumb; +/** +* Implementation of hook_ctools_plugin_api(). +* +* Tell CTools that we support the default_mymodule_presets API. +*/ +function custom_breadcrumbs_ctools_plugin_api($owner, $api) { + if ($owner == 'custom_breadcrumbs' && $api == 'default_custom_breadcrumbs_presets') { + return array('version' => 1); + } +} + +/** +* Implementation of hook_ctools_plugin_directory(). +*/ +function custom_breadcrumbs_ctools_plugin_directory($owner, $plugin_type) { + // Load the export_ui plugin. + if ($plugin_type == 'export_ui') { + return 'plugins/export_ui'; + } +} + +function _custom_breadcrumbs_load_breadcrumb($name) { + return ctools_export_crud_load('custom_breadcrumb', $name); } function _custom_breadcrumbs_load_for_type($node) { - $result = db_query('SELECT * FROM {custom_breadcrumb} WHERE node_type = :node_type', array(':node_type' => $node->type)); - while ($breadcrumb = $result->fetchObject()) { + + $results = ctools_export_load_object('custom_breadcrumb', $type = 'names', array('node_type' => $node->type)); + + foreach ($results as $breadcrumb) { + if (!empty($breadcrumb->disabled)) { + continue; + } if (!empty($breadcrumb->visibility_php)) { // Use PHP code to check the visibility. ob_start(); @@ -134,12 +178,7 @@ function _custom_breadcrumbs_load_for_type($node) { } function _custom_breadcrumbs_load_all_breadcrumbs($refresh = FALSE) { - static $breadcrumbs; - if ($refresh || !isset($breadcrumbs)) { - $result = db_query('SELECT * FROM {custom_breadcrumb}'); - $breadcrumbs = $result->fetchAll(); - } - return $breadcrumbs; + return ctools_export_crud_load_all('custom_breadcrumb', $refresh); } function custom_breadcrumbs_save_breadcrumb($breadcrumb = NULL) { @@ -158,9 +197,19 @@ function custom_breadcrumbs_save_breadcrumb($breadcrumb = NULL) { } } -function _custom_breadcrumbs_delete_breadcrumb($bid) { - db_delete('custom_breadcrumb')->condition('bid', $bid)->execute(); +function _custom_breadcrumbs_delete_breadcrumb($name) { + db_delete('custom_breadcrumb')->condition('name', $name)->execute(); +} + +/** + * + * Disable the entry + * @param string $name + */ +function _custom_breadcrumbs_disable_breadcrumb($name) { + ctools_export_set_status('custom_breadcrumb', $name, TRUE); } + /** * Private function for custom breadcrumb to create a crumb item * diff --git a/plugins/export_ui/cb_export_ui.inc b/plugins/export_ui/cb_export_ui.inc new file mode 100644 index 0000000..1fe7c07 --- /dev/null +++ b/plugins/export_ui/cb_export_ui.inc @@ -0,0 +1,73 @@ + 'custom_breadcrumb', // As defined in hook_schema(). + 'access' => 'administer custom breadcrumbs', // Define a permission users must have to access these pages. + + // Define the menu item. + 'menu' => array( + 'menu item' => 'custom_breadcrumbs', + 'menu title' => 'Custom Breadcrumbs', + 'menu description' => 'Administer Custom Breadcrumbs presets.', + ), + + // Define user interface texts. + 'title singular' => t('preset'), + 'title plural' => t('presets'), + 'title singular proper' => t('Custom Breadcrumbs preset'), + 'title plural proper' => t('Custom Breadcrumbs presets'), + + // Define the names of the functions that provide the add/edit forms. + 'form' => array( + 'settings' => 'custom_breadcrumbs_ctools_export_ui_form', + ), +); + +/** +* Define the preset add/edit form. +*/ +function custom_breadcrumbs_ctools_export_ui_form(&$form, &$form_state) { + $preset = $form_state['item']; + + $form['node_type'] = array( + '#type' => 'textfield', + '#title' => t('Node type'), + '#description' => t('The node type this custom breadcrumb trail will apply to.'), + '#default_value' => $preset->node_type, + '#required' => true, + ); + + $form['visibility_php'] = array( + '#type' => 'textarea', + '#title' => t('Breadcrumb visibility '), + '#description' => t('Determine whether this breadcrumb should be displayed by using a snippet of PHP to return TRUE or FALSE. Note that this code has access to the $node variable, and can check its type or any other property.'), + '#default_value' => $preset->visibility_php, + '#required' => false, + ); + + $form['titles'] = array( + '#type' => 'textarea', + '#title' => t('Titles'), + '#description' => t('A list of titles for the breadcrumb links, one on each line.'), + '#default_value' => $preset->titles, + '#required' => true, + ); + + $form['paths'] = array( + '#type' => 'textarea', + '#title' => t('Paths'), + '#description' => t('A list of Drupal paths for the breadcrumb links, one on each line.'), + '#default_value' => $preset->paths, + '#required' => true, + ); + +} \ No newline at end of file