Skip to content

Instantly share code, notes, and snippets.

View woutersf's full-sized avatar

woutersf woutersf

View GitHub Profile
@woutersf
woutersf / routematch.php
Last active December 28, 2016 12:09
D8 Match a canonical route
<?php
//If is frontpage
$on_frontpage = \Drupal::service('path.matcher')->isFrontPage();
//Match canonical route
$route_match = \Drupal::routeMatch();
if ($route_match->getRouteName() == 'entity.node.canonical') {
// Do something..
}
@woutersf
woutersf / d8_menu_get_items.php
Last active December 22, 2016 06:22
D8 Add class in preprocess conditionally (replaces menu_get_items)
<?php
//For nodes.
if ($node = \Drupal::routeMatch()->getParameter('node')) {
if ($node->getType() == 'article') { // Test the node type.
$is_video = $node->get('field_is_video')->value; // Get the field value of on of the node fields.
$class_suffix = $is_video ? 'video' : 'no-video';
$vars['attributes']['class'][] = Html::getClass($node->getType() . '--' . $class_suffix); //Add Class to the variables.
}
}
@woutersf
woutersf / drupal8_render_Xml_or_Json.php
Last active January 4, 2017 08:52
D8 output XML/Json
<?php
///////////////////JSON
//How to render Json from Drupal 8
$response = new Response();
$response->setContent(json_encode($mydata));
$response->headers->set('Content-Type', 'application/json');
return $response;
@woutersf
woutersf / drupal8_test_user_logged_in.php
Last active January 4, 2017 08:51
drupal8 test if user is logged in
<?php
//If anonymous
if (\Drupal::currentUser()->isAnonymous()) {
// Do something..
}
//if user has role
$roles = \Drupal::currentUser()->getRoles();
if(!in_array("administrator", $roles)) {
<?php
//use...
class MySettingsForm extends ConfigFormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
//The form
//..
<?php
//The submit handler function:
public function reImportAllFiles(array &$form, FormStateInterface $form_state){
$form_state->setRedirect('vrt_dram.batch', array());
}
@woutersf
woutersf / drupal8_image_style_url.php
Created December 30, 2016 10:25
Drupal 8 image-Style an image
<?php
use Drupal\image\Entity\ImageStyle;
use \Drupal\file\Entity\File;
//Load the file
$file_id = 2;
$file = file_load($file_id);
//Render the IMage style "large"
$image_large_url = ImageStyle::load('large')->buildUrl($file->getFileUri());
@woutersf
woutersf / Drupal8_drupal_http_request.php
Last active January 3, 2017 10:00
Drupal8 version of drupal_http_request
<?php
$client = \Drupal::httpClient();
$url = 'http://something.com/data.json';
try {
$request = $client->get($url);
$status = $request->getStatusCode();
//Do something here.
//For example:
return json_decode($request->getBody()->getContents());
}
@woutersf
woutersf / drupal8_variable_get_override_settings.php
Last active March 12, 2017 08:18
Drupal8 settings and config, override settings in settings.php, variable_get en variable_set
<?php
// Fetching The data via the container (old variable_get)
$data = \Drupal::config('my_module.settings')->get('my_setting_name');
// Fetching the data via the Settings class
use Drupal\Core\Site\Settings;
$settings = Settings::get('http_client_config');
$data = $settings['my_setting_name'];
@woutersf
woutersf / drupal8_test_https.php
Last active May 12, 2020 12:30
Drupal 8 test if on https page
<?php
// Drupal 7
global $is_https;
if ($is_https) {
// Do something
}
// Drupal 8
if (Drupal::request()->isSecure()) {