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

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

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.

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

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *