How to render content outside of drupal instance - drupal-7

i created one website using drupal. In Drupal i crated content pages in that instance using wisywig editor. now my intention is using this content block outside of drupal. can you anyone please help me how to use this content block outside of drupal (with code or something else).
i mean how to render content outside of drupal instance

Rendering blocks and nodes is straight-forward.
Before you can do either you must initiate the drupal core:
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Using getcwd() assumes you are placing the script in drupal's root.
To display blocks, use block_load():
$blocks[] = block_load('block',$delta); //first block to display
$blocks[] = block_load('block',$delta2); //second block to display
print drupal_render(_block_get_renderable_array(_block_render_blocks($blocks)));
For nodes, there are several approaches. node_view(node_load()) seems to be the cleanest:
print drupal_render(node_view(node_load($nodeId)));

Related

Drupal 7 alter node display

I have a content type with two image fields, banner and logo.
I am trying to implement logic which will allow one of the two to display depending on whether an editor elected to display just the banner or just the logo from radio button options.
I setup a small custom module implementing hook_node_view and tried to unset the image field from the node object but no joy. Code fragment below:
function mymodule_node_view($node, $view_mode, $langcode){
unset($node->field_main_picture[$node->language][0]);
unset($node->field_main_picture);
$node->field_main_picture = null;
}
None of those attempts worked.
I found the answer to my question.
The node object contains an array called content which is the renderable data Drupal will print to screen.
It's in that array that my unsets need to take place. I.E:
unset($node->content['field_main_picture']);
And the main picture image disappears.
I hardly suggest you to work with Devel module when you are programming like that. It allows you to display variables in your page and visualize the tree. For example, you can call dpm($node) function in your hook_node_view() to see what's in $node and how to access it.

Drupal 7 : How to show specific block in a particular view

I want to display particular block on view page. I have added list to show specific view in a particular block as <viewpagename>, but it is not working. How can I show a particular block on a particular view page in Drupal 7?
Long time I didn't touch Drupal, however, if I remember right, after you choose a path for your view, you can add that path to the settings of the block.
so if the view is available on /view-page, add to the block setting "view-page" in the "include" section.
There is a block_views module made specifically for this reason I believe. It's in beta but works rather nicely for Drupal 7:
https://drupal.org/project/block_views
Cheers,
-cs

Drupal 7 Template Suggestions not working

If I create a region in the info file like:
regions[footer_panel] = Footer panel
then render this in the page template (page.tpl.php):
print render($page['footer_panel']);
then try and create an overide template to work with (as described in documentation):
block--footer_panel.tpl.php
finally print some static text in that file, Im not getting any results. Please could someone advise?
Caches have been flushed and block.tpl is in the templates folder.
Try adding the generic block.tpl.php to the theme as well. Sometimes it won't pick up the block template suggestions if that file isn't present first. Be sure to clear the cache again.
I'm also assuming that you have a block inserted into that region. If you do not have a block designated to show in that region creating a template file will do nothing. You need to create a block and then create the template file based on that blocks name/ID not the region name.

module block view only prints if it is on a certain page

In Drupal 7 Is there a way for me to insert my block into a region only on certain pages inside of a module code? Or do I have to do that in the gui block list?
I've created a banner module, but want to be able to give the ability to choose the pages it appears on. For starts, it could appear only on the front page. I tried a $is_front check, but I am getting an error that $is_front or $variables are undefined.
This doens't work inside of my block_view() function in my module.
if ($is_front){
$block['content'] = theme('mydata', $banner_node_list);
}
I think your best bet is to use the block GUI to select where it appears. I can't see any benefits to doing it in the code when it's already built in to be honest.

Drupal 7 not using the template suggestion

I have added the following to my template.php file, in the [themename]_preprocess_page function:
<?php
if ($variables['is_front'])
{
$variables['theme_hook_suggestions'] = array();
$variables['theme_hook_suggestions'][] = 'page__index';
}
if (isset($variables['node'])) {
// If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
$variables['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $variables['node']->type);
}
If I run a var_dump on the $variables array, I can see that, on my front page, the 'theme_hook_suggestions' is set to only use 'page__index'. I have a file named 'page--index.tpl.php'. Drupal still uses page.tpl.php.
I also commented out the code above, and renamed the file to 'page--front.tpl.php' and it still used page.tpl.php. I clear the caches after every change.
What am I missing?
Edit: To help clarify, I want to override the entire design of the page for the front - no columns or sidebars, different graphics, different backgrounds on some of the div's, etc. I don't want to override the 'node--' template files (yet).
In the end, I need to have a static front page with a different design than the rest of the site, then a custom node template for each content type.
I worked with the awesome folks on the #drupal IRC channel and found the issue. Not sure if this is a bug in the code or on purpose - but you cannot use the word 'index' for those theme suggestions. I changed the content type's name to 'homepage' and voila!

Resources