Skip to content

Instantly share code, notes, and snippets.

View zuhairkareem's full-sized avatar
🏠
Working from home

zuhairkareem

🏠
Working from home
View GitHub Profile
@zuhairkareem
zuhairkareem / gist:c4598d3b16ea364ec80539a5b3723c9e
Last active August 18, 2016 09:18
To Get NID from current URL in D8
<?php
use \Drupal\Core\Url;
$current_url = Url::fromRoute('<current>');
$path = $current_url->getInternalPath();
// Retrieve an array which contains the path pieces.
$path_args = explode('/', $path);
$nid = $path_args['1'];
@zuhairkareem
zuhairkareem / gist:3d5ca244c20617a39d0ac9a8a5c19111
Created June 29, 2016 10:24
Get field data from entity in D8
$entity_st = \Drupal::entityManager()->getStorage('entity_name');
$entity_obj = $entity_st->load(entity_id);
$field_value = $entity_obj->get('field_name')->value;
@zuhairkareem
zuhairkareem / gist:06a56b80bc3497dc6db0d408122feadf
Last active September 18, 2016 16:54
Get value from field reference of an entity
<?php
// Get all values of field reference as array
// $entity is entity object - node, user, taxonomy
$field_arr_ref = $entity->field_name_reference->getValue();
$field_arr_id = $field_arr_ref[0]['target_id'];
//Another method to get all values
$ref_entities = $entity->get('field_name')->referencedEntities();
// Get only one value
function hook_views_query_alter(\Drupal\views\ViewExecutable $view, \Drupal\views\Plugin\views\query\QueryPluginBase $query) {
if ($view->id() == 'view_name' && $view->current_display == 'block_1') {
$current_url = Url::fromRoute('<current>');
$path = $current_url->getInternalPath();
// Retrieve an array which contains the path pieces.
$path_args = explode('/', $path);
$node_id = $path_args['1'];
$node_object = Node::load((int) $node_id);
$tag_arr = $node_object->field_tags->getValue();
foreach ($tag_arr as $key => $value) {
@zuhairkareem
zuhairkareem / gist:f950e1f4f61bc6547847ffdf2bbf8e5d
Created June 29, 2016 10:28
Get URL from image field from node in D8
$node->field_image->entity->url();
$url_object->toString();
@zuhairkareem
zuhairkareem / gist:95c823a07b3be121e2870abe0111a231
Created June 29, 2016 10:30
Get imageurl of image field with an imagestyle
use Drupal\image\Entity\ImageStyle;
$image_url = ImageStyle::load('customer_logo_150x120_')->buildUrl($node->field_customer_logo->entity->getFileUri());
@zuhairkareem
zuhairkareem / gist:7f6d414c080202f17eec3092117002e1
Last active August 29, 2016 11:58
Get Block in a variable in a preprocess function in a template file in D8
First we have to get the list of blocks with block id which we can we get if we execute this query
<?php
$ids = \Drupal::entityQuery('block')->execute();
?>
If the block we created is not in this list(in the case of creation of custom block not assigned to any regiom)
, we have to place the block in some region and disable it.
<?php
$block = \Drupal\block\Entity\Block::load($block_id);
@zuhairkareem
zuhairkareem / gist:27a6f741b8214b3efeed71164cc8c89c
Created July 13, 2016 12:07
Get url from link field in twig
content.field_link.0['#url']
@zuhairkareem
zuhairkareem / 0_reuse_code.js
Created July 13, 2016 12:09
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console