Tuesday, January 29, 2019

How to create a subsite in Drupal


  1. Create a subdirectory inside sites folder of your Drupal installation, it will be inside docroot folder.
    Drupal-Multisite Directory Structure
  2. Create a virtual host. You can follow any of the steps from traditional way to executing a bash script. I recommend executing a bash script so that you don't need to go through all steps everytime.
  3. Make a copy of /docroot/sites/example.sites.php called /docroot/sites/sites.php
  4. Add the below line of code inside the sites.php.
    $sites['sub-site-address.domain'] = 'subsite';
  5. Check the newly created subsite from your browser 'sub-site-address.domain' and install the site.
  6. Create folders for modules, themes, libraries etc., and add your custom theme or module specific to this newly created site if you wish.

Friday, January 25, 2019

Steps involved from request to response of website

When you first type in an address, the browser will look up the hostname in DNS, if it is not already in the browser cache.
Then the browser sends an HTTP GET request to the remote server.
What happens on the server is really up to the server; but it should respond back with an HTTP response, that includes headers, which perhaps describe the content to the browser and how long it is allowed to be cached. The response might be a redirect, in which case the browser will send another request to the redirected page.
Obviously, server response time will be one of the critical points for perceived performance, but there are many other things to it.
When a response is returned from the server, the browser will do a few things. First, it will parse the HTML returned, and create it's DOM (Document Object Model) from that. Then it will run any startup Javascript on the page; before the page is ready to be displayed in the browser. Remember, that if the page contains any resources such as external stylesheets, scripts, images and so on, the browser will have to download those, before it can display the page. Each resource is a separate HTTP get, and there is some latency time involved here. Therefore, one thing that in some cases can greatly reduce load times is to use as few external resources as possible, and make sure they are cached on the client (so the browser don't have to fetch them for each pageview).
To summarize, to optimize performance for a web page, you want to look at, as a minimum:
  • Server response time
  • Bandwidth /content transfer time.
  • Make sure you have a small and simple DOM (especially if you need to support IE6).
  • Make sure you understand client-side caching and the settings you need to set on the server.
  • Make sure you make the client download as little data as possible. Consider GZipping resources and perhaps dynamic content also (dependent on your situation).
  • Make sure you don't have any CPU intensive javascript on Page load.
Ref: https://stackoverflow.com/questions/2896199/what-are-the-steps-involved-from-entering-a-web-site-address-to-the-page-being-d

Tuesday, January 22, 2019

PHP Quick Help

Static late binding

As of version 5.3, PHP introduced a new feature called PHP static late binding. Basically, instead of using the self, you use the static keyword that references to the exact class that was called at runtime.

Traits

Introduced in PHP 5.4.0.

Inheritance makes the code very tightly coupled therefore makes the code hard to maintain.

To overcome this problem, as of version 5.4.0,  PHP introduced a new unit of code reuse named trait. Traits allow you to reuse a set of methods freely in many different classes that do not need to be in the same class hierarchy.

The trait is similar to a class but it is only for grouping methods in a fine-grained and consistent way. It is not allowed to instantiate a trait on its own.

To use a trait in a class, you use the use keyword. All the trait’s methods are available in the class where it is used. Calling a method of a trait is similar to calling an instance method.

Abstract Class

Can not be instantiated. Should contain at least one abstract method.

Shallow Copy & Deep Copy


$p2 = $p1; // $p2 & $p1 objects will point to same object, reference change only.


$p2 = clone $p1; //Creates a new object from $p1 object

Function & Method

Both are same, a set of statements grouped by a name. So that they can execute wherever we want by using that name. The only difference is that a method is written inside a class, so it's always part of the class and can be called by using the object of the same class. Functions are written outside the class or normally in a plain PHP file.

Magic Methods


Magic methods are nothing but class member functions that are automatically executed based on some other actions.


__set(), __get(), __construct(), __destruct(), __call(), __clone(), __toString(), __autoload()  are examples of magic methods.


__set() & __get() are executed when an object tries to access(read / write) a private or non existent property.



__call() is executed when an object tries to call a nonexistent method.

Interface


An interface is nothing but a class with some skeleton member functions(methods) which will help the programmers to implement the same methods in a different class which are unrelated. So this will allows you to model multiple inheritances so that a class can implement multiple interfaces whereas it can extend only one class.

Why should we use PHP interfaces?
There are several reasons that we should use interface in our PHP object-oriented programming:

By implement an interface, the caller of the object need to care only about the object’s interface, not implementations of the object’s methods. Therefore the implementations can be changed without affecting the caller of the interface.
An interface allows unrelated classes to implement the same set of methods, regardless of their positions in the class inheritance hierarchy.
An interface enables you to model multiple inheritances because a class can implement more than one interface whereas it can extend only one class.

Thursday, January 17, 2019

Some useful Linux commands

Copy files from one folder to another

The below will command will copy all files inside the source folder to the destination folder.

cp path/to/source/* path/to/destination

This will not copy sub-folders and files in it.


cp -r path/to/source/* path/to/destination

or

cp -a path/to/source/* path/to/destination

This will recursively copy folders, sub-folders, and files in it.

Rename a folder


MV path/to/old path/to/new

Change read/write/access/owner/group permissions of a folder


CHMOD [XYZ] path/to/folder

X - Will affect Owner permissions
Y - Will affect Group permissions
Z - Will affect Others permissions

[X, Y & Z] will be calculated from below

0 – no permission
1 – execute
2 – write
3 – write and execute
4 – read
5 – read and execute
6 – read and write
7 – read, write, and execute

For example, if we want to give Read, write and execute permission to Owner, Read & Execute permissions to Group and read permission to Others, we have to execute the command like below

CHMOD 754 path/to/folder

Change ownership of a folder or file with current user


sudo chown -R $USER /path/to/folder

Tuesday, January 15, 2019

Show previous chapter, next chapter navigation in book page of Drupal 8.

Q: I want to show a navigation to previous chapter & next chapter in my book detail page(node view). I'll explain the scenario: I have a parent book and multiple child pages, each may have children up-to level n. I'm showing the level 1(depth 2) pages in a single view(details page) and the children in same page. Here comes the navigation part. I want to show a navigation to previous and next chapters in the Book details page.

A: After creation of the Book page details page, create a view as per the below pictures

1.Create a View-Give name and select the content type. Check 'Create a Block'.

2. As per the scenario I want to see Level 2 book pages(siblings of current page), so I set Depth is equal to 2 under Filter criteria. Sort criteria set us Depth's Ascending order.
3. Created a contextual filter: Node ID and set its default value from the URL(as we are viewing the node view page)
4. Under more we have to check the exclude option so that the result will exclude the current book page.

5.Save the view. See I created two blocks , one for previous link and one for next link. 
By visiting structure > Block Layout you are able to place the newly created blocks.

 6. Now we have to create a module to apply some query conditions to the view. Simply create a module and paste the below code in your module's .module file and enable it.

<?php
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;
/**
 * To get the next and previous chapters
 *
 * @param ViewExecutable $view
 * @param QueryPluginBase $query
 * @return void
 */
function chapter_navigation_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {   
  
  $node = \Drupal::routeMatch()->getParameter('node');
  
  if ($view->id() == 'chapter_navigation' && $node) {
    $operator = $view->getDisplay()->display['id'] == 'next_chapter' ? '>=' : '<=';
    foreach ($query->where as &$condition_group) {
      array_push($condition_group['conditions'], [
        'field' => 'book.weight '.$operator.' ' . $node->book['weight'],
        'value' => [],
        'operator' => 'formula',

      ]);

    }   
 
  }

}