欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

给drupal 单独页面定制主题

shiping1 的头像
来自 http://drupal.stackexchange.com/questions/812/how-do-i-change-a-theme-based-on-the-url/816#816

Drupal的答案是Drupal的开发人员和管理员一问一答的网站。这是100%免费,无需注册。

我有example.org运行Drupal的实例。其中有一个主题,安装并启动X。现在,在有限时间内的事件应该举行,有关的专门章节已在现场“刻出来。” 所有有关该事件的事情会去example.org/event2011。

我希望有一个不同的主题,每当本条所指的页面查看(如要显示http://example.org/event2011/abouthttp://example.org/event2011/node/123)。我该怎么做呢?

我经历了许多模组走了,但他们都不是配套的Drupal 7。最好,我想它使用的模块来完成,并且不希望在代码级别改变任何东西我自己。

分享改善这个问题
 
1 
您是否希望有一个完整而独立的站点下的路径/ event2011/node/123看起来,你是?  杰里米法国 3月16日'11 12:49
添加评论

像其他意见的自定义解决方案建议可能是最好的,但如果你真的想用一个模块,你最好的赌注是ThemeKey截至5/23/11,它有一个稳定的版本。

分享改善这个答案
 添加评论

考虑到变化是一个有限的时间段,而实施这些代码并不困难(我说的不是改变了核心代码,这是不是一个好主意,这样做的),我建议实现一个自定义模块,改变正在使用的特定页面的主题。

它足以实现hook_custom_theme() 如下所示:

功能mymodule_custom_theme () { 
  如果 ARG 0  ==  'event2011'  { 
    回报 “为主题,以用于该页面' ; 
  } 
}

如果您需要更改主题只适用于http://example.com/event2011,但不适用于http://example.com/event2011/node/123,那么代码应改为

功能mymodule_custom_theme () { 
  如果 ARG 0  ==  'event2011'  &&  ARG 1 )) { 
    返回 '为主题,以用于该页面' ; 
  } 
}

至于使用的主题回调的菜单回调的定义,该文件说:

作为一般规则,采用主题回调函数应只限于它的功能是非常密切依赖于特定的主题页面,因为它们只能通过模块,专门针对在这些页面被覆盖hook_menu_alter() 实施更通用主题切换功能(例如,一个模块,它允许主题被动态地设置基于当前用户的角色)的模块应该使用hook_custom_theme()来代替。

分享改善这个答案
 添加评论

另外,您可以使用新的主题回调菜单系统的选项hook_menu_alter)(如下所述。PS结帐hook_menu()有关更多详细信息主题回调

<?PHP
 / **
*用工具hook_menu_alter()。
* / 
函数mymodule_menu_alter (&$项 { 
  / /设置主题回调函数对所有节点的页面。按照该
  为hook_menu / /标准行为()的属性,这将是
  / /继承所有路径节点/%节点下为好,除非
  / /他们定义自己的主题回调。
  $项目[ '节点/%节点' ] [ “主题回调' ]  =  'mymodule_default_node_theme' ;

  / /设置一个不同的主题回调节点编辑页面,并通过
  / /沿节点对象到这个功能,所以我们可以做出决定,
  基于它/ /。
  $项目[ '节点/%节点/编辑' ] [ '主题回调' ]  =  'mymodule_edit_node_theme' ; 
  $项目[ '节点/%节点/编辑' ] [ '主题论据' ]  = 阵列1 ); 
} 
/ **
*默认为使用“some_theme”主题节点页面。
* / 
函数mymodule_default_node_theme () { 
  返回 'some_theme' ; 
}

/ **
*对于编辑页面的节点,使用'some_other_theme“的主题。
*/
function mymodule_edit_node_theme($node) {
  return $node->type == 'page' ? 'some_other_theme' : mymodule_default_node_theme();
}
?>

另外也有使用更传统的一例hook_custom_theme()

<?PHP 
 / **
*实hook_custom_theme()。
* / 
函数mymodule_custom_theme () { 
  / /检查路径使用ARG(0)
  / /检查$ USER 
  / /做任何特殊的检查,你想和简单的返回键的主题(主题文件夹的名称大部分时间)
    返回 'special_theme' ; 
  } 
} 
?>

:取自http://drupal.org/node/224333#custom_theme

分享改善这个答案
 添加评论

你可以简单地使用上下文和使用主题得到应用时,路径是/ event2011 /你可以简单的设置路径在cotext的状况和改变主题的内容的作用。这是你可以主题之间基于URL的网站很容易切换。甚至也适用于手机;)

分享改善这个答案
 
  
主题行动传递自定义变量的主题,它实际上并没有改变它  亚历 5月6日'11 19:55
添加评论

如何根据用户角色切换主题:

创建一个自定义模块,并复制和粘贴以下内容:

<?PHP
 / **
 * hook_init的执行情况()。
 * / 
函数mymodule_init () { 
  全球$ custom_theme $的用户
  如果 in_array “我的特殊作用” $用户- > 角色)) { 
    $ custom_theme =  'mytheme的' ; 
  } 
} 
?>

你必须更换:

MyModule的 =>你的模块名

我的特殊作用=>与你的用户需要有才能让他们看到不同的主题角色的名称。

mytheme的 =>与您要切换到的主题的名称

 




来自 http://drupal.stackexchange.com/questions/812/how-do-i-change-a-theme-based-on-the-url/816#816
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a Drupal instance running on example.org. Which has a theme X installed and activated. Now for a limited period an event is supposed to be held on the site for which a special section has been "carved out." All the things related to that event will go to example.org/event2011.

I want to have a different theme to be shown whenever a page under this section is viewed (e.g. http://example.org/event2011/about, http://example.org/event2011/node/123). How do I do that?

I have gone through many modules, but none of them is supporting Drupal 7. Preferably, I would like it to be done using modules and don't want to change anything my self at the code level.

shareimprove this question
 
1 
Are you looking to have a whole separate site under the path /event2011/node/123 looks like you are? –  Jeremy French Mar 16 '11 at 12:49
add comment

A custom solution like the other comment suggests is probably best, but if you really want to use a module, your best bet is ThemeKey. As of 5/23/11, it has a stable release.

shareimprove this answer
 add comment

Considering that the change is for a limited period of time, and that implementing such code is not difficult (I am not talking of changing the core code, which is not a good idea to do), I would suggest to implement a custom module that changes the theme being used for specific pages.

It is enough to implement hook_custom_theme() as follows:

function mymodule_custom_theme() {
  if (arg(0) == 'event2011') {
    return 'the theme to use for that page';
  }
}

If you would need to change the theme only for http://example.com/event2011, but not forhttp://example.com/event2011/node/123, then the code should be changed to

function mymodule_custom_theme() {
  if (arg(0) == 'event2011' && !arg(1)) {
    return 'the theme to use for that page';
  }
}

As for using the theme callbacks in the definition of a menu callback, the documentationsays:

As a general rule, the use of theme callback functions should be limited to pages whose functionality is very closely tied to a particular theme, since they can only be overridden by modules which specifically target those pages in hook_menu_alter(). Modules implementing more generic theme switching functionality (for example, a module which allows the theme to be set dynamically based on the current user's role) should use hook_custom_theme()instead.

shareimprove this answer
 add comment

Alternatively you can use the new theme callback option of the menu systemhook_menu_alter() as outlined below. p.s Checkout hook_menu() for more details on thetheme callback

<?php
/**
* Implements hook_menu_alter().
*/
function mymodule_menu_alter(&$items) {
  // Set the theme callback function for all node pages. As per the
  // standard behavior for hook_menu() properties, this will be
  // inherited by all paths underneath node/%node as well, unless
  // they define their own theme callback.
  $items['node/%node']['theme callback'] = 'mymodule_default_node_theme';

  // Set a different theme callback for node edit pages, and pass
  // along the node object to this function so we can make decisions
  // based on it.
  $items['node/%node/edit']['theme callback'] = 'mymodule_edit_node_theme';
  $items['node/%node/edit']['theme arguments'] = array(1);
}
/**
* Defaults to using the 'some_theme' theme for node pages.
*/
function mymodule_default_node_theme() {
  return 'some_theme';
}

/**
* For editing page nodes, uses the 'some_other_theme' theme.
*/
function mymodule_edit_node_theme($node) {
  return $node->type == 'page' ? 'some_other_theme' : mymodule_default_node_theme();
}
?>

Additionally there is also an example of using the more traditional hook_custom_theme()

<?php 
/**
* Implements hook_custom_theme().
*/
function mymodule_custom_theme() {
  // check path using arg(0)
  // check $user
  // do whatever special checking you want and simply return theme key (name of theme folder most of the time)
    return 'special_theme';
  }
}
?>

Taken from: http://drupal.org/node/224333#custom_theme

shareimprove this answer
 add comment

You can simply use Context and use the theme to get applied when the path is /event2011/ you can simple set the path in condition of cotext and change the theme in the action of the content. This was you can very easily switch between themes on a site based on URL. Even works for mobiles ;)

shareimprove this answer
 
  
the theme action passes custom variables to the theme, it doesn't actually change it –  AlexMay 6 '11 at 19:55
add comment

How to switch theme depending on user role:

Create a custom module and copy & paste the following:

<?php
/**
 * Implementation of hook_init().
 */
function mymodule_init() {
  global $custom_theme, $user;
  if (in_array('my special role', $user->roles)) {
    $custom_theme = 'mytheme';
  }
}
?>

You have to replace:

mymodule => with your module name

my special role => with the name of the role that your users will need to have in order for them to see a different theme.

mytheme => with the name of the theme that you want to switch to

shareimprove this answer
 add comment

Your Answer

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
普通分类: