Drupal Tutorials

Creating dynamic blocks using CCK, Node reference and Views in Drupal

Knowledge of creating content types with CCK as well as creating Views is essential.

This tutorial guides you through setting up a CCK node reference widgets with Views to show a related content blocks on node view.
Related content blocks can be a great way to get people to explore your site further if they land on content from a search engine.

Big thanks to elandirayan for writing this tutorial for DrupalSN, thanks elandirayan!

Hi, this is Elan, I am writing this tutorial to share my knowledge with you all guys. I hope that this tutorial will be useful for some of the folks.

Please go through it and if you find good or bad leave a comment. So that I will enhance in my next tutorial.

CCK

For Implementation of the dynamic block in the Drupal pages(nodes) three modules are required.

  1. CCK
  2. Views
  3. Node Reference (part of the CCK module bundle)

Here are the step by step implementation of the Dynamic block.

Step 1: Enable the CCK and the View Module. In the CCK enable Node Reference also.

Step 2: The Node Reference Will play the vital role in the Implementation

Step 3: Once enable process gets complete, Add a new content type via content management for our tutorial I am naming it as “New”.

Step 4: Once the content type is added click on the Manage fields.

Step 5: Once you click you will get the options to add the new field which is shown in below image.

CCK Node reference setup

As shown in the above mentioned image choose the “Node Reference”. Once the Node Reference choosed, the next select box is enabled in that choose the “Autocomplete Text Field”.

CCK Node reference configuration

Step 6: Click on save. will show the config page for the “ Autocomplete Text Field” which is shown in below image.

In this page, Under global Settings, Enable Require. Then choose the number of values(which is the number of pages where our block have to be displayed. If same block have to be display in more that one page,this will be useful. Better choose the “Unlimited” option).

Enable the content types where our block have to be displayed.

Step 7: click field settings.

Step8: Our configuration settings are Done. Click on the Create content choose our content type learn

Step 9: Input title and the body.( The body is going to display as the block content for our blocks. If it’s not an text we can use image, videos, etc.. as block content.).

Step 10: The node reference added one new field.

Step 11: for that Reference field (Node reference), Start to type the Title of the page where our block have to be get displayed Which is Shown in below image.

Node reference field

Please make a note of it in the above image, I just typed p, it is showing he title of the pages starts with p. Simple choose the title for the list.

By default it will show two text box to add the reference, if you need to add more, click add another item will add one more text box.

Once it is done click on save.

Views

The node is created using cck which we need to display it as block in the pages.

Step 12: Click views under site building.

Step 13: Add new views.

Step 14: Fill the Basic things and add a block in the views.

Step 15: Configure the views for the content type.

For Reference regarding the configuration , refer the Below image.

Views setup

Simple configuration only. For the Arguments, Choose content. It will show our reference filed which we added. Choose that Refernce field. Then configure that reference field.

In the  reference field, Leave title, Breadcrumb  as blank.  In the next “Action to take if argument is not present: “ choose ‘Provide default argument ‘. in that choose ‘ Node ID from URL ‘ At the bottom enable ‘Allow multiple terms per argument. ‘ check box.

The argument will pass the node ID where the block have to display. If ID is not passed, the block content will be empty.

As per the Drupal standard, Drupal won’t display the blocks, if block content is empty.

Block Configuration

Navigate to the block admin page (Administer > Site Building > Blocks) and enable the block we just created in our View above.

We don’t need to set an visibility options because (as mentioned above) the View only works on node view and will only display if there are related nodes selected in our node reference field.

The Result

Browse back to the node you created in steps 8 – 11, and there you have your node reference block, in our example it’s in the right column called "demo block". This block outputs the body of the referenced node as we select "body" as our View "field". If you wanted to output a link to the referenced node you could go back into your View and change the field to "node:title" and select the "Link this field to its node" option in the field settings.

The result

The “Related Tutorials” block right below is powered in this way.

Commenting on this Tutorial is closed.

Categories:

Comments (6)

I used this tutorial to try and set up Author profiles in blocks on Articles pages. But the block is empty. – The Articles content type has a nodereference field which references an Author. – The view filters by Node Type: Article, the argument is the nodereference field, and the fields are Author title, Author photo, etc. – The block is configured correctly in Blocks Admin, because if I do not use arguments in the view, but instead use a relationship, I can get it to display all available authors (which is useless also, but at least it tells me the block is configured correctly)
Can you tell me what I’m doing wrong?

Followup: I got it to work by
1. adding a Relationship using the noderef field (not required)
2. instead of a noderef argument, using a Node ID argument, with these settings: no relationship, Provide default argument:Node ID from URL, and Allow multiple terms per argument.

to note: it does not preview correctly (at all) in Views. Need to save the View and try it on an actual page.

I’d just like to say thank you for writing this straight forward tutorial. This is the first tutorial that I have came across that makes perfect sense and allowed me to get started!
Pariuri Sportive

Adodis Technology Private Ltd's picture

hi,

Thanks to Elan for sharing this great tutorial and it will be more useful in finding the applications of views and cck in creating the dynamic blocks.

http://www.drupal-web-developers.com
http://www.module-node.com

Followup: I got it to work by
1. adding a Relationship using the noderef field (not required)
2. instead of a noderef argument, using a Node ID argument, with these settings: no relationship, Provide default argument:Node ID from URL, and Allow multiple terms per argument.

to note: it does not preview correctly (at all) in Views. Need to save the View and try it on an actual page.” – Looks great! horde leveling guide

Insfilo's picture

cool. thanks for sharing this!
I’m new here, I notice the tutorials section is rarely updated. what a pity.
Indonesia Drupal Design