[Drupal] Major code changes while migrating from Drupal-7 to Drupal 8
As the drupal is getting ready to migrate to a higher version 8. The older version is overhauled by the newer in every sense. As Zyxware is a regular contributor to drupal, its relevant that we need to upgrade our contibuted modules too.
Here I am listing some of the major changes found out during migrating modules from Drupal 7 to Drupal 8
Instead of $node you have to use NodeInterface $node and use Drupal\node\NodeInterface on the top of the page
Following are the changes(first colume drupal 7 code and second colume contain changed drupal 8)
- $node->nid : $node->id()
- $node->title : $node->title->value OR $node->getTitle()
- $node->type : $node->getType()
- $node->body[$langcode][0]['value'] : $node->$node->get('body')->value OR $node->body->value;
-
Changes to check user permission Drupal 7:
user_access('access example_content')
Drupal 8:
(\Drupal::currentUser()->hasPermission('access example_content'))
-
To debugg your code Drupal 7:
watchdog('debug', '<pre>'. print_r($array, TRUE) .'</pre>');
Drupal 8:
\Drupal::logger('module_name')->notice('<pre>'.print_r($array, TRUE).'</pre>');
-
To get path alias Drupal 7:
$path = drupal_get_path_alias($path);
Drupal 8:
$path = \Drupal::service('path.alias_manager')->getPathByAlias($path);
-
Get q variable from URL Drupal 7:
$_GET['q']
Drupal 8:current_path()
-
Access current user variables. Drupal 7:
global $user $user->uid;
Drupal 8:
$user = \Drupal::currentUser();
$user->id();