Wednesday, February 22, 2017

Drupal 8 .info.yml

Drupal 8 uses to handle routing, sessions and the services container, amongst other things, is borrowed from Symfony 2 File Name: sixbones.info.yml




name: Six Bones
description: Custom Module
package: Custom

type: module
core: 8.x
       
 

Tuesday, April 5, 2016

Drupal 7 Add Custom Content Type Body Field


<?php
node_types_rebuild();
$types = node_type_get_types();
node_add_body_field($types['content_type_name']);
?>

Use: php filter and run this code or use devel/php

Friday, February 26, 2016

Install PHP SOAP on CentOS 7 in PHP 7

Install "php-soap":
============
sudo yum install php70w-soap

Terminal:
=======
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.digistar.vn
 * epel: epel.mirror.srv.co.ge
 * extras: centos-hn.viettelidc.com.vn
 * nux-dextop: mirror.li.nux.ro
 * updates: mirror.digistar.vn
 * webtatic: sp.repo.webtatic.com
Resolving Dependencies
--> Running transaction check
---> Package php70w-soap.x86_64 0:7.0.3-1.w7 will be installed
--> Processing Dependency: php70w-common(x86-64) = 7.0.3-1.w7 for package: php70w-soap-7.0.3-1.w7.x86_64
--> Running transaction check
---> Package php70w-common.x86_64 0:7.0.2-1.w7 will be updated
--> Processing Dependency: php70w-common(x86-64) = 7.0.2-1.w7 for package: php70w-gd-7.0.2-1.w7.x86_64
--> Processing Dependency: php70w-common(x86-64) = 7.0.2-1.w7 for package: php70w-process-7.0.2-1.w7.x86_64
--> Processing Dependency: php70w-common(x86-64) = 7.0.2-1.w7 for package: php70w-opcache-7.0.2-1.w7.x86_64
--> Processing Dependency: php70w-common(x86-64) = 7.0.2-1.w7 for package: php70w-tidy-7.0.2-1.w7.x86_64
--> Processing Dependency: php70w-common(x86-64) = 7.0.2-1.w7 for package: php70w-pdo-7.0.2-1.w7.x86_64
--> Processing Dependency: php70w-common(x86-64) = 7.0.2-1.w7 for package: php70w-bcmath-7.0.2-1.w7.x86_64
--> Processing Dependency: php70w-common(x86-64) = 7.0.2-1.w7 for package: php70w-xml-7.0.2-1.w7.x86_64
--> Processing Dependency: php70w-common(x86-64) = 7.0.2-1.w7 for package: php70w-mbstring-7.0.2-1.w7.x86_64
--> Processing Dependency: php70w-common(x86-64) = 7.0.2-1.w7 for package: php70w-7.0.2-1.w7.x86_64
--> Processing Dependency: php70w-common(x86-64) = 7.0.2-1.w7 for package: php70w-cli-7.0.2-1.w7.x86_64
---> Package php70w-common.x86_64 0:7.0.3-1.w7 will be an update
--> Running transaction check
---> Package php70w.x86_64 0:7.0.2-1.w7 will be updated
---> Package php70w.x86_64 0:7.0.3-1.w7 will be an update
---> Package php70w-bcmath.x86_64 0:7.0.2-1.w7 will be updated
---> Package php70w-bcmath.x86_64 0:7.0.3-1.w7 will be an update
---> Package php70w-cli.x86_64 0:7.0.2-1.w7 will be updated
---> Package php70w-cli.x86_64 0:7.0.3-1.w7 will be an update
---> Package php70w-gd.x86_64 0:7.0.2-1.w7 will be updated
---> Package php70w-gd.x86_64 0:7.0.3-1.w7 will be an update
---> Package php70w-mbstring.x86_64 0:7.0.2-1.w7 will be updated
---> Package php70w-mbstring.x86_64 0:7.0.3-1.w7 will be an update
---> Package php70w-opcache.x86_64 0:7.0.2-1.w7 will be updated
---> Package php70w-opcache.x86_64 0:7.0.3-1.w7 will be an update
---> Package php70w-pdo.x86_64 0:7.0.2-1.w7 will be updated
---> Package php70w-pdo.x86_64 0:7.0.3-1.w7 will be an update
---> Package php70w-process.x86_64 0:7.0.2-1.w7 will be updated
---> Package php70w-process.x86_64 0:7.0.3-1.w7 will be an update
---> Package php70w-tidy.x86_64 0:7.0.2-1.w7 will be updated
---> Package php70w-tidy.x86_64 0:7.0.3-1.w7 will be an update
---> Package php70w-xml.x86_64 0:7.0.2-1.w7 will be updated
---> Package php70w-xml.x86_64 0:7.0.3-1.w7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch          Version             Repository       Size
================================================================================
Installing:
 php70w-soap            x86_64        7.0.3-1.w7          webtatic        172 k
Updating for dependencies:
 php70w                 x86_64        7.0.3-1.w7          webtatic        2.8 M
 php70w-bcmath          x86_64        7.0.3-1.w7          webtatic         35 k
 php70w-cli             x86_64        7.0.3-1.w7          webtatic        2.8 M
 php70w-common          x86_64        7.0.3-1.w7          webtatic        1.2 M
 php70w-gd              x86_64        7.0.3-1.w7          webtatic        135 k
 php70w-mbstring        x86_64        7.0.3-1.w7          webtatic        540 k
 php70w-opcache         x86_64        7.0.3-1.w7          webtatic        122 k
 php70w-pdo             x86_64        7.0.3-1.w7          webtatic         89 k
 php70w-process         x86_64        7.0.3-1.w7          webtatic         40 k
 php70w-tidy            x86_64        7.0.3-1.w7          webtatic         29 k
 php70w-xml             x86_64        7.0.3-1.w7          webtatic        127 k

Transaction Summary
================================================================================
Install  1 Package
Upgrade             ( 11 Dependent packages)

Total download size: 8.1 M
Is this ok [y/d/N]: y
Downloading packages:
No Presto metadata available for webtatic
(1/12): php70w-bcmath-7.0.3-1.w7.x86_64.rpm                |  35 kB   00:00    
(2/12): php70w-gd-7.0.3-1.w7.x86_64.rpm                    | 135 kB   00:00    
(3/12): php70w-mbstring-7.0.3-1.w7.x86_64.rpm              | 540 kB   00:00    
(4/12): php70w-opcache-7.0.3-1.w7.x86_64.rpm               | 122 kB   00:00    
(5/12): php70w-pdo-7.0.3-1.w7.x86_64.rpm                   |  89 kB   00:00    
(6/12): php70w-process-7.0.3-1.w7.x86_64.rpm               |  40 kB   00:00    
(7/12): php70w-soap-7.0.3-1.w7.x86_64.rpm                  | 172 kB   00:00    
(8/12): php70w-tidy-7.0.3-1.w7.x86_64.rpm                  |  29 kB   00:00    
(9/12): php70w-7.0.3-1.w7.x86_64.rpm                       | 2.8 MB   00:01    
(10/12): php70w-xml-7.0.3-1.w7.x86_64.rpm                  | 127 kB   00:00    
(11/12): php70w-common-7.0.3-1.w7.x86_64.rpm               | 1.2 MB   00:04    
(12/12): php70w-cli-7.0.3-1.w7.x86_64.rpm                  | 2.8 MB   00:06    
--------------------------------------------------------------------------------
Total                                              1.3 MB/s | 8.1 MB  00:06    
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : php70w-common-7.0.3-1.w7.x86_64                             1/23
  Updating   : php70w-cli-7.0.3-1.w7.x86_64                                2/23
  Updating   : php70w-7.0.3-1.w7.x86_64                                    3/23
  Updating   : php70w-gd-7.0.3-1.w7.x86_64                                 4/23
  Updating   : php70w-process-7.0.3-1.w7.x86_64                            5/23
  Updating   : php70w-tidy-7.0.3-1.w7.x86_64                               6/23
  Updating   : php70w-opcache-7.0.3-1.w7.x86_64                            7/23
  Updating   : php70w-bcmath-7.0.3-1.w7.x86_64                             8/23
  Updating   : php70w-xml-7.0.3-1.w7.x86_64                                9/23
  Updating   : php70w-mbstring-7.0.3-1.w7.x86_64                          10/23
  Updating   : php70w-pdo-7.0.3-1.w7.x86_64                               11/23
  Installing : php70w-soap-7.0.3-1.w7.x86_64                              12/23
  Cleanup    : php70w-7.0.2-1.w7.x86_64                                   13/23
  Cleanup    : php70w-cli-7.0.2-1.w7.x86_64                               14/23
  Cleanup    : php70w-pdo-7.0.2-1.w7.x86_64                               15/23
  Cleanup    : php70w-mbstring-7.0.2-1.w7.x86_64                          16/23
  Cleanup    : php70w-xml-7.0.2-1.w7.x86_64                               17/23
  Cleanup    : php70w-bcmath-7.0.2-1.w7.x86_64                            18/23
  Cleanup    : php70w-opcache-7.0.2-1.w7.x86_64                           19/23
  Cleanup    : php70w-tidy-7.0.2-1.w7.x86_64                              20/23
  Cleanup    : php70w-process-7.0.2-1.w7.x86_64                           21/23
  Cleanup    : php70w-gd-7.0.2-1.w7.x86_64                                22/23
  Cleanup    : php70w-common-7.0.2-1.w7.x86_64                            23/23
  Verifying  : php70w-7.0.3-1.w7.x86_64                                    1/23
  Verifying  : php70w-gd-7.0.3-1.w7.x86_64                                 2/23
  Verifying  : php70w-cli-7.0.3-1.w7.x86_64                                3/23
  Verifying  : php70w-process-7.0.3-1.w7.x86_64                            4/23
  Verifying  : php70w-tidy-7.0.3-1.w7.x86_64                               5/23
  Verifying  : php70w-opcache-7.0.3-1.w7.x86_64                            6/23
  Verifying  : php70w-bcmath-7.0.3-1.w7.x86_64                             7/23
  Verifying  : php70w-xml-7.0.3-1.w7.x86_64                                8/23
  Verifying  : php70w-mbstring-7.0.3-1.w7.x86_64                           9/23
  Verifying  : php70w-common-7.0.3-1.w7.x86_64                            10/23
  Verifying  : php70w-pdo-7.0.3-1.w7.x86_64                               11/23
  Verifying  : php70w-soap-7.0.3-1.w7.x86_64                              12/23
  Verifying  : php70w-7.0.2-1.w7.x86_64                                   13/23
  Verifying  : php70w-bcmath-7.0.2-1.w7.x86_64                            14/23
  Verifying  : php70w-mbstring-7.0.2-1.w7.x86_64                          15/23
  Verifying  : php70w-xml-7.0.2-1.w7.x86_64                               16/23
  Verifying  : php70w-common-7.0.2-1.w7.x86_64                            17/23
  Verifying  : php70w-gd-7.0.2-1.w7.x86_64                                18/23
  Verifying  : php70w-pdo-7.0.2-1.w7.x86_64                               19/23
  Verifying  : php70w-cli-7.0.2-1.w7.x86_64                               20/23
  Verifying  : php70w-process-7.0.2-1.w7.x86_64                           21/23
  Verifying  : php70w-tidy-7.0.2-1.w7.x86_64                              22/23
  Verifying  : php70w-opcache-7.0.2-1.w7.x86_64                           23/23

