As discussed in #687512: Is splitting [menupath] token into indvidual breadcrumbs possible? adding new special identifiers to custom_breadcrumbs could solve some issues.

Therefore I wrote a patch to make implementing 'special identifiers' pluggable, so any module could define its own identifiers.

The attached patch contains following steps:

  • Added hook_cb_identifiers_info() to define new identifiers with identifier as key, 'title', 'help', 'callback' and 'file'
  • Retrieve all defined identifiers with custom_breadcrumbs_get_identifiers
  • Altered theme_custom_breadcrumbs_help_identifiers() to show pluggable identifiers
  • Added _custom_breadcrumbs_get_breadcrumb() to custom_breadcrumbs_set_breadcrumb() to make the get-method available for other functions
  • Breadcrumbs are passed as arrays of 'crumb items' that contain 'title', 'href' (needed for menu_set_active_trail) and 'crumb' (the actual html-output)
  • Replaced _custom_breadcrumbs_create_crumb with _custom_breadcrumbs_create_crumb_items to support multiple crumb creation
  • unfortunately there are some white-space corrections within the patch - sorry for that
  • the attached custom_breadcrumbs.identifiers.inc contains the default special identifier callbacks function and has to be added too for the patch.

It was nice if you could review the patch asap, so I could work out a patch for #687512.

Comments

derhasi’s picture

Title: Make special identifiers pluggable - and capable of building mutliple crumbs » Make special identifiers pluggable - and capable of building multiple crumbs
StatusFileSize
new929 bytes
new16.1 KB

Unfortunately the attached patch files disapperead, so have to upload it again.

derhasi’s picture

StatusFileSize
new13.24 KB

Attached the same patch, but without trailing spaces diffs, so it should be more readable.

Also needs http://drupal.org/files/issues/custom_breadcrumbs.identifiers.inc__0.txt from above

derhasi’s picture

StatusFileSize
new13.98 KB

New patch attached, because I did not call custom_breadcrumbs_get_identifiers() in theme_custom_breadcrumbs_help_identifiers().

http://drupal.org/files/issues/custom_breadcrumbs.identifiers.inc__0.txt still required.

TripleEmcoder’s picture

Subscribing.

MGN’s picture

Status: Needs review » Needs work

Thanks for contributing this derhasi! I think this is the correct approach, and have been wanting to extend the use of identifiers. Rather than building the identifier-specific code into the custom breadcrumbs module, it makes sense to put it into its own file (like you did in custom_breadcrumbs.identifiers.inc).

I might prefer to do a couple of things slightly differently, but give me a little time to test this out. Most of this looks great.

I am thinking of building on the approach taken in the token module with tokenSTARTER. In this case, we would provide custom_breadcrumbs_identifiers.module and build in the basic identifiers. Then users could add their own identifiers to this module (rather than having to create their own). Of course other modules could use this new api in the way that you intended it as well. As users contributed ideas to extend the base set of identifiers, we could add them to this new module. And if users don't want to use identifiers at all, the module wouldn't need to be enabled. What do you think about this?

I am marking this as needs work for now, just because I want to try out this approach and see if it has merit, not that I see any major problems with your patch.

derhasi’s picture

Thanks for your quick answer.
I don't understand what you mean with "approch in tokenSTARTER", or better to say, I cannot see a differenece to the current patch's approach. So maybe you could explain your thoughts to me.

As you said it would be nice to have a custom_breadcrumbs_identifiers.module that contains some basic identifiers. I agree, but the main functionality (hook implementation, _custom_breadcrumbs_create_crumb_items()) has to stay in custom_breadcrumbs core. What can be done is to move custom_breadcrumbs.identifiers.inc and custom_breadcrumbscb_identifiers_info() to the submodule (custom_breadcrumbs_identifiers.module).

One thing I don't like about my patch is the handling of $locations for menu set active trail. dereine suggested to use a ctools plugin to remove $locations from the functions. At the moment I don't exactly know, how it could be done with ctools, but could be worth a try.

MGN’s picture

Title: Make special identifiers pluggable - and capable of building multiple crumbs » Provide hooks for modules to provide special identifiers for custom breadcrumbs, capable of building multiple crumbs
Assigned: derhasi » MGN
Status: Needs work » Needs review
StatusFileSize
new1.41 KB
new11.87 KB

