drupal---theme code
$i = 0;
$suggestion = 'page';
$suggestions = array();
while ($arg = arg($i++)) {
$arg = str_replace(array("/", "\\", "\0"), '', $arg);
$suggestions[] = $suggestion . '-' . $arg;
if (!is_numeric($arg))开发者_StackOverflow {
$suggestion .= '-' . $arg;
}
}
i am a newbie of drupal,i can't follow the above code well, hope someone can explain it to me.i know the first line is assign the 0 to $i,then assign 'page' to an array. and i know arg is an array in drupal. for example, now the url is example.com/node/1. how to use this url to use the above code.
It looks like its purpose is to generate ID strings (probably for a CSS class) depending on paths and excludes numeric components of the path out of the generated ID.
For example, 'my/123/article'
produces the ID "page-my-article"
.
It seems this comes from a function (because the loop reads parameters using arg()
) and that it expects Drupal paths such as "node/123/edit"
.
So the function would be called something like that:
mystery_function("my/123/article", "even/better/article");
Variables:
$i
is the variable that stores the loop index
$suggestion
is a String that store the generated ID. It is initialized to "page" because the ID is meant to have the syntax "page-SOMETHING".
$arg
comes from the while loop: it reads the parameters passed to the mystery function one by one
$suggestions
is an array that contains the generated IDs, one per argument passed to the mystery function.
In the loop:
The "$arg = str_replace...
" line removes unwanted characters like "\" (however that line could definitely be improved).
The "$suggestions[] = ...
" line adds the ID to the array of results.
The "if (!is_numeric($arg)...
" line excludes numbers from the generated ID (e.g. "my/123/article" is probably supposed to produce "my-article")
The "$suggestion .= ...
" line appends the value of "$arg" to the value of "$suggestion" and stores it in "$suggestion"
But honestly, I wouldn't advise to use that code: I doubt it works as intended given $suggestion
isn't reinitialized at each loop so the value of the first path will get attached to the second, and to the third, and so on, and I doubt that is intentional.
Most likely this code is located in a theme's template.php in the preprocess_page hook. If that is the case, it's used to create template suggestions based on an argument supplied like the node id, to make it possible to create a page template per node.
What this code does, is that it loops though all the arguments in the drupal url, This could be user/3
, node/3
, taxonomy/term/3
or any custom url.
- It first does some cleanup in the argument, to make sure that no weird symbols is added. This is not needed for most urls, but is probably there as a safety to avoid needing to have to create weird template names in some cases. This is done with
str_replace
- Next it adds the a suggestion to the list, based on the arg.
- If the arg isn't numeric it adds that to the suggestion so it will be used in the next loop.
The idea is that you with the above urls will get added some template suggestions that look like this:
- page
- page-user
- page-user-3
And
- page
- page-taxonomy
- page-taxonomy-term
- page-taxonomy-term-3
In this list drupal will use the last one possible, so if page-user-3.tpl.php exists, that will be used as page template for user/3
, if not page-user.tpl.php will be used and so on.
This can be desired if you want to create customize page templates for the users page, or the nodes page while being able to create customized page templates for specific users.
This is not, however, a strategy I would want to employ. If you do this, you will end of with lots of different versions of a page template, and it will end up creating the maintenance nightmare that CMS systems was supposed to eliminate. If you truly need this many different page templates, you should instead look at context or panels, and put some logic into this instead.
精彩评论