Index: /Users/andrew/Documents/workspace/pinchin/sites/default/modules/custom_breadcrumbs/custom_breadcrumbs.install =================================================================== --- /Users/andrew/Documents/workspace/pinchin/sites/default/modules/custom_breadcrumbs/custom_breadcrumbs.install (revision 147) +++ /Users/andrew/Documents/workspace/pinchin/sites/default/modules/custom_breadcrumbs/custom_breadcrumbs.install (working copy) @@ -13,6 +13,8 @@ titles varchar(255) NOT NULL default '', paths varchar(255) NOT NULL default '', node_type varchar(64) default '', + pages varchar(255) default '', + placement varchar(64) default '' PRIMARY KEY (bid) ) /*!40100 DEFAULT CHARACTER SET UTF8 */ "); break; @@ -21,7 +23,9 @@ bid serial CHECK (nid >= 0), titles varchar(255) NOT NULL default '', paths varchar(255) NOT NULL default '', - node_type varchar(64) default '' + node_type varchar(64) default '', + pages varchar(255) default '', + placement varchar(64) default '' )"); db_query("CREATE INDEX {custom_breadcrumb}_bid_idx ON {custom_breadcrumb} (bid)"); break; @@ -30,4 +34,23 @@ function custom_breadcrumbs_uninstall() { db_query('DROP TABLE {custom_breadcrumb}'); +} + +/** + * Add the pages visibility column and the insertion option variable. + * + */ +function custom_breadcrumbs_update_2() { + switch ($GLOBALS['db_type']) { + case 'mysql': + case 'mysqli': + $ret[] = update_sql("ALTER TABLE {custom_breadcrumb} ADD COLUMN pages varchar (255) default ''"); + $ret[] = update_sql("ALTER TABLE {custom_breadcrumb} ADD COLUMN placement varchar (64) default 'replace'"); + break; + case 'pgsql': + db_add_column($ret, 'custom_breadcrumb', 'pages', 'varchar(255)', array('not null' => FALSE, 'default' => 'replace')); + db_add_column($ret, 'custom_breadcrumb', 'placement', 'varchar(64)', array('not null' => FALSE, 'default' => 'replace')); + break; + } + return $ret; } \ No newline at end of file Index: /Users/andrew/Documents/workspace/pinchin/sites/default/modules/custom_breadcrumbs/custom_breadcrumbs.module =================================================================== --- /Users/andrew/Documents/workspace/pinchin/sites/default/modules/custom_breadcrumbs/custom_breadcrumbs.module (revision 147) +++ /Users/andrew/Documents/workspace/pinchin/sites/default/modules/custom_breadcrumbs/custom_breadcrumbs.module (working copy) @@ -45,7 +45,8 @@ function custom_breadcrumbs_nodeapi($node, $op, $teaser, $page) { if ($op == 'view' && !$teaser && $page) { - if ($breadcrumb = _custom_breadcrumbs_load_for_type($node->type)) { + if ($breadcrumbs = _custom_breadcrumbs_load_for_type($node->type, drupal_get_path_alias($_GET['q']), 1)) { + $breadcrumb = $breadcrumbs[0]; $titles = explode("\n", $breadcrumb->titles); $paths = explode("\n", $breadcrumb->paths); @@ -59,6 +60,19 @@ $trail[] = l($title, trim($paths[$i])); } } + // Do we insert the breadcrumb at the appropriate place or replace + // any exisiting breadcrumbs? + switch ($breadcrumb->placement) { + case 'begin': + $trail = array_merge($trail, array_slice(drupal_get_breadcrumb(), 1)); + break; + case 'end': + $trail = array_merge(drupal_get_breadcrumb(), array_slice($trail, 1)); + break; + case 'replace': + default: + break; + } drupal_set_breadcrumb($trail); } } @@ -69,7 +83,7 @@ function custom_breadcrumbs_page() { $breadcrumbs = _custom_breadcrumbs_load_all_breadcrumbs(TRUE); - $header = array(t('node type'), ''); + $header = array(t('node type'), t('Filter by paths'), ''); $rows = array(); foreach ($breadcrumbs as $breadcrumb) { @@ -75,6 +89,7 @@ foreach ($breadcrumbs as $breadcrumb) { $row = array(); $row[] = $breadcrumb->node_type; + $row[] = nl2br($breadcrumb->pages); $row[] = l(t('edit'), 'admin/build/custom_breadcrumbs/edit/' . $breadcrumb->bid); $rows[] = $row; } @@ -129,6 +144,25 @@ '#description' => t('A list of Drupal paths for the breadcrumb links, one on each line.'), '#default_value' => $bid ? $breadcrumb->paths : NULL ); + + $form['pages'] = array( + '#type' => 'textarea', + '#title' => t('Filter by Paths'), + '#description' => t('A list of Drupal paths to show this breadcrumb on. Use to create multiple breadcrumbs of the same content type for different sections of the site. Wildcards are permitted.'), + '#default_value' => $bid ? $breadcrumb->pages: NULL, + ); + + $form['placement'] = array( + '#type' => 'radios', + '#title' => t('Breadcrumb Placement'), + '#options' => array( + 'replace' => t('Replace any breadcrumbs with the custom breadcrumb.'), + 'begin' => t('Append to the begining of an existing breadcrumb.'), + 'end' => t('Append to the end of an existing breadcrumb.'), + ), + '#default_value' => $bid ? $breadcrumb->placement : 'replace', + '#description' => t('Choose how to insert this breadcrumb.'), + ); $form['help'] = array( '#type' => 'fieldset', @@ -189,12 +223,33 @@ return $breadcrumb; } -function _custom_breadcrumbs_load_for_type($type) { +/** + * Load all custom breadcrubs matching the given parameters. + * + * @param $type + * Limit to breadcrumbs for node type $type. + * @param $path + * Limit to breadrcumbs matching the given path. + * @param $limit + * Limit the size of the array returned. + * @return Array of breadcrumb objects matching the given parameters. + */ +function _custom_breadcrumbs_load_for_type($type, $path = NULL, $limit = NULL) { $sql = "SELECT * FROM {custom_breadcrumb} WHERE node_type = '%s'"; $result = db_query($sql, $type); + $i = 0; while ($breadcrumb = db_fetch_object($result)) { - return $breadcrumb; + if($limit && $i >= $limit) return $breadcrumbs; + if (!$path || $breadcrumb->pages == '') { + $breadcrumbs[] = $breadcrumb; + $i++; + } + else if (drupal_match_path($path, $breadcrumb->pages)) { + $breadcrumbs[] = $breadcrumb; + $i++; + } } + return $breadcrumbs; } function _custom_breadcrumbs_load_all_breadcrumbs($refresh = FALSE) { @@ -214,15 +269,15 @@ function _custom_breadcrumbs_save_breadcrumb($breadcrumb = NULL) { if (isset($breadcrumb->bid)) { $sql = "UPDATE {custom_breadcrumb} SET"; - $sql .= " titles = '%s', paths = '%s', node_type = '%s'"; + $sql .= " titles = '%s', paths = '%s', node_type = '%s', pages = '%s', placement = '%s'"; $sql .= " WHERE bid = %d"; - db_query($sql, $breadcrumb->titles, $breadcrumb->paths, $breadcrumb->node_type, $breadcrumb->bid); + db_query($sql, $breadcrumb->titles, $breadcrumb->paths, $breadcrumb->node_type, $breadcrumb->pages, $breadcrumb->placement, $breadcrumb->bid); } else { $sql = "INSERT INTO {custom_breadcrumb}"; - $sql .= " (titles, paths, node_type)"; - $sql .= " VALUES ('%s', '%s', '%s')"; - db_query($sql, $breadcrumb->titles, $breadcrumb->paths, $breadcrumb->node_type); + $sql .= " (titles, paths, node_type, pages, placement)"; + $sql .= " VALUES ('%s', '%s', '%s', '%s', '%s')"; + db_query($sql, $breadcrumb->titles, $breadcrumb->paths, $breadcrumb->node_type, $breadcrumb->pages, $breadcrumb->placement); } } @@ -229,4 +284,38 @@ function _custom_breadcrumbs_delete_breadcrumb($bid) { $sql = 'DELETE FROM {custom_breadcrumb} WHERE bid = %d'; db_query($sql, $bid); +} + +/** + * Taken from cacheexclude.module as suggested by the author. Thanks! + */ + +// This function is new in Drupal 6, but is oh-so-useful here that I'm backporting it. +// When upgrading this module to Drupal 6, remove this function. +if (!function_exists('drupal_match_path')) { + /** + * Check if a path matches any pattern in a set of patterns. + * + * @param $path + * The path to match. + * @param $patterns + * String containing a set of patterns separated by \n, \r or \r\n. + * + * @return + * Boolean value: TRUE if the path matches a pattern, FALSE otherwise. + */ + function drupal_match_path($path, $patterns) { + static $regexps; + + // This part is not in Drupal 6, but is necessary because the regex is broken + // for the front page, as always. + if ($path == variable_get('site_frontpage', 'node') && in_array('', array_map('trim', explode("\n", $patterns)))) { + return true; + } + + if (!isset($regexps[$patterns])) { + $regexps[$patterns] = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($patterns, '/')) .')$/'; + } + return preg_match($regexps[$patterns], $path); + } } \ No newline at end of file