Tuesday, 29 May 2018

How to change Apache port in Windows

This problem may occur due to apache not getting required port (default is 80).
The port may be being used by other services.
For example: Skype also has default port 80.
Installing Skype and Apache both on same machine will cause conflict and hence Apache will not start.
Either, you change Skype port or change Apache port as described in following steps:
Change the ports of Apache and it will work for you. Go to httpd.conf
How to change port for Apache:
Search for:

ServerName localhost:80

Change it to:

ServerName localhost:81

Also Search For:

Listen 80

Change it to:

Listen 81

If you have created any virtual hosts, change the ports there also. Then restart your apache.

Reference: https://stackoverflow.com/a/18306621/1841760

Friday, 25 May 2018

Reorder divs (sequence) using CSS

Say, you have two divs. Left and right.
On desktop, you want sequence
Left - Right
But, on mobiles, you want:
Right - Left

This can be achieved through CSS properties.
Code:

Note: Use media queries appropriately.

<!DOCTYPE>
<html>
<head>
<title>Re ordering of divs using CSS.</title>
</head>
<body>
<div class="parent">
<div class="left" style=" height: 100px; background-color: grey; float: left;"><h1>Left</h1></div>
<div class="right" style="height: 100px; background-color: black; float: left;"><h1>Right</h1></div>
</div>

<style>
.parent div h1 {color: #ffffff;}
.parent {width: 100%; display: flex; flex-direction: column;}
.left {padding: 10px; width: 100%; order: 2;}
.right {padding: 10px; width: 100%; order: 1}
</style>
</body>
</html>

Without using above solution:



After using above solution:


Any suggestions/comments are most welcome.

Monday, 11 December 2017

Is there a way in drupal 8 to tell if the current page is a node?

    \Drupal::request()->attributes->get('node');

Taken from here: https://api.drupal.org/api/drupal/core!lib!Drupal.php/function/Drupal%3A%3Arequest/8

Now the docs say to never use this function, but I have used this to determine what kind of page I am on.

There is another function, but I am not sure it applies in your case:

https://api.drupal.org/api/drupal/core%21modules%21node%21node.module/function/node_is_page/8

At the very least, if you do not have an entity, you could mimic that in your code:

      $route_match = \Drupal::routeMatch();
      if ($route_match->getRouteName() == 'entity.node.canonical') {
        return true;
      }

Reference

Friday, 8 December 2017

Get all nodes of given type in Drupal 8 (maybe with a help of loadMultiple())


$nids = \Drupal::entityQuery('node')->condition('type','my_custom_type')->execute();
$nodes =  \Drupal\node\Entity\Node::loadMultiple($nids);

Thursday, 7 December 2017

Drupal 8 list all node types programatically

$types = \Drupal::entityTypeManager()
->getStorage('node_type')
->loadMultiple();
$nodeTypes = ! empty($types) ? array_keys($types) : [];

echo '<pre>';print_r($nodeTypes);echo '</pre>';

Source: drupal.stackexchange answer

Monday, 17 July 2017

How to avoid Notices and warnings errors in PHP code.

While writing PHP code, we often get errors like warnings and notices.

To avoid them, following are some best practises:

1) Always check if a variable is set before using it.

For example:

$age = $_GET['age'];

Should be replaced by

$age = isset($_GET['age']) ? ($_GET['age'] : '';

Reference:

http://php.net/manual/en/function.isset.php

2) Always check if array is not empty before looping over it.

For example:

foreach ($arr as $elem) {
  // Your code.
}

Should be replaced by:

if (! empty($arr)) {
 foreach ($arr as $elem) {
   // Your code.
 }
}

Reference:

http://php.net/manual/en/function.empty.php

How to convert stdClass object to associative array in php

Use json_encode() and json_decode()

$arr = json_decode(json_encode($yourObject), TRUE);

json_decode() 's second parameter is set to TRUE.

Function definition:

mixed json_decode ( string $json [, bool $assoc = false [, int $depth  > = 512 [, int $options = 0 ]]] )

That will convert your object into an associative array.

Reference:

https://stackoverflow.com/questions/34428702/convert-stdclass-object-to-associative-array-in-php/34428735#34428735

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid Tip #1) Children do not learn things, they emitate. So, try to do things by yours...