Drupal 6 - 强制页面视图显示不同的视图?

发布于 2024-12-03 04:49:45 字数 1640 浏览 2 评论 0原文

这是一个疯狂的问题...

我有一个设置为页面的视图显示。它在主题 A(桌面)中看起来很棒,但在主题 B(移动)中看起来很糟糕。因此,我为主题 B 制作了不同版本的视图。由于桌面/移动“站点”相同,只是主题不同,因此无论选择什么主题,该页面的 url 都将相同。

因此,我希望能够将用户指向:

mysite/this_crazy_view

并让返回的页面根据其所在的主题选择正确的视图。我知道,如果我使用块,我只会将适当的块分配给页面在逐个主题的基础上有问题,但由于显示使用页面显示,我不知道正确的方法是什么。

如果我可以帮助的话,我宁愿不将视图重建为块(如果无法帮助,那就这样吧......)所以我希望有某种方法可以通过 tpl.php 文件有条件地加载视图或类似的东西...

我在模块中使用的代码(根据下面 @Clive 的建议)是:

<?php
function bandes_custom_hook_menu() {
  $items['charley_test'] = array(
    'title' => 'Title',
    'access arguments' => array('access content'),
    'page callback' => 'bandes_custom_set_page_view',
    'type' => MENU_NORMAL_ITEM  );
  return $items; 
}

function bandes_custom_set_page_view() {
  global $theme_key;
  $view_name = $theme_key == 'mobile_jquery' ? 'course_views_mobile' : 'course_views';
  $display_id = 'page_5'; 
  return views_embed_view($view_name, $display_id);
}
?>

我已经多次清除缓存并在 $items 数组中尝试了各种不同的路径。 course_views 和 course_views_mobile 绝对可以独立工作。

我还想知道是否可以创建一个views-view--course-views--page-5.tpl.php,其中除了views_embed_view(course_views_mobile, page_5)部分之外几乎不包含任何内容? (仅针对两个主题之一......)

实际上我认为答案比上述所有答案都简单。重定向的事情让我很舒服,所以我删除了模块,将路径重置为我一直在使用的路径,然后尝试了模板/主题方法。

这是:views-view--course-views--page-5.tpl.php,仅在移动主题上使用,但指的是非移动视图(有点让我头疼,但它有效)

<?php
//get the view
print "IM IN YR VUE, MESSING THNGZ UP!"; //yeah, I'm going to remove this part...
$view_name="course_views_mobile";
$display_id="page_5";
print views_embed_view($view_name, $display_id);
?>

任何原因不应该工作吗? (或者为什么这是一个非常糟糕的主意?)

Kind-of a crazy question here...

I have a view display that's set up as a page. It looks great in theme A (desktop), but terrible in theme B (mobile). So I made a different version of the view for theme B. Since the desktop/mobile 'sites' are the same just with different themes, the url for this page will be the same regardless of hte theme selected.

So I would like to be able to point the user to:

mysite/this_crazy_view

and have the returned page select the proper view depending on which theme it's in. I know that if I were using blocks I would just assign the appropriate blocks to the page in question on a theme-by-theme basis, but since the displays are using page display I don't know what the right approach would be.

I would rather not rebuild the views as blocks if I can help it (if it can't be helped, so be it...) so I was hoping there was some way to conditionally load the view via the tpl.php file or something like that...

The code I'm using in my module (per @Clive 's recommendation below) is:

<?php
function bandes_custom_hook_menu() {
  $items['charley_test'] = array(
    'title' => 'Title',
    'access arguments' => array('access content'),
    'page callback' => 'bandes_custom_set_page_view',
    'type' => MENU_NORMAL_ITEM  );
  return $items; 
}

function bandes_custom_set_page_view() {
  global $theme_key;
  $view_name = $theme_key == 'mobile_jquery' ? 'course_views_mobile' : 'course_views';
  $display_id = 'page_5'; 
  return views_embed_view($view_name, $display_id);
}
?>

I've cleared the cache a number of times and tried a variety of different paths in the $items array. The course_views and course_views_mobile both definitely work on their own.

I was also wondering if I could just create a views-view--course-views--page-5.tpl.php which contains almost nothing aside from the views_embed_view(course_views_mobile, page_5) part? (Only on one of the two themes...)

Actually I think the answer was simpler than all of the above. The redirect thing was giving me fits, so I removed the module, reset the paths to what I had been using, and tried the template/theme approach instead.

This is: views-view--course-views--page-5.tpl.php, only used on the mobile theme, but referring to the non-mobile view (kinda gives me a headache, but it works)

<?php
//get the view
print "IM IN YR VUE, MESSING THNGZ UP!"; //yeah, I'm going to remove this part...
$view_name="course_views_mobile";
$display_id="page_5";
print views_embed_view($view_name, $display_id);
?>

Any reason that shouldn't work? (Or why it is a really bad idea?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最偏执的依靠 2024-12-10 04:49:45

又是我:)

实际上我只是想到了一个简单的方法来解决这个问题;如果您可以将视图的 URL 更改为您想要访问它们的路径之外的其他内容(任何路径都可以),您可以实现 hook_menu() 函数在该路径的自定义模块中,根据您的主题进行选择:

function MYMODULE_hook_menu() {
  $items['this_crazy_view'] = array(
    'title' => 'Title',
    'access arguments' => array('access content'),
    'page callback' => 'MYMODULE_crazy_view_page',
    'type' => MENU_CALLBACK // or MENU_NORMAL_ITEM if you want it to appear in menus as usual
  );

  return $items; // Forgot to add this orginally
}

function MYMODULE_crazy_view_page() {
  global $theme_key;

  $view_name = $theme_key == 'foo' ? 'theView' : 'theOtherView';
  $display_id = 'page_1'; // Or whatever the page display is called

  return views_embed_view($view_name, $display_id);
}

应该这样做窍门

Me again :)

I just thought of an easy-ish way around this actually; if you can change the URL of your views to something other than the path you want to access them at (any path would do) you could implement a hook_menu() function in a custom module for that path, to make the choice depending on your theme:

function MYMODULE_hook_menu() {
  $items['this_crazy_view'] = array(
    'title' => 'Title',
    'access arguments' => array('access content'),
    'page callback' => 'MYMODULE_crazy_view_page',
    'type' => MENU_CALLBACK // or MENU_NORMAL_ITEM if you want it to appear in menus as usual
  );

  return $items; // Forgot to add this orginally
}

function MYMODULE_crazy_view_page() {
  global $theme_key;

  $view_name = $theme_key == 'foo' ? 'theView' : 'theOtherView';
  $display_id = 'page_1'; // Or whatever the page display is called

  return views_embed_view($view_name, $display_id);
}

That should do the trick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文