Drupal: how to pass variable from node to block? - drupal

I'd like to do in
do some processing of node fields in node.tpl.php
save it to a variable
and display in a block that is loaded after the node (on the same page).
How to pass a variable from a node to a block?

Since you're running Drupal 7, you have access to the new hook_page_alter() function, from either a custom module or theme. By implementing this, you can easily move parts of the main content area (eg, the node), into any number of different blocks. You'll want to use the show(), hide() and render() functions to properly hide content.
Alternatively, using show() and hide() from within your node.tpl.php template should properly hide the elements, and then you could grab the node object from your block using the menu_get_object() function.
There is normally a video available on hook_page_alter() here, but the site is down at the moment.

do some processing of node fields in
node.tpl.php
A purist would say that you shouldn't be doing it there. hook_node_load() can be used for processing of fields.
Then you can just use menu_get_item() in your block to get the $node object and access any values you have added to it.

Related

How do you create pages programmatically with drupal?

I currently have a Drupal website. I need to make 1000 pages that are in the form domain.com/names/name1.html domain.com/names/name2.html etc. Each differs slightly but is in the same format. What is the best way to go around this? I am having trouble finding any tutorials on this topic.
Let's first clarify.
As the comments under your question suggested you could 1. create nodes programmatically. Let's say you have a content type (a node type) "Page" and then by calling hook_install (to perform the task upon module installation) or hook_update_N (to perform the task upon running the update routine) from within a custom module you could create a thousand nodes of type "Page" (in a for-loop or better as a batch process to prevent timeouts), set their path alias and whatever else you need to set, done. There are a tons of tutorials out there already.
And of course you also can 2. create pages programmatically by calling hook_menu from within a custom module and adding a menu item (a route) which accepts wildcards ($items['names/%']). You then can define a callback function ('page callback') to build your page content that will be called when a matching URL gets accessed and you can also pass the wildcards as arguments ('page arguments') to that callback function. The callback function could simply return a render array for example.
Adding a menu item with a callback function to render some custom content via hook_menu normally means you really only control the inner page content dynamically (the main page content region or however it's called in your current theme). Header, main menu, footer etc. simply stay the same. Depending on your needs you also need to build custom links to your custom pages somehow and provide them as custom block or whatever you are planning to do with them.
A good overview of the second approach can be found in the Example module's docs and in detail in the Page Example submodule's docs.
hook_menu example: page_example_menu
the example page callback function: page_example_description
all Example Page functions

Drupal page content

I'm new to drupal and reading through docs, but hoping to get an explanation for something.
I have a page called page--type-home-page.tpl.php.
Part of this page prints render($page['content'])). I want to remove something that is rendered as part of the page content from the page, but don't understand where this comes from and where/how to look.
Thanks!
Assuming you're talking about Drupal-7
Well the $page['content'] contains a string, which is a rendered version of what's injected into the content region of your theme.
By default, the only block in this region is the "Main page content" block that is generated by the Drupal core. Many things can generate this content but it always pass through the menu API. For instance, if you're viewing a node, the URL used is: node/12. The node module declares a menu entry for node/%node, this menu entry contains a callback function that will render whatever the module wants to render. The module, then, may use different strategy to render it's content from a simple function to a complex imbrication of templates.
The key to alter what's in the box, sorry, what's in the $page['content'], is to know what is rendered and to understand how it's rendered.
If it's a node, first you want to look if you can achieve your goal through the display settings of the content type. admin/structure/types/manage/page/display: And this is true for all entities (users, comments, taxonomy term etc.) Because this is the first thing the module of these entities will put together when they'll try to render your content.
If this is not enough to achieve your goal, you can look into the module that renders the path to see if it hasn't a .tpl.php. You'll be able to re-use it in your theme. You'll want to copy/paste the file in your theme and edit it.
If the module do not have a tpl file to override, try a template suggestion: here's a list from Drupal.org
Ex: node--type.tpl.php
If all this doesn't satisfy your need, you'll have to dig into preprocess functions; Those functions allow you to modify what's in the variables passed to .tpl.php files. That's a little more advanced and I recommend you to read this previous stackoverflow question/answer
Simply, don't use that $page['content'] which prints all content, but place your custom template code instead and print separate field values where you need them like:
<?php print render($content['your_field_name']); ?>
https://drupal.stackexchange.com/questions/30063/how-to-print-fields-in-node-tpl-php
If you want to do just simple styling, like excluding some field you can use content type display options like mgadrat explained, but if you want to use some complex styling, with totally custom html this solution is easier.

Drupal Lightbox issue (not jQuery lightbox)

