Porting Drupal modules from D7 to D8.

There are hell lot of resources available on web which will help you porting from D7 to D8. There are lot of changes in Drupal 8. Drupal 8 has borrowed some components from symfony. Read following articles about drupal and symfony.

1. http://drupalwatchdog.com/volume-4/issue-1/something-borrowed-something-drupal

2. https://www.acquia.com/resources/podcasts/acquia-podcast-132-drupal-symfony-what-open-source-is-about

I suppose porting module will be a great way to understand drupal 8. So i tried to porting  a module name username_check to drupal 8. Here is the github repo for D8 module and the presentation i created to record all the changes involved in porting the module to D8.

Here are the few resources which i collected and few which i created.which will help you to learn porting.

1. https://www.drupal.org/update/modules/7/8

2. https://drupalize.me/blog/201412/adventures-porting-d7-form-module-drupal-8

3. http://drupalwatchdog.com/volume-4/issue-1/upgrading-your-modules

There are more available on web. Since D8 is still in beta  so you can’t just stick to these blogpost or article, you need to keep exploring.

How to make a form element required with states ?

So the problem is when you try to make a field required using ‘states’ , it is not getting validated because everything happens in the browser (client side), nothing get validated on server side. So if you try this code.

<code>

‘required’ => array(
‘:input[name=”save_settings”]’ => array(‘checked’ => TRUE),
),

</code>

This will add the red asterisk to the form element but it will not get validated. So the solution is

You need to add custom validation in hook_form_validate function.

Reference : https://api.drupal.org/api/examples/form_example%21form_example_states.inc/function/form_example_states_form/7

https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_process_states/7#comment-16909

Schedule your future tweets.

http://dev-scheduleatweets.pantheonsite.io/  This application schedule tweet and post on your behalf. This web application is made with Drupal. There are few application which already exist with this feature, but this must be the first one made with drupal 😉 . This small application does only one thing schedule your future tweet and keep them in queue, cron runs every minute to check if there is any tweet in the queue. If yes it simply post in on your behalf when you are busy enjoying new year or birthday or taking nap :). Here is one screenshot to show how things work :
tweet
This is the page in action, this page where you schedule your post, as you can see Contains only two field one for your tweet and other one for the time in future. That’s it !!!

You can also view your “tweets in the queue” and “tweets which are already delivered”. Then there is a page called “Page 51” where resided everything else ;). I also don’t know why the page is called page 51 😀 . And yes one more thing, it just took two days to build it and deploy , power of drupal 🙂 .

So if you have any queries or feedback, please comment below (if its a postive feedback) or mail me (if its a negative one ) . hahahah 😀

Display Flag count

