Figuring out the parent link in html.tpl.php [clos

2019-09-16 10:26发布

问题:

Is there a way to figure out what the title of the parent page is in html.tpl.php? I'm asking because I change my background image depending on what parent link was chosen on my page. Hope it makes sense, the url is: http://quaaoutlodge.com/drupal-7.14/

Thanks, Ron


Didn't figure out how this can be done... had to revise my architecture and take the required steps to accomplish what I wanted to do...

回答1:

PHP is a server-side programming language. When a user requests a page, the server executes the PHP for that page and sends the resulting HTML to the user. PHP communication only happens in this way. The user only ever sees the resulting HTML. If the user needs to interact with PHP (like changing the page output), the user has to initiate another page request.

Javascript is a client-side programming language. In a normal server environment, Javascript execution only occurs on the user's machine (not on the server). Javascript execution does not occur during PHP execution, so you can't mix Javascript code in with PHP code. Rab's suggestion to use Javascript doesn't answer your question, and your reply to him was attempting to mix Javascript with PHP.

In Drupal terms, you have a collection of nodes on your site accessed through an organized menu. You want to change a specific image on the page (I'm going to assume the Quaaout Lodge logo) whenever the user is viewing a child page of a specific first-level menu item. E.g., viewing "Our History" or "Getting Here" would load (instead of the default Quaaout Lodge logo) a different image connected in some way to the parent item of "The Lodge".

One of the easiest solutions since it seems like you aren't very familiar with Drupal would be to specify the image as a background-image property in CSS instead of as an img element. Each page should have a unique class attribute applied to the body element of its page. E.g., on the "Our History" page, one of the classes applied to the body element is page-node-7. page-node-7 is not applied to any other page in Drupal. You can quickly look up each page's unique class and add CSS properties to change the background-image for each one, etc. If you want to use this method but make it a bit cleaner, you might be able to improve it by using the Node Class module to apply custom classes to each page. You could add a "lodge-image" class to each page that you create under the "The Lodge" menu item. This would let content editors change the image for specific pages without anyone needing to directly modify the CSS. I'm assuming (I might be wrong) that the Node Class module in Drupal7 would add a class to the body element as well as the node's HTML container.

Alternatively, a better solution would be to use a Drupal module to select the logo based on certain menu or path conditions. These will only work if the image is set using the logo configuration options in Drupal (in the Appearance settings). The most appropriate module would probably be Logo Tool, but it has not yet been developed for Drupal7, so it's not yet an option. The Subsites module has a Drupal7 release in development. Dev releases aren't typically recommended for use on production sites, but it seems like it's worth a shot.

Some other methods that would be better but would be more difficult are: override/set the logo in a template.php preprocess function, write a new module to either set the logo or create a custom image block (and add a new region to your theme in which to display the block), convert Logo Tool to Drupal7 yourself, write a new module to automatically add a class attribute to the body element for the page's menu ancestors (CSS method without additional action needed beyond creating the menu entry), and a few more I can think of.

So you have a lot of options.