How do I get a page’s current URL in Twig for Drupal 8?

Having access to a site’s URL in your templates can come in handy for many different use cases, but one of the more common scenarios might be for those of you building multi-sites.

Let’s say you’re building a site with various domain names and you want to be able to conditionally change some content or functionality for each domain in your Twig templates. You can get the current URL and parse it for a substring like this:

{% set site_url = url("<current>") %}
{% if 'example-one.com' in site_url|render|render %}
     {# example-one.com content #}
{% elseif 'example-two.com' in site_url|render|render %}
     {# example-two.com content #}
{% endif %}

How do I set up a Drupal 8 Module to be an AJAX service?

File structures seem a lot different in Drupal 8, and I’m still getting the hang of them. This is an example of a simple module I made, so hopefully it can be a good starting point for you too!

my_module/my_module.info.yml

name: Module Name Here
type: module
description: Description of the module
core: 8.x

my_module/my_module.module

<?php 
//basically any code you want your module to do
//ex: any functions that need API calls or data that you call in any preprocessor functions
use Drupal\my_module\Controller\DefaultController as MyModule;
function my_module_get_data(){
  $data = MyModule::hello_world();
  return json_decode($data->getContent());
}

**Drupal 8 includes a way to make URLs via a routing.yml file.
my_module/my_module.routing.yml

#GENERAL
my_module.hello_world:
  path: '/my_module/hello_world'
  defaults:
    _controller: '\Drupal\my_module\Controller\DefaultController::hello_world'
  requirements:
    _permission: 'access content'

my_module/src/Controller/DefaultController.php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends ControllerBase{
  public static function hello_world(){
    return new JsonResponse(true);
  }
}

So in this example: now if you go to http://mywebsite.com/my_module/hello_world if will call the defaultcontroller in your module and print out JSON you can use in an ajax call.  You’ll also notice I can use it on the server side as well (my_module.module)

To me this was a lot easier or a process than Drupal 7 was. Happy Drupal Coding!

How do i get all the taxonomies of a node to render in Drupal7?

After following a tutorial for getting the taxonomy terms of a node, I found that I still didn’t have all the pieces of the puzzle. Doing a foreach over $tags = $node->field_tags; would just give a disappointing one result with those nodes had more than one taxonomy assigned to it. (The other 3 were being lost in the code abyss!)

This was solved by digging a little deeper into the object that was return:

<div class="tagsList views-field-field-tags">
	<? $tags = $node->field_tags['und']; ?>                            
	Tags: <? $count = 0; 
	foreach($tags as $tag){ 
	if($count > 0){echo ", ";}
	echo '<a href="'.taxonomy_term_path($tag['taxonomy_term']).'">'.$tag['taxonomy_term']->name.'</a>'; 
	$count++;
	} ?>
</div>

Turns out i had to go one layer deeper to loop the tags object as the language code is undefined. SO now my taxonomies are working great on the node.tpl.php for that feed.

Source