General Knowledge Base

Bash:
cmd.exe C:\xampp\mysql\bin mysql -u root -p --force test2 < C:\Users\rgopi\Downloads\test.sql


Ajax: 
If you want to do ajax actions on form elements, attach #ajax property in form field definitions.

For example, when you use #ajax in an select field, #ajax automatically adds onChange() event to ajax.

#ajax support following important parameters

#ajax['callback'] => The callback is a function which returns a renderable array which consists of html or ajax commands.
#ajax['wrapper']: The CSS ID of the area to be replaced by the content returned by the #ajax['callback'] function. The content returned from the callback will replace the entire element named by #ajax['wrapper']. The wrapper is usually created using #prefix and #suffix properties in the form.


Ajax in menu callbacks:
If you want to perform ajax actions on menu call backs. Use ajax_deliver.
It packages and sends the result of a page callback as an Ajax response.

$commands[] = ajax_command_html('#myajax_wrapper', "This is some content delivered via a page load with param <b>" . $param . "</b>.");

Drupal debugging:
1. How to find what are all the modules invokes this hook

in module.inc in function module_invloke_all() add this code "if ($hook == 'cron') watchdog('cron', "hit $module cron");"
as given below

$function = $module .'_'. $hook;

if ($hook == 'cron') watchdog('cron', "hit $module cron"); 

$result = call_user_func_array($function, $args);

2. How to find modules that implements this hook
module_implements('hook')
example:
module_implements('form_alter')

3. How to alter the order of the modules implementing this hook

function mymodule_module_implements_alter(&$implements, $hook) {
  if ($hook == 'form_alter') {
    print_r($implements);
  }
}

4. How will i find which modules alters my form
module_implements('form_alter')
Within each module, form alter hooks are called in the following order: first, hook_form_alter(); second, hook_form_BASE_FORM_ID_alter(); third, hook_form_FORM_ID_alter()

5. To target all forms for alteration use node_form
example: mymodule_form_node_form_alter

6. Form-inspect is a module to see form structure and form data.

Drupal Theme:
=====================
1. Theme() function
==================
Theme function Generates themed output.
All requests for themed output must go through this function (however, * calling the theme() function directly is strongly discouraged).
Theme function takes two arguments. theme hook & variables. So the theme() function should be called using theme_hook() function

For example: to theme terms, taxonomy module implement hook_theme with theme_taxonomy_term().



2. Theme.inc has some default theme_hook() implementations like theme_breadcrumb(), theme_image(), theme_link(), theme_more_link(), theme_username() etc.. Modules register theme hooks within a hook_theme() implementation and provide a default implementation via a function named theme_HOOK() (e.g., theme_taxonomy_term()) or via a template file named according to the value of the 'template' key registered with the theme hook.

3. In summary hook_theme() provides registrations for theme hooks.

=================
Overriding
=================

How to override core theme_hook() functions?
In template.php provide function yourthemename_hookname() {//override}. Example: aegonmf_breadcrumb()
How to override core theme hook tpl files?
Copy tpl in your theme folder

=============
Preprocessors
==============
1. If the theme implementation is a template file, several functions are called before the template file is invoked, to modify the $variables array. These fall into the "preprocessing" phase and the "processing" phase, and are executed (if they exist), in the following order.
a) template_preprocess(&$variables, $hook): Creates a default set of variables for all theme hooks with template implementations.
template_preprocess() provides/modifies variables for all theme hooks that have template implementation.
Example excercise: I created a function called mymodule_preprocess() {$variables['sathya'] = 'gopi'}; and i can print $sathya in any tpl file
flow: template_preprocess (theme.inc) => modulename_preprocess (.module) => themename_preprocess(template.php)

b) template_preprocess_HOOK(&$variables): Should be implemented by the module that registers the theme hook, to set up default variables.For example, node module registers template_preprocess_node() to modify variables for node.tpl.php.
template_preprocess_page() modify variables for page.tpl.php
template_preprocess_html() modify variables for html.tpl.php
tempalte_preprocess_maitanance_page() modify variables for maintanance.tpl.php
flow: template_preprocess_HOOK(theme.inc) => modulename_preprocess_hook(.module) => themename_preprocess_hook(template.php)

