Skip to content

Instantly share code, notes, and snippets.

View woutersf's full-sized avatar

woutersf woutersf

View GitHub Profile
<?php
//The submit handler function:
public function reImportAllFiles(array &$form, FormStateInterface $form_state){
$form_state->setRedirect('vrt_dram.batch', array());
}
<?php
//use...
class MySettingsForm extends ConfigFormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
//The form
//..
@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)) {
@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 / 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 / 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..
}