There are following changes in drupal7 and drupal8.
1. variable_set()/variable_get() are gone
In Drupal 7, all variables are global, so accessing and saving them is done this way:
// Load the site name out of configuration.
$site_name = variable_get('site_name', 'Drupal');
// Change the site name to something else.
variable_set('site_name', 'This is the dev site.');
In Drupal 8, configuration will only be lazy-loaded when needed. The above code would therefore change as follows:
// Load the site name out of configuration.
$config = config('core.site_information');
$site_name = $config->get('site_name');
// Change the site name to something else.
$config->set('site_name', 'My Awesome Site');
$config->save();
2. $_GET[‘q’] is gone
The PHP super global key $_GET['q'] is 100% missing in Drupal 8, now you are forced to use the standard method current_path().
Drupal 7:
function contact_mail($key, &$message, $params) {
$variables = array(
'!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
}
Drupal 8:
function contact_mail($key, &$message, $params) {
$variables = array(
'!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
}
3. hook_init() is gone
I was wondering why this hook was not firing, turns out this was removed recently - I was able to replace it instead with hook_page_build().
1. variable_set()/variable_get() are gone
In Drupal 7, all variables are global, so accessing and saving them is done this way:
// Load the site name out of configuration.
$site_name = variable_get('site_name', 'Drupal');
// Change the site name to something else.
variable_set('site_name', 'This is the dev site.');
In Drupal 8, configuration will only be lazy-loaded when needed. The above code would therefore change as follows:
// Load the site name out of configuration.
$config = config('core.site_information');
$site_name = $config->get('site_name');
// Change the site name to something else.
$config->set('site_name', 'My Awesome Site');
$config->save();
2. $_GET[‘q’] is gone
The PHP super global key $_GET['q'] is 100% missing in Drupal 8, now you are forced to use the standard method current_path().
Drupal 7:
function contact_mail($key, &$message, $params) {
$variables = array(
'!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
}
Drupal 8:
function contact_mail($key, &$message, $params) {
$variables = array(
'!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
}
3. hook_init() is gone
I was wondering why this hook was not firing, turns out this was removed recently - I was able to replace it instead with hook_page_build().
Druapl - 8.0.0-beta15
ReplyDeleteLook like current_path() function no longer exists. Alternatively we can use
$current_path = \Drupal::url('', [], ['absolute' => FALSE]);
// For absolute url
$current_path = \Drupal::url('', [], ['absolute' => TRUE]);