开发者

Panel page URL - Need to access named arguments

I have a Panel page, which I have given a path of: books/travel-books/%city/%country/%page. The help text underneath the field says "The URL path to get to this page. You may create named placeholders for variable parts of the path by using %name for required elements and !name for optional elements. For example: "node/%node/foo", "forum/%forum" or "dashboard/!input". These named placeholders can be turned into contexts on the arguments form.", so I have named my arguments appropriately.

So now in my code, I need to get the values of those arguments. I've seen arg(0), but that requires me knowing which index the argument has. Is there anyway to access it by the name I gave it in the path? Something like arg('city')??

The reason being, that I need to have similar path arguments on many pages, and need to access the values of these in my module. But the arguments may be in different places for anoth开发者_StackOverflow中文版er page. For instance, another page might be at: flights/%city/%country. Then I want to access the city argument within the same function, but it is at a different index.

Can anyone help?


you can check for arg(0) first, see whether it is 'books', 'flights', whatever... then associate names accordingly. do it as a helper function in a custom module and call it before referencing (wherever you're referencing it).


The text that you quoted from the panels help text is referring to what panels call context.

Panels

Panels has a great use if you want to aware of what context a certain piece of content is being viewed. Fx if you had several shops with different products, you might want to control which blocks in a sidebar would be displayed, based on the shop that the product belonged to.

This is essential what context is in panels and what the named placeholdes are used for. You can be default add different kinds of context, fx nodes, users, taxonomy terms. You can then use the different pieces of context if various ways.

If panels default options is not enough, you can also create your own plugins to panels to make it handle your special cases. But it requires a lot of time to learn how panels work.

Other solutions

Using panels might not be the easiest option for you, it depends what you are aiming for. But if you don't need to make pages that is aware of the context, this would probably be a lot easier to do with views and theming. Views can handle arguments in urls very well, and it is a lot simpler to both style and configure.


In Drupal 7 you can get the arguments from hook_content_type_render

function <your plugin name>_content_type_render($subtype, $conf, $panel_args, $context) {
  $block = new StdClass;
  $block->title = t('test');
  $block->content = 'test panel arg: '.$panel_args[0];
  return $block;
}

Custom panel tutorial

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