This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//If anonymous | |
if (\Drupal::currentUser()->isAnonymous()) { | |
// Do something.. | |
} | |
//if user has role | |
$roles = \Drupal::currentUser()->getRoles(); | |
if(!in_array("administrator", $roles)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//The submit handler function: | |
public function reImportAllFiles(array &$form, FormStateInterface $form_state){ | |
$form_state->setRedirect('vrt_dram.batch', array()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Drupal 7 | |
global $is_https; | |
if ($is_https) { | |
// Do something | |
} | |
// Drupal 8 | |
if (Drupal::request()->isSecure()) { |
OlderNewer