There should be some debug message (activated in module configuration), which allow user to see which method he should defined to replace specified breadcrumb on current page.

Comments

kenorb’s picture

Priority: Normal » Minor
donquixote’s picture

Ah, you mean like "find__node__" for router path "node/%" ?
You can always implement find(), and then dpm($item->route). If this says "node/%", you can then implement "find__node__".

But yes, could be useful to have this kind of debug message as a feature.

kenorb’s picture

Currently I've this:

--- crumbs.decorateItem.inc	(revision 6595)
+++ crumbs.decorateItem.inc	(working copy)
@@ -31,6 +31,7 @@
   
   protected function _invoke($object, $m, $subkey = NULL) {
     foreach (array($m . $this->_method_suffix, $m) as $method) {
+      drupal_set_message(get_class($object) . '=>' . $method);
       if (method_exists($object, $method)) {
         return $object->$method($this->_path, $this->_item, $subkey);
       }

But I don't know if it's the right place to print it.

kenorb’s picture

Already implemented here:
http://drupal.org/project/crumbs_plus

donquixote’s picture

I hope you noticed the 2.x branch?

A quick question for the API redesign:
For a router path of "node/%", would it help if the method was $plugin->findParent__node_x(), instead of $plugin->findParent__node__() ?
The only "risk" would be if some router path does actually use the letter x as an url fragment. I don't believe this is realistic.
For that rare case we could introduce an escape sequence, such as: "/%/" becomes "_x_", but "/x" becomes "__x_", but maybe it is better to just not start with that paranoia.

kenorb’s picture

No, I didn't. That's great.
I think double underscore or triple are good, similar to Views API.

donquixote’s picture

Views API ? Where do they use double underscore?
We need to think about the following characters that are not allowed in method names, but common in router paths:
'-', '_', '%', '/', and in rare cases '.'.

donquixote’s picture

Of course we could invent a really safe notatation, with '_' as the escape character.
So, '-' would become '_d' (d = dash), '_' would become '_u' or '__', '%' would become '_x', '/' would become '_s', and '.' would become '_t' (dot).
The result would be a pain to read,
'node/%/edit' would become 'node_s_x_sedit'. Ouch.

To improve this a little bit, we need to have shortcut for '/%/' and for '/':
'/%/' becomes '_x_'
'/%' becomes '_x', if at the end of the path.
'/' becomes '_slash_' (no, I really don't like this one)
'_' becomes '__'.
'-' becomes '_dash_'
'.' becomes '_dot_'.

donquixote’s picture

Another attempt. Replacements ordered by priority.
'/%/' becomes '_x_'
'/%' becomes '_x', if at the end of the path.
'/x/' becomes '__x_'.
'/x' becomes '__x_', if at the end of the path.
'/' becomes '_', if not followed by '%'.
'_' becomes '__u_' (ugly)
'-' becomes '__dash_' (ugly)
'.' becomes '__dot_' (ugly)

Thus,
'_x_' becomes '__u_x__u_'
'_x' becomes '__u_x', if at the end of the path.
'__' becomes '__u___u_'

If this pattern is now completely reversible, I dunno. But it is horribly ugly for '-', '_' and '.'.
I wish method names were not that restricted ..
Yes I know you can always do if/else magic, but then you don't need these special method names anyway.

The point is not that we want to have method names with '__x_', but simply to avoid false positives for exotic paths. For instance, findParent__node_x() should only match 'node/%', and not 'node/-' or 'node/.' or 'node-_' etc.

donquixote’s picture

I decided to change this mechanic.
Here is the new implementation (on my local version), in crumbs.plugin_engine.inc.

<?php
  protected function _buildMethodSuffix($router_path) {
    $method_suffix = strtolower($router_path);
    $method_suffix = preg_replace('#[^a-z0-9\%]#', '_', $method_suffix);
    $method_suffix = strtr($method_suffix, array('%' => 'x'));
    $reverse = strtr($method_suffix, array('_' => '/'));
    $reverse = preg_replace(array('#/x/#', '#/x$#'), array('/%/', '/%'), $reverse);
    // we need to do this two time to catch things like "/x/x/x/x".
    $reverse = strtr($reverse, array('/x/' => '/%/'));
    if ($reverse === $router_path) {
      return $method_suffix;
    }
    return FALSE;
  }
?>

If this thing returns FALSE for a given router path, then this router path can not be represented by a method name.
In such cases you need to use the basic method (in 2.x this is findParent() and findTitle()), without a suffix.
You can use switch ($item['path']) or if ($item['path'] === '...') to filter for specific router paths.

For instance, router paths that contain a dash "-" or an underscore, an upper case character, etc, do no longer their own method names.

I think this restriction is much better than having to worry about possible ambiguities, or having awkward-looking method names with things like "__dash_" in them. After all, that method suffix trick is just a shortcut, not a technical necessity.

donquixote’s picture

The new syntax is now
findParent__node_x_edit
findParent__node_x
etc.
Where '/%/' is replaced by '_x_'.
Dashes and underscores are simply not allowed in router paths. Or, if you have such a router path, you simply can't use it for a method suffix. You need to deal with that path in the suffix-less method.

<?php
class MyPlugin {
  function findParent__node_x($path, $item) {
    // now we know absolutely that the router path is "node/%".
    ...
  }
  
  function findParent($path, $item) {
    switch ($item['route']) {
      case 'path/with/%/ugly/under_score':
        ...
        break;
      case 'path-with-dashes':
        ...
        break;
    }
  }
}
?>
donquixote’s picture

Status: Active » Needs review

"needs review" = "probably fixed in 2.x"

I think the new scheme is quite obvious. In case of doubt, run dpm(menu_get_item()) to have a look at the router path.
Btw, there is a new debug tool in the 2.x branch, where you can check how the parent path is determined for a given path.

kenorb’s picture

Views examples with double-underscore:
hook_preprocess_views_view__VIEWNAME
http://drupal.org/node/241570#comment-2508512

http://drupal.org/node/165706
See: Dynamic theming

donquixote’s picture

Ah yes.
We also use double underscore, to separate the method base name from the method suffix.
So, what does it mean for crumbs?
I think the new pattern is quite ok, isn't it?

donquixote’s picture

Status: Needs review » Closed (works as designed)

A lot of this discussion is outdated now, and will only confuse people.
It is no longer findParent__node__(), but findParent__node_x(). Etc.

If there is still a need for documentation on the method suffix, let's discuss this in a new issue focused on the Drupal 7 version.

See #1814556: Method suffix for paths with dashes