=================================

Theme hook overriding:
a) If the theme hook implementation is a function then use themename_themehook() in template.php to override.
Order: theme_hook() in theme.inc, enginename_hook in engine.inc, themename_hook in template.php

b) If the theme hook implementation is a template then its variables can be modified using modulename_preprocess_hook, themename_preprocess_hook(template.php)


Git Flow:
Git Flow Summary

Branching
===========

The main trunks that stay around forever are develop and master. master holds your latest release and develop holds your latest "stable" development copy.

Contributors create feature branches (prefixed with feature/ by convention) off of develop :

$ git checkout -b feature/my-feature develop
and hotfix branches (prefixed with hotfix/ by convention) off of master:

# hotfix the latest version of master
$ git checkout -b hotfix/hotfix-version-number master

# or hotfix from a specific version
$ git checkout -b hotfix/hotfix-version-number <starting-tag-name>
These branches are "disposable", meaning they have a short lifespan before they are merged back to the main trunks. They are meant to encapsulate small pieces of functionality.

Finishing Branches
===================
When a contributor is done with a feature branch, they merge it back into develop:

$ git checkout develop
$ git merge --no-ff feature/my-feature
$ git branch -d feature/my-feature
When they're done with a hotfix branch, they merge it back into both master and develop so the hotfix carries forward:

$ git checkout master
$ git merge --no-ff hotfix/hotfix-version-number
$ git checkout develop
$ git merge --no-ff hotfix/hotfix-version-number
$ git branch -d hotfix/hotfix-version-number
This is the continuous integration aspect.

Releases

When you're ready to start packaging up a release, you create a release branch from your "stable" develop branch (same as creating feature branches). You then bump the version number in a tag (described below).

Using separate release branches allows you to continue developing new features on develop while you fix bugs and add finishing touches to the release branch.

When you're ready to finish the release, you merge the release branch into both master and develop (just like a hotfix) so that all your changes carry forward.

Tagging

When you create a release branch or a hotfix branch, you bump the version number appropriately in a tag. With vanilla git, that looks like this:

$ git tag -a <tag-name> -m <tag-description>
You'll then also have to push the tags (separately) to your remote repository:

$ git push --tags
It's usually best to use semantic versioning in which your versions take the form major.minor.hotfix. Major bumps are backwards incompatible, whereas minor and hotfix bumps are not backwards incompatible (unless you're in beta, 0.x.x).

Merging

As you saw above, git-flow encourages you to merge branches with the following command:

$ git merge --no-ff <branch-name>
The --no-ff option allows you to maintain all of your branch history without leaving a bunch of branches lying around in the current commit of the repository (so no worries, you won't have a branch for every version).

You're also encouraged to pull with

$ git pull --rebase
So you don't add lots of useless merge commits.

You can configure git to do both of these things by default in your .gitconfig. I'll let you look that one up though ;)

Browsing versions

When someone is looking for a specific version of your codebase, they can checkout the tag by name:

# checkout in detached HEAD to browse
$ git checkout <tag-name>

# OR checkout and create a new local branch (as you might for a hotfix)
$ git checkout -b <new-branch-name> <tag-name>
Or, if someone is browsing on github, there is also a "tags" tab in the "branches" dropdown.

Using the git-flow extension (recommended)

My favorite way to use this model is with the git flow extension for git.

(Edit: Louis has recommended the AVH fork which works better with git describe and might be more active now. Thanks Louis. )

The extension automates all the messy parts (like using merge --no-ff and deleting branches after merging) so that you can get on with your life.

For example, with the extension, you can create a feature branch like so:

$ git flow feature start my-feature-name
and finish it like so

$ git flow feature finish my-feature-name
The commands for hotfixes and releases are similar, though they use the version number in place of a branch name, like so:

# Create hotfix number 14 for this minor version.
$ git flow hotfix start 2.4.14

# Create the next release
$ git flow release start 2.5.0
Git flow then creates the version tag for you and kindly reminds you to bump the version in any configuration or manifest files (which you could do with a task manager like grunt).

Hope that helps :) I'm not sure exactly how you'd integrate it all with your Travis CI setup, but I'm guessing githooks will get you there.

