kaushalkishore's blog

Guzzle HTTP Drupal 8 Calling

try {
	$result = $client->get(URL_OF_API);
	$stream = $result->getBody();
	$jsonData = $stream->getContents(); // returns all the contents
	$dataArray = json_decode($jsonData);
	print_r($dataArray);
}
catch (\GuzzleHttp\Exception\ClientException $e) {
	$response = $e->getResponse();
}

Entity Manager in Drupal 8

$storage = \Drupal::entityManager()->getStorage('node');
$nids = $storage->getQuery()
->condition('type', 'media')
->condition('status', 1)
->execute();
$nodeArray = $storage->loadMultiple($nids);

Converting Object to Array:
$object->toArray()

Include JS or CSS Library in Drupal 8

1. Create a file module_name.libraries.yml and add the below code

Plugin_name: 
  version: 1.x
  js:
    js/JS_FILE_NAME: {}

2. Implement the below hook into your module_name.module file

function module_name_page_attachments_alter(array &$attachments) {
  $attachments['#attached']['library'][] = 'module_name/plugin_name';
}

3. Place your js file into module folder inside the js folder.

Pages