Installed:
  php70w-soap.x86_64 0:7.0.3-1.w7                                              

Dependency Updated:
  php70w.x86_64 0:7.0.3-1.w7             php70w-bcmath.x86_64 0:7.0.3-1.w7     
  php70w-cli.x86_64 0:7.0.3-1.w7         php70w-common.x86_64 0:7.0.3-1.w7     
  php70w-gd.x86_64 0:7.0.3-1.w7          php70w-mbstring.x86_64 0:7.0.3-1.w7   
  php70w-opcache.x86_64 0:7.0.3-1.w7     php70w-pdo.x86_64 0:7.0.3-1.w7        
  php70w-process.x86_64 0:7.0.3-1.w7     php70w-tidy.x86_64 0:7.0.3-1.w7       
  php70w-xml.x86_64 0:7.0.3-1.w7       

Complete!

Restart Apache:
===========
systemctl restart httpd

Check if SOAP enabled or not:
=====================
php -m | grep -i soap
soap




Thursday, January 21, 2016

How to print drupal 7 breadcrumb in custom .tpl files?

Where-ever you can add it.
<?php print theme('breadcrumb', array('breadcrumb'=>drupal_get_breadcrumb())); ?>

Thursday, December 17, 2015

A simple multistep form with a Next and a Back button - Drupal


function hook_menu(){
    $items = array();
    $items['multistep'] = array(
    'title' => 'Multistep',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('multistep'),
    'access callback' => TRUE,
  );
    return $items;
}

//-------------------------------------- Form 1 ----------------------------------------------------

function multistep($form, &$form_state) {

  if (!empty($form_state['page_num']) && $form_state['page_num'] == 2) {
    return multistep_page_two($form, $form_state);
  }
  if (!empty($form_state['page_num']) && $form_state['page_num'] == 3) {
    return multistep_page_three($form, $form_state);
  }
 
  $form_state['page_num'] = 1; 

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#size' => 20,
    '#maxlength' => 20,
    '#required' => TRUE,
    '#default_value' => !empty($form_state['values']['name']) ? $form_state['values']['name'] : '',
  );

  $form['next'] = array(
    '#type' => 'submit',
    '#value' => 'Next >>',
    '#submit' => array('multistep_next_submit'),
    '#validate' => array('multistep_next_validate'),
  );
  return $form;
}


