-
Drupal Code Based Development
Mar 5, 2018
APCu and OpCache optimization for a large Drupal site PUBLIC:
Put this in your /etc/php.d/opcache.ini or similar file:
php
# enhance opcache beyond defaults
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=100000
opcache.max_wasted_percentage=10
opcache.revalidate_freq=240
opcache.validate_timestamps=0
opcache.fast_shutdown=1
opcache.interned_strings_buffer=8
opcache.enable_cli=1
If you use an APCu user bin to cache various drupal cache bins in memory you will want to further optimize / tune things with it in /etc/php.d/apcu.ini or similar file. This also has support for you in drupal for upload progress without needing pecl.
php
apc.rfc1867=1
apc.rfc1867_prefix=upload_
apc.rfc1867_name=APC_UPLOAD_PROGRESS
apc.rfc1867_freq=0
apc.rfc1867_ttl=3600
Get HTTP_REFERER as Drupal normal path without language prefix PUBLIC
php
/**
* Get HTTP_REFERER as Drupal normal path without language prefix.
*/
function referer_path() {
if (!empty($_SERVER['HTTP_REFERER'])) {
$url = parse_url($_SERVER['HTTP_REFERER']);
if ($url['path'][0] == '/') {
$url['path'] = substr($url['path'], 1);
}
list(, $path) = language_url_split_prefix($url['path'], language_list());
return $path;
}
}
Drupal Coding Processes
Get field data from user object in Drupal 8 PUBLIC:
Get the proper field data from a user object using Drupal development as a programming hub you want to involve? Make the body and website data be presented to you and consider making moves that provide you data which you require.
php
// Load the current user.
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
// get field data from that user
$website = $user->get('field_website')->value;
$body = $user->get('body')->value;
// some default getters include
$email = $user->get('mail')->value;
$name = $user->get('name')->value;
$uid= $user->get('uid')->value;
POST example with Guzzle and session cookies PUBLIC:
php
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/core/vendor/autoload.php');
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$base_url = 'http://example.com';
try {
$client = new Client([
'base_url' => $base_url,
'cookies' => true,
'allow_redirects' => true,
'debug' => true
]);
$response = $client->post("/user/login", [
'cookies' => true,
"body" => [
"name"=> "username",
"pass"=> "password",
'form_id' => 'user_login_form'
]
]);
$token = $client->get('rest/session/token', [
'cookies' => true
])->getBody(TRUE);
$token = (string)$token;
$node = array(
'_links' => array(
'type' => array(
'href' => $base_url . '/rest/type/node/page'
)
),
'title' => array(0 => array('value' => 'New node title')),
);
$response = $client->post('entity/node', [
'cookies' => true,
'headers' => [
'Accept' => 'application/json',
'Content-type' => 'application/hal+json',
'X-CSRF-Token' => $token,
],
'json' => $node
]);
if ($response->getStatusCode() == 201) {
print 'Node creation successful!';
} else {
print "unsuccessful... keep trying";
print_r(get_defined_vars());
}
} catch(RequestException $e) {
echo $e->getRequest();
echo "\n\n";
if ($e->hasResponse()) {
echo $e->getResponse();
}
}
?>
Codes in Drupal Development
Page redirect after submit form with AJAX PUBLIC:
If you use ajax that is provision of form submission, the use of drupal_goto() is not implemented properly. The only thing happening is the error 200. Forcefully moving on page redirect can be used as a useful helper provided to you by CTools (this approach especially looks useful when your form comes as a display in your lightbox).
php
/**
* Implements hook_boot()
*/
function mymodule_boot() {
//Save current path
$_SESSION['current_path'] = current_path();
}
function mymodule_custom_ajax_form_submit($form, &$form_state) {
//Close lightbox and redirect to the current page
$path = $_SESSION['current_path'];
ctools_include('ajax');
ctools_add_js('ajax-responder');
$commands[] = ctools_ajax_command_redirect($path);
print ajax_render($commands);
exit;
}
Comments
- (no comments)