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

How to solve Apache 2.4.3 (with XAMPP 1.8.1) not starting in windows

Problem:

Just got XAMPP 1.8.1 installed on my Windows 8 PC, this version includes packages mentioned below:

    Apache 2.4.3
    MySQL 5.5.27
    PHP 5.4.7
    phpMyAdmin 3.5.2.2
    FileZilla FTP Server 0.9.41
    Tomcat 7.0.30 (with mod_proxy_ajp as connector)
    Strawberry Perl 5.16.1.1 Portable
    XAMPP Control Panel 3.1.0 (from hackattack142)


When I launched and tried to start Apache, it gave following error:



    12:04:41 PM  [Apache] Attempting to start Apache app...
    12:04:41 PM  [Apache] Status change detected: running
    12:04:42 PM  [Apache] Status change detected: stopped
    12:04:42 PM  [Apache] Error: Apache shutdown unexpectedly.
    12:04:42 PM  [Apache] This may be due to a blocked port, missing dependencies,
    12:04:42 PM  [Apache] improper privileges, a crash, or a shutdown by another method.
    12:04:42 PM  [Apache] Check the "/xampp/apache/logs/error.log" file
    12:04:42 PM  [Apache] and the Windows Event Viewer for more clues

After that I checked error.log, it was empty so no help from there.


Solution:


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

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...