//----------------------------------------- Form 2 -------------------------------------------------

function multistep_page_two($form, &$form_state) {

  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#required' => TRUE,
    '#default_value' => !empty($form_state['values']['email']) ? $form_state['values']['email'] : '',
  );

  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('<< Back'),
    '#submit' => array('multistep_page_two_back'),
    '#limit_validation_errors' => array(),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Next >>'),
    '#submit' => array('multistep_page_two_submit'),
  );
  return $form;
}

//------------------------------------------ Form 3 ------------------------------------------------

function multistep_page_three($form, &$form_state) {
  $form['phone'] = array(
    '#type' => 'textfield',
    '#title' => t('Phone'),
    '#required' => TRUE,
    '#default_value' => !empty($form_state['values']['phone']) ? $form_state['values']['phone'] : '',
  );

  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('<< Back'),
    '#submit' => array('multistep_page_three_back'),
    '#limit_validation_errors' => array(),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#submit' => array('multistep_page_three_submit'),
  );
  return $form;
}

//------------------------------------------ Validate -----------------------------------------------

function multistep_next_validate($form, &$form_state) {
    // validate code here...
}

//------------------------------------------- Back ---------------------------------------------------

function multistep_page_two_back($form, &$form_state) {
  $form_state['values'] = $form_state['page_values'][1];
  $form_state['page_num'] = 1;
  $form_state['rebuild'] = TRUE;
}

function multistep_page_three_back($form, &$form_state) {
  $form_state['values'] = $form_state['page_values'][2];
  $form_state['page_num'] = 2;
  $form_state['rebuild'] = TRUE;
}

//------------------------------------- Submit Handlers ------------------------------------

function multistep_next_submit($form, &$form_state) {
  $form_state['page_values'][1] = $form_state['values'];

  if (!empty($form_state['page_values'][2])) {
    $form_state['values'] = $form_state['page_values'][2];
  }
  $form_state['page_num'] = 2;
  $form_state['rebuild'] = TRUE;
}

function multistep_page_two_submit($form, &$form_state) {
  $form_state['page_values'][2] = $form_state['values'];

  if (!empty($form_state['page_values'][3])) {
    $form_state['values'] = $form_state['page_values'][3];
  }
  $form_state['page_num'] = 3;
  $form_state['rebuild'] = TRUE;
}

function multistep_page_three_submit($form, &$form_state) {
  $form_state['page_values'][3] = $form_state['values'];
  //dsm($form_state['page_values']);
  drupal_set_message(t('The form has been submitted.'));
}


For more information refer Drupal example module: form_example_tutorial_8

Tuesday, December 15, 2015

Add CSS files and JavaScript files at the bottom of a page in Drupal 7

template_preprocess_html() in template.php file. Preprocess variables for html.tpl.php

function themename_preprocess_html(&$variables) {

  // Add external file
  drupal_add_css('https://fonts.googleapis.com/css?family=Roboto:400,300,500,700',array('type' => 'external'));

  // Add Javascript file at the bottom of a page
  drupal_add_js('/sites/all/themes/purple/js/bootstrap.min.js', array('type' => 'file', 'scope' => 'footer', 'weight' => 10));

}

Wednesday, November 25, 2015

How to export with limit the number of records from mysqldump?

mysqldump --opt --where='1 limit 10' -h IP_ADDRESS -u USER_NAME DATABASE_NAME TABLE_NAME > databasetable.sql

Thursday, October 29, 2015

Get node title from nid without using node_load($nid) in Drupal

Get node title without using node_load($nid).

function get_node_title($nid) {
  $node_title = db_select('node', 'n')
      ->fields('n',array('title'))
      ->condition('nid',$nid,'=')
      ->execute()
      ->fetchField();
  return $node_title;
}

How to render menu programatically in Drupal 7?

In template.php file you can add the below code:

$menu = menu_tree_output(menu_tree_all_data('main-menu', null, 1));
$variables['menu'] = drupal_render($menu);

Page.tpl.php (render the main menu tree in the page.tpl.php like this.)
<?php print $menu; ?>

menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL)
[https://api.drupal.org/api/drupal/includes!menu.inc/function/menu_tree_all_data/7]