Drupal, Silex, Symfony2, Ubuntu, Website Development

Access node machine name in Drupal 8 function

At times you need to know the machine name of the current node. The function below will allow you to access the machine name anywhere on a page load.

 * @return array|null
function _access_current_node_machine_name()
    $nidResult = \Drupal::service('path.current')->getPath();
    $nid = explode('/', $nidResult);
    if ($nid[2] != NULL) {
        $node = \Drupal\node\Entity\Node::load($nid[2]);
        return array(
            'node_id' => $nid[2],
            'node_content_type' => $node->getType(),
    return NULL;

Read more

Adding a global variable to the twig layer on every part of the page in Drupal 8

In Drupal 8 adding a variable so the twig layer can access it on every part of  the page can be done by using the template preprocessor, The word template in this function will be replaced by your theme name.

 * Implements hook_preprocess().
function template_preprocess(&$vars) {

  $vars['global_var_name'] = 'This is my var';


And then in the twig layer(file) where you need to use the variable.

Read more

How to I access an entity (e.g. node) field in Drupal8

This function access an entity field on a node in Drupal8. It works the following way, 
  • Checks to see if node exists
  • Gets the current nodes NID
  • Loads the entity that is attached to the NID
  • Returns the field name of the NID 
 * @param $fieldName
 * @return array of Objects
function _access_a_nodes_field($fieldName) {
    $nid = \Drupal::routeMatch()->getParameter('node');
    $node = \Drupal\node\Entity\Node::load($nid->id());
    return $node->get($fieldName)->getValue();
  return NULL;
Read more

Request a Drupal 8 node NID on page load

This function does exactly what it says, it returns the current Drupal 8 node NID that is being requested by the user. Be careful where you call it if the page is not a node it can break the website.

 * @return string
function _access_node_id() {
  $node = \Drupal::routeMatch()->getParameter('node');
  return $node->id();
Read more

Accessing taxonomy's name and parent TID in Drupal 8

This function allows you to access a taxonomy entity fields and get the following results. The child's parents TID, the TID, and the field's string name of the TID being requested.  The results are placed into an array and custom keys can be used when being iterated over.

It works in Drupal 8 but I have access the fields using name spacing and avoided depreciated Drupal 8 functions and methods so it should be future proofed for Drupal 9 

Read more

How to stop a specific block from being cached in Drupal 8

If you have a block in Drupal 8 that needs to update its contents every time there is a page load then use the hook and code below.

On a Drupal 8 website we developed, it needed to swap out different menus depending on the page that was being requested. We found that Drupal was caching the block twig logic and the only way we could get the menus to swap was keep clearing the cache. To stop this from happening we added ['max-age'] = 0 to the right part of the block render array and all the caching on the block type stopped.

Read more

Exiting a SSH terminal session once if the terminal is frozen

If you have an SSH terminal session open and you can't get the host computer to talk to you don't close the terminal. Exiting an SSH terminal session once it is frozen is really easy just type the command in below and restart the SSH session

Press enter in the terminal and then "~ and ."
Read more

Can't add Online accounts in ubuntu 16.04

In ubuntu 16.04 to add an online account like a Nextcloud instance got to  settings -> online accounts, If you click the add button and it does will not respond then install gnome-control-center gnome-online-accounts packages via the command line. This allows you to bypass the unity online accounts created by ubuntu  and use the gnome version. Below is the commands you need to open gnome online accounts.

$: gnome-control-center online-accounts
Read more

Accessing a block types content programatically

For anyone who is looking to access Block Fields programmatically that were created in block content in Drupal 8, this function will achieve that and will also allow you to access the files URL created in the "block content" image field if it exists. This uses Drupals new namespacing so it should work into the future with Drupal 9

I created a block type in Drupal 8's UI (structure/block/block-content/types ) and added the fields. To access block type in hook_preprocessor_html programmatically you can use the following function

Read more

Make vim default editor for git and ubuntu

To make vim the default editor for Git or your entire operating system. Firslty make sure that vim is installed and then you can chose one of the options below.


If you want to change Git default editor only use the following

$~ git config --global core.editor "vim"

If you want to change the default editor across the whole installation use this command

$~ sudo update-alternatives --config editor
Read more