GIT:
1. How to create a branch.

Git branch branchname

2. Create branch and switch to that branch

Git checkout -b branchname

3. Assume that you have a master branch.

4. There is a issue and you want to create a new branch issue53.

5. You got a issue and you are creating branch issue53
git branch -b issue53
git commit -m "somechange" index.html


6. At the same time you got some production issue
git checkout master
git branch hotfix
git checkout hotfix
git commit -m "somechange"
you have completed the changes in hosfix branch

7. Switch to master brach and merge the changes from hotfix
git chekout master
git merge hotfix

8. Now delete the hotfix branch
git branch -d hotfix

9. Now move back to issue53 branch and complete the changes
git checkout issue53
git commit -m "somechanges"

10. Now move back to master and merge the changes
git checkout master
git merge issue53

11. Now delete the branch issue53
git branch -d issue53

11.If you want to use a graphical tool to resolve these issues, you can run git mergetool, which fires up an appropriate visual merge tool and walks you through the conflicts:


Git workflows:
1. Centralized workflow
There is a master branch.
Everybody in the Team has push access to that branch.
The code will not be overwritten because Git wont allow non-fast forward commits.


2. Integration Manager workflow
The project maintainer pushes to their public repository.

A contributor clones that repository(forking in github) and makes changes.
The contributor pushes to their own public copy.
The contributor sends the maintainer an e-mail asking them to pull changes.
The maintainer adds the contributor’s repo as a remote and merges locally.
The maintainer pushes merged changes to the main repository.

3. Dictator and Lieutenants Workflow
Regular developers work on their topic branch and rebase their work on top of master. The master branch is that of the dictator.
Lieutenants merge the developers’ topic branches into their master branch.
The dictator merges the lieutenants’ master branches into the dictator’s master branch.
The dictator pushes their master to the reference repository so the other developers can rebase on it.

Git merge:
git checkout feature
git merge master
This creates a new “merge commit” in the feature branch that ties together the histories of both branches. it will create domand shape.

git rebase:
git checkout feature
git rebase master
This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master. But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.

Jquery:
1. jquery("domelement") is the selector of the domelement

2. $ is the shorthand for jquery

3. $() is the shorthand for $(document).ready()

4.
$( '#header' ); // select the element with an ID of 'header'
$( 'li' );      // select all list items on the page
$( 'ul li' );   // select list items that are in unordered lists
$( '.person' ); // select all elements with a class of 'person'

5. To check whether my selector returned something use

if ( $( '#nonexistent' ).length > 0 ) {
}

6. $( '<p>Hello!</p>' ); creates new element

7. We can determine whether a selection meets certain criteria using the .is() method

8. Getters & setters
set li with text 'new html'
$( 'li' ).html( 'New HTML' );

Drupal behaviour
===============
If you want your javascript functions to run even after ajax call write your scripts in behaviours

To create a function use
functionname: function () {

}

(function ($) {
Drupal.behaviors.gopi = {
            attach: function (context, settings) {
    // Run before real initialization
    this.setup();
    this.setup2();
  },

  setup: function () {
      // alert("hello");
  },
  setup2: function () {
      // alert("hello2");
  }
};
})(jQuery);

9. dot operator is used to access the property of the object.
Drupal is a object. settings, behaviours, locale, FA are properties of Drupal object.

10. myObj={} // a blank object

In JavaScript every function is actually a Function object.
One of the main difference between a function expression and a function declaration is that A function expression can be called (invoked) immediately by using a set of parentheses, but a function declaration cannot be.


Drupal:
custom_example.module

