You are here

5 Great Drupal 8 Gotcha's

Drupal beach nice


Drupal 8 has changed many things developers were used to. Some functionality has been depreciated while other has come in.

Here we have listed 5 gotcha's thay may indeed provide you with some usefull help and inspiration for next projects.

Gotcha 1. Getting a single item field value is easier;

Pretty much straight foward now. Imagine you'd have and entity object loaded, like $entity and you'd have a field called 'field_tags'. Correct typically that would be an entity referrence - relationalship to taxonomy item.
So to get a value of this field;

$value = $entity->get('field_behaviour')->first()->getValue();
$value is aculally an array, so to access the target taxonomy id
$tid = $value['target_id'];

Reffer to field definitions for the structure of $value array. See also that I am reffering to first item in the field value list, so no more nasty reset() call to an array.

Gotcha 2. Deleting a view from a storage.

If you remember in Drupa7 all views created via UI tool were stored in database. As of Drupal8 Views have been transfered to configuration entity. Rather than being storred in database configuration entities and are cached on file system. So if you don't need a view any more and project has a well implemented continuos integration system, the best way
way is to add deletion script in module_name.install file;

function module_name_update_8001($sandbox){
$view = \Drupal\views\Views::getView('view_id');
    if ($view) {

Gotcha 3. Adding a new base field to an entity

In Drupal7 should you considered adding a new field to an entity it either required;

  • run a sophisticated update system and adding new column to multiple tables or,
  • creating your own schema and then relating to enitity table system.

Drupal8 has made this process easier. Now everything is field on entity, including id - identifier. That means you ceasily add a new base field to an entity (node for example). Here is how it is done in code;

 * Implements hook_entity_base_field_info().
function mymodule_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type)
    $fields = [];
    if($entity_type->id() == 'node'){
        $fields['properties'] = \Drupal\Core\Field\BaseFieldDefinition::create('map')
            ->setLabel('Query Properties')
            ->setDescription('Serialised Properties to expose as services');

    return $fields;

Paste this code inside your .module file. For sure if module is new and not installed enabling it will trigger field creation. If you do this on the existing one you may need to run drush updatedb --entity-updates to launch field updates.

For more detailed description on field types please see this link -

Gotcha 4. Installing and un-intalling a module programmatically

You may say that this functionality existed in Drupal7, yes true. However one thing that has changed is module management workflow. There is no more disabling or enabling, hence you either install or uninstall a module. So use it with caution, if your module holds a database schema it will be wiped out completely.
The other thing with Drupal8 drastically embracing object orientated programming, lazy loading and services there is no more drupal_uninstall_modules function, so how I can achieve this task programmatically? As it turns out quite similar. In your .install file create a new update funtion;

function mymodule_update_8002($sandbox){}

For installation put this in the function body;


For uninstalling


For sure run drush updb or hit update.php in the browser afterwards to trigger new updates.

Gotcha 5. Our well belowwed arg() function is depreciated

Yes, this nice and easy to use url parameter access tool is taken off Drupal8 so what we could do? There are few options;

$current_path = \Drupal::service('path.current')->getPath();
$path_args = explode('/', $current_path);

$path args becomes an array, so if your url is something like - main/staff/here - 'main' == $path_args[1]

You can also load an menu object from route parameter like this,

// Load the current node.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {

For more on this matter you can follow here -

We have shared just 5 of all new Gotcha's in Drupal8. Indeed there are more, so share below and follow our social media links for more new updates.