<?php
/**
* Implements hook_node_view_alter()
*/
function flag_alter_node_view_alter(&$build) {
//print_r($build[‘links’][‘flag’][‘#links’][‘flag-like’][‘title’]);
if (isset($build[‘links’][‘flag’][‘#links’][‘flag-like’])) {
//find out the number of bookmarks for this node
$node_id = $build[‘body’][‘#object’]->nid;
//todo
// fid should not be hard coded
$results = db_query(“SELECT count FROM {flag_counts} WHERE content_type =’node’ AND content_id = :nid AND fid = ‘3’”, array(‘:nid’=>$node_id));
foreach ($results as $record) {
$result = $record->count;
}
if (empty($result)) {
$result = 0;
}
$build[‘links’][‘flag’][‘#links’][‘flag-like’][‘title’] .= “($result)”;
}
}

In the reference example we have D6 code.

 

Add custom breadcrum

I have to add a custom breadcrumb to my twitter page (Share >> Members >> User >> Recent tweets) . for this we need to add a the following code in templete.php. We will user your_theme_preprocess_breadcrumb() function.

function your_theme_preprocess_breadcrumb(&$variables) {
// Replace the default search api breadcrumb.
foreach ($variables[‘breadcrumb’] as $key => $breadcrumb) {
$strip_breadcrumb = strip_tags($breadcrumb);
if ($strip_breadcrumb == ‘[all items]’) {
$variables[‘breadcrumb’][$key] = t(‘All Content’);
}
}
if ((arg(0) == “user”) && (is_numeric(arg(1)))) {
$user = profile2_load_by_user(arg(1));
$user_name = $user[‘main’]->field_ekn_name[‘und’][0][‘value’];
$user_uid = $user[‘main’]->uid;
//echo “”; print_r($user_uid);
//echo “”; print_r($user);die();
$variables[‘breadcrumb’][1] = l(‘Share’, ‘share’);
$variables[‘breadcrumb’][2] = l(‘Members’, ‘share/members’);
$variables[‘breadcrumb’][3] = t($user_name);
if ((arg(0) == “user”) && (is_numeric(arg(1))) && arg(2) == ‘tweets’) {
$variables[‘breadcrumb’][3] = l(t($user_name),’share/members/’ .$user_uid);
$variables[‘breadcrumb’][4] = t(‘Recent Tweets’);
}
}
}

Run hook_cron on submit

In twitter module imported status only get updated on cron run. So whenever a account is added in the twitter block nothing is displayed. So i run hook_cron on submit, i just added the hook_cron in custom submit handler as in last post.

function twitter_account_form_submit_custom($form, &$form_state) {
module_load_include('lib.php', 'twitter','twitter');
module_load_include('inc', 'twitter','twitter');
$name = ($form_state['profiles']['main']->field_twitter_handle['und'][0]['value']);
$twitter = new Twitter($name);
try {
$account = $twitter->users_show($name, FALSE);
} catch (TwitterException $e) {
form_set_error('screen_name', t('Request failed: @message.', array('@message' => $e->getMessage())));
return;
}
$uid= $form_state['values']['uid'];
$result = db_select('twitter_account' , 't')
->fields('t')
->condition('uid',$uid, '=')
->execute()
->fetchAssoc();
if($result){
db_delete('twitter_account')
->condition('uid', $uid, '=')
->execute();
}
twitter_account_save($account, TRUE, user_load($form_state['values']['uid']));
twitter_cron();
}

Add a extra submit handler to the hook_form_alter

In my case i have to add a twitter account to the user profile, but by default it comes in a tab. So i have to add a field( for twitter handler) in the user profile so that on submit that twitter handler can be added to the user profile.
i created a custom function for submitting the form, this is mainly a overriding the default submit handler by just changing the function name.

function twitter_account_form_submit_custom($form, &$form_state) {
module_load_include('lib.php', 'twitter','twitter');
module_load_include('inc', 'twitter','twitter');
$name = ($form_state['profiles']['main']->field_twitter_handle['und'][0]['value']);
$twitter = new Twitter($name);
try {
$account = $twitter->users_show($name, FALSE);
} catch (TwitterException $e) {
form_set_error('screen_name', t('Request failed: @message.', array('@message' => $e->getMessage())));
return;
}
$uid= $form_state['values']['uid'];
$result = db_select('twitter_account' , 't')
->fields('t')
->condition('uid',$uid, '=')
->execute()
->fetchAssoc();
if($result){
db_delete('twitter_account')
->condition('uid', $uid, '=')
->execute();
}
twitter_account_save($account, TRUE, user_load($form_state['values']['uid']));
}

Now i have to add submit handler to the hook_form_alter. i just need to add a extra submit handler the $form[#sumit][]= ‘ ‘; .

function hook_form_alter {
if($form_id == 'user_profile_form')
{
$form['#submit'][] = 'ekn_user_profile_form_submit';
$form['#submit'][] = 'twitter_account_form_submit_custom';
}
}

Reset password using drush

This commands will be help you to reset the admin password of your site.

# Provide you a login link
drush uli
# Set the password for any user
drush upwd admin –password=”new password”

One more way :

cd php scripts/password-hash.sh 'drupal'

This will generate a hash for you, copy and paste in below queries.

drush sql-cli
update users set name='admin', pass='Hash generated from above command' where uid=1;
quit

Install Drupal From Command Line

If you are using drupal, you must have heard of drush, if not check it now, its the one of the awesomest thing in drupal.More links

1. Project page on drupal.

2. Documentation : how to install

Now i suppose you have drush installed on your system. So you can install drupal in three easy steps.

1. Drush dl drupal

2. mv drupal-7.x your_site_name && cd your_site_name

3.drush si --db-url=mysql://mysql_username:password@127.0.0.1/db_name --account-name=you_choose --account-pass=you_choose --account-email=your_email --locale=pt_br --site-name="Drupal Test"

Overriding the breadcrumb

An easy way to Override the breadcrumb to use hook_breadcrumb in templete.php. In my profile page the user profile name was missing only “home” was getting displayed. i used to following code in templete.php :
function YOUR_THEME_NAME_breadcrumb($variables) {
$breadcrumb = $variables['breadcrumb'];
if(arg(0) == "user") {
$breadcrumb[]= drupal_get_title();
$output = '

‘;
return $output;
}
}
One more way is : create a breadcrumb.tpl.php file in your theme folder and add following line:
This will only change the default symbol( >>) between breadcrumb string to !.