<?php
/*
* Implementation of hook_menu()
*/
function custom_example_menu(){
  // path location (<a href="http://www.url.com/foo/bar" title="http://www.url.com/foo/bar">http://www.url.com/foo/bar</a> )
  $items['foo/bar'] = array(
    // page title
    'title' => 'Page Title',
    // describe the page for the menu system.  site visitors will not see this
    'description' => 'Simple custom hook_menu() implementation.',
    // function that is called when visiting the new path
    'page callback' => 'my_page_function',
    // permissions required to view page
    'access arguments' => array('access content'),
  ); 
  return $items;
}
/*
* Returns custom content to Drupal
*/
function my_page_function(){
  // Call theme() function, so that Drupal includes the custom-page.tpl.php template
  return theme('my_custom_template');
}
  
/*
* Implementation of hook_theme().
*/
function custom_example_theme(){
  return array(
    'my_custom_template' => array(
      // template file name will be custom-page.tpl.php
      'template' => 'custom-page',
    ),
  );
}

Drupal core will call attached behaviors when the DOM (the object representation of the HTML) is fully loaded or DOM is manipulated via AJax call, passing in two arguments:

context: which contains the DOM.
settings: which contains all the settings injected server side.

if some portion of the content is removed or moved across the page, the code will have no option to unregistered event handlers or update information about the already processed elements. Drupal provides an API for this called behaviors

Functionality to override JavaScript
Easy to reattach and attach behavior to specific context
HTML can be loaded via AHAH

Scenerio1:
A file upload happening via ajax is showing error message on browser. You want to hide it. How?
Ans: Drupal.behaviours

Scenerio2:
I have a JS file in core and I want to override it with my version of JS file. How do I do that?
Ans: hook_js_alter()

scenerio3:
I have a custom error page where I want to attach search form.
Ans: $output .= drupal_get_form('search');

function mytheme_preprocess_html(&$vars) {
  $vars['classes_array'][] = 'new-class';
}


SASS:
Sass, we usually refer to the preprocessor and the language as a whole.
Sass, also known as the indented syntax
SCSS, a CSS-like syntax. If you rename as .scss file to .css it will just work.

SCSS
====
1. SCSS helps to maintain large style sheets
2. A tool can convert SCSS document to .css. The convertion can happen on the fly also by webserver.
Practicle uses of SCSS:

A) Nesting

article {
      border-right: solid 0.2em red;
}

article h1 {
      color: red;
}

article p {
      margin: 1em 0;
}
can be converted to

article {
      border-right: solid 0.2em red;

      h1 {
            color: red;
      }

      p {
            margin: 1em 0;
      }
}

B) Variables
They make it possible to use the same value in multiple places
$main-color: red;

article {
      border-right: solid 0.2em $main-color;
}
C) Mixins
Mixins contains styles that can be re-used throughout the SCSS document.
@mixin message {
      border-style: solid;
      border-width: 2px;
      border-radius: 1em;
      padding: 1em;
}

.message {
      &.alert {
            @include message;
            border-color: $alert-color;
      }
}

partials:
You can create partial Sass files that contain little snippets of CSS that you can include in other Sass files. This is a great way to modularize your CSS and help keep things easier to maintain.You can use @import directive to include the partial file in another file.

Import:
Import is used to import .scss file in another .scss file. The only drawback is that each time you use @import in CSS it creates another HTTP request.


extend:
using @extend lets you share a set of CSS properties from one selector to another.


Shell/Performance:
#!/bin/bash => It tells that bash program is going to run this script
chmod u+x => only i can execute the script
chmod +x => anybody can execute the script

performance:
Systat is a statistical software
All the metrics for a particular day are saved in sa<day of month>. Example sa14.
The default setting of sar is to record the latest 7 days. This value can be increased up to 1 month. Every day is stored in a single file (e.g. /var/log/sysstat/sar01). This files can be concatenated with "cat". To merge multiple days, please execute the following command:
cat /var/log/sysstat/sar?? > /tmp/sar.all
The resulting file can be directly opened in kSar.

Drupal message:
Drupal message body is handled with Drupal messenger() function

Comments

Popular posts from this blog

Programatically create layout builder section in Drupal

Code quality analysis of Drupal. Can I use Sonar?

Set up Drupal7 to Drupal8 migration in simple steps (using Drush)