I've modified derhasi's patch so the special identifiers provided by custom breadcrumbs comes in a separate module, custom_breadcrumbs_identifiers. This allows the use of module_implements() and module_invoke() which simplifies the implementation (no need to define separate callback functions and do file includes to provide the feature). It also will better allow for future special identifier expansions.

I've also tried to simplify the approach by providing two hooks that other modules can implement to provide special identifiers : hook_cb_identifier_list() is used in the custom breadcrumbs ui to describe the behavior of each special identifier, and hook_cb_identifier_values() is used to provide the replacement value for the special identifier. As with derhasi's patch, the key feature is that special identifiers can return an array of crumbs.

I found a couple of bugs in the previous patch that are fixed in this one. The home breadcrumb trail is handled correctly now. There was also a typo with a $location needing to be $locations. Finally, I've adjusted the code comments. Otherwise, the approach is the same as in derhasi's patch.

The new submodule includes a <book-hierarchy> identifier that demonstrates multiple-crumb replacement using parent page titles and paths to form a breadcrumb for book pages. I think this identifier may an improvement over the bookpath tokens.

derhasi’s picture

I'm looking forward to review the patch. But cannot promise to do it until the weekend.

MGN’s picture

@derhasi, have you had a chance to review this? I would like to commit soon and prepare to release 6.x-2.0-beta3.

derhasi’s picture

MGN, couldn't review it, but will do it tomorrow. Just wrote it down to my todo-list ...

derhasi’s picture

I finally found time to review your patch.

With your implementation in _custom_breadcrumbs_create_crumb_items() on line 535 you built in a "new feature". As I'm not sure you recognized it, I'll point it out:
Modules can implement identifiers in hook_cb_identifiers_values() without defining them in hook_cb_identifiers_list().
This is an advantage for building multiidentifiers ([identifier-*]) or identifiers with options [identifier:bold:12px]. On the other hand it does not check if the module is "owner" of the identifier by identifying it in hook_cb_identifiers_list(). This is no problem, it only confused me in the first moment.

return value check in _custom_breadcrumbs_create_crumb_items()

A Problem is your return value check in line 537. There the return should be checked on NULL, because a value-function may implement an empty array ( return array()), so it can define an item, that does not return any crumb items. For example this is necessary for a parent-menu-item implementation - if there is no parent item, there should be no crumb for that.

In the attached patch I replaced line 537 with

    // Break on the first implementation that does return a value not NULL.
    if (isset($values)) break;

line 539 with if (isset($values)) {.
In custom_breadcrumbs_identifiers_cb_identifier_values there should be set line 45 to $crumb_items = NULL;.

I built a module to implement a parent-menu identifier. It's located on http://github.com/derhasi/dev/tree/custom_breadcrumbs_identifier.

enable custom_breadcrumbs_identifiers on update

For legacy reasons, the new submodule should be enabled automatically, to avoid missing identifiers:
therefore I adjusted custom_breadcrumbs.install:

/**
 * Enable custom_breadcrumbs_identifiers for legacy.
 */
function custom_breadcrumbs_update_6203() {
  module_enable(array('custom_breadcrumbs_identifiers'));
  $return[] = array(
    'success' => module_exists('custom_breadcrumbs_identifiers'),
    'query' => 'Custom_breadcrumbs_identifiers was enabled for legacy reasons. Please disable it, if you do not use special identifiers in your bredcrumb settings.',
  );
  return $return;
}

title check in _custom_breadcrumbs_get_trail_items()

as some identifiers do not need a title to be set, I'd remove or move the title check. Did not implement a "fix" for that in the attachment.

Besides I adjusted the custom_breadcrumbs_identifiers module to fit in coding standards.

MGN’s picture

Status: Needs review » Fixed

Thanks for the careful review. I agree with everything you have indicated above, and I appreciate the automatic enabling of custom_breadcrumbs_identifiers on update. I expect that will save me a half dozen support requests!

I did not remove the title check in _custom_breadcrumbs_get_trail_items() at this time. While not needed for special identifiers, the check was added to prevent unintended whitespace from developing into a crumb. I think its sufficient to require a title to be present even if it is just a placeholder that will be silently ignored. [<none> should not be used as the placeholder since it will abort the breadcrumb generation, but the identifier itself could be used...] I've tried to indicate this in the description of the identifiers.

This has now been committed to 6.x-2.x-dev.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.