I am having a issue in Flag_list module, can anyone please help me out how to put a flag list and how to view it please?
The module has very good description how to do it : http://drupal.org/project/flag_lists
How to use:
Install as usual.
At admin/build/flags you will see a flag "fl_template." This is a default template flag provided by flag_lists. You can edit it to make changes to list messages and to assign it to node types. This
template will be used only on types you assign.
You can create new templates here: admin/build/flags/lists/template and apply them to other node types.
A template can be assiged to many node types, but a node type can only have one template assigned to it.
Enable the "list" block so that it is displayed on your content. The block will only be visible if the content is listable and the user has permission to create lists. That is, the node type must be assigned a template, or the block won't appear. Further, you should enable the link to create lists in the block's config.
Create new lists using the link in the block.
See your lists and listed content at user/[uid]/flags/lists

Drupal: How to theme view

i have row style set to NODE in my view.
I need to access "title" field of every node and modify it's, say, color and print it.
Then I need to print content.
How do I loop through the nodes and access fields inside each node? I just do not know how to count total number of nodes, and how to access each field in the node.
Thanks!
Instead of trying to loop through the nodes, you are almost certainly better off switching to a Row style of Fields and using (or overriding) the the per field templates that views provides.
That said, if all you really need to do is color a field, you can do that in CSS with the existing classes that Views adds by default.
You can edit directly the node-NODETYPE.tpl.php, adding your html before and after the title.
Your javascript function can then check whether the node is being printed within the appropriate view or not.

Creating a view for a node

I'm having some problems creating a view for a node within my website.
The content has a path of 'content/%' where % is the title of the node.
What I tried to do is to create a page view in the Views2 module, but to no avail.
When I attempt to preview with the name of one of my nodes, nothing appears, and when I visit the node live, it renders text to the screen without any formatting.
Here are some of the settings from my View...please let me know if I should provide any extra information!
Page Settings
Path: content/%
Arguments
Node:Title
Title: %1
Action to take if argument is not present: Provide default argument
Default argument type: Node ID from URL
Edit
I will describe the behavior I'm expecting to see...
I have a list of nodes in my website in a view....that shows up without any issue.
What I would like to do is click on the node and go to that node's page, which is basically an expanded view of a single item from my list.
I thought what I could do is clone my list view and then add an argument to trim the scope from a list to a single item, but this isn't working too well. What is the proper method for styling a single node?
Edit:
Based on your edit, you've already used Views to create a list of nodes, and now you want to be able to click on one and get to the node page.
If you just want the default node output, you shouldn't have to do anything else. Don't try to use a View to override default node rendering; Drupal will print it out by default.
If you want to theme your node uniquely (beyond what moving the fields around and changing display settings in the UI allows you to do), then you should look at custom template files and theming. Resource here: http://drupal.org/theme-guide
You can change the default node.tpl.php file if you want to change how all nodes are displayed, or you can copy that file and rename it to node-name_of_custom_type.tpl.php to uniquely theme a custom content type you've created, but I'd suggest reading up on the Drupal theme system before making those changes.
End of edit
When you want to assign a path to a Views Page display, you shouldn't model it after any existing paths (e.g. node paths, as you mention). It should be something unique to avoid path alias conflicts. So for example:
my-path/%
Now, if you're just looking to embed a view inside of your node (and you want to pass along the node id as the argument), you're half-way there. Your Arguments setting, based on your description, is correct. However, you don't need a page display.
Here are a couple of options:
Quick 'n' Dirty: Create a block display (which doesn't need a path) and stick the block in a region on the node page (I've used the content_bottom region) to "embed" it in your node
Powerful, Flexible: Use the views_embed_view function (api docs here) inside your node (the body or a CCK field, just make sure to use the "PHP filter" input format)
If you use views_embed_view, you have the flexibility of sticking your Views results wherever you like, and you don't even need to create a Views page or block display, because that function can just use the "Default" display.
Note: If you export your View and save the export code on a site like pastebin or drupalbin, folks here can import the View and see it directly to help answer your question in full detail.
Okay, got it. :)
Using Node: Title as an argument is pretty tricky: with the default settings, the argument must match the title exactly. So, if you wanted to match on "Test value", your path would need to look like content/Test%20value (note the caps).
This will get you the node you want. You said you have default argument set to Node ID from URL; that really doesn't apply for Node: Title arguments. What it does is attempt to find a numeric Node ID and use that as the argument, which will never validate on a Node: Title unless the node's title happens to match the node's ID.
If you just want to display all the nodes if there isn't a title argument present, select Display all values as the action to take if an argument is not present. If you want to display all nodes if the title argument is invalid (like a typo or something), make sure Display all values is set in the Validator field group for the action to take if the argument does not validate.
Because of the exact match restriction on node titles, it's generally not a good idea to use Node: Title as the argument. Instead, consider using "content/NID", where NID is the Node ID of the row's node, as the link in the original view Then, instead of the Node: Title argument in your view, use the Node: ID argument. This should make it less error-prone.

Categories

Resources