Drupal is a flexible, open-source content management system (CMS) for building complex websites and applications.

Render block and views block drupal 7 programmatically

his is article is for Drupal 7 to render block in templates and custom blocks using php codes

Actually there is very simple ways to render block pro-grammatically 

1) Module Invoke

$block = module_invoke('module_name', 'block_view', 'delta or machine name of block');
print render($block['content']);

Module invoke is most common way to render a block, above example is self explained or see the below example

Drupal 8: check user is logged in or not in *.html.twig

In the new era the drupal8 twig is introduce, so old basic php drupal hook is not useful in theme templates. Now twig is power under the template and if you want to know the user is logged in or Not you can use the following method in your template like node.html.twig or html.html.twig or page.html.twig etc 

{% if logged_in %}
 !!!! do you stuff !!!
{% else %}
 !!!! do you stuff !!!
{% endif %}

Its Just a simple thing helps to add script, or your static html or dynamic coding 

Happy Coding, Have Fun

How to render i18n translated block in drupal 7 Templates

Hey guys, 

Sometimes we created the themes template in drupal 7 and we need to render custom blocks and views block to display proper diversion, but I face a problem to render block in multilingual site it show the same in both languages.

So I found the solution, hopes it helps to other also 

$block_id = 2;
$block = block_block_view($block_id);
$block['content'] = i18n_string(array('blocks', 'block', $block_id, 'body'), $block['content']);
print render($block['content']);

Just need to update the $block id with yours.

send Variable to Js File From PHP in drupal Custom Module

If we need to use the php variable in our js file we can use Core Drupal.settings Functionality :

 example:

Let we assume we want to use at the page load so, add this is hook_init():

function mymodule_init(){

$token_value = "this is value of your variable";

drupal_add_js(array('mymodule' => array('token' => $token_value)), array('type' => 'setting'));

}

Now get the in your js file, but make sure your js file also preloaded to website via theme.info or any other way ...

Render a drupal node d7

If you want to render a node in drupal 7 it is pretty easy, you just a node id or nid of node for doing this:

simply

<?php 
$nid = 13; //use your node nid which you want to render
$nodeview = node_view(node_load($nid));
print drupal_render($nodeview);
?>

You can use this any template like node,page, block, region or field etc.

Drupal 8 add custom menu classes to using menu.html.twig

In Drupal 8 Twig templates give extra power to drupal to add dynamic content in twig style, In this tutorial we can see how we can add the custom menu classes to menu ul li using menu.html.twig.

If you are creating your own theme or custom theme in drupal 8 then to overwrite the menu class we need to override the menu.html.twig template to our theme directory, so copy menu.html.twig form drupal core template to our theme directory

Core template path: [drupal_root]/core/modules/system/templates/menu.html.twig

Subscribe to Drupal