Drupal Problems and Solutions

DrupalABC aims to provide solution to programming problems faced by Drupal developers.Start learning by reading some of the latest problems below-

How to fetch all the terms of a vocabulary in Drupal 8?

Since taxonomy term is an entity all the Terms for a specific vocabulary can be fetched using following entity query.

$query = \Drupal::entityQuery('taxonomy_term')->condition('vid', 'YOUR_VOCABULARY');
$result = $query->execute();
foreach ($result as $key => $value) {
      $result[$key] = Term::load($value)->getName();
}

$result can used for getting key value array of TID and Term Name.

How to attach JS library in a view in Drupal 8?

JS library to a view can be attached using hook_views_pre_render(). Use the following code to achieve it:

/**
 * Implements hook_views_pre_render().
 */
function your_module_views_pre_render(ViewExecutable $view) {
  if ($view->id() == 'YOUR_VIEW_ID' && $view->current_display == 'YOUR_VIEW_DISPLAY') {
    $view->element['#attached']['library'][] = 'your_module/your_library';
  }
}

How to remove a field from views output in Drupal 8

You can use hook_views_pre_render and change the value of the field in the following way. In this, I have removed field_image from 2nd row in the view. 

function your_module_views_pre_render(ViewExecutable $view) {
  if ($view->id() == "view_id") {
      foreach ($view->result as $key => $value) {
        if($key == 1 ) {
            $value->_entity->set('field_image', ['target_id' => 0]);
        }
      }
  }
}

 

How to attach jQuery in Drupal 8 Controller

jQuery file can be added to the controller in the following way:

In the controller, file make sure that library defined in libraries.yml is returned along with the markup,

return [
      '#type' => 'markup',
      '#markup' => '<div id="my_js_application"></div>',
      '#attached' => ['library' => ['example_module/example_library']]
    ];

In the libraries.yml, the example_library has to be defined.

example_library:
  version: 1.x
  js:
    js/myapp.js: {}

dependencies:
    - core/jquery

 

Now in myapp.js use the following syntax :

(function ($, Drupal) {

}(jQuery, Drupal));

 

What should be the NGINX settings for Drupal 7 website

Following NGINX server block can be used to run Drupal 7.

server {
        server_name YOURWEBSITENAME;
        root /var/www/html/drupal7;

        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        # Very rarely should these ever be accessed outside of your network
        location ~* \.(txt|log)$ {
                allow 127.0.0.1;
                deny all;
        }

        location ~ \..*/.*\.php$ {
                return 403;
        }

        location / {
                # This is cool because no php is touched for static content
                try_files $uri $uri/ @rewrite;
                expires max;
        }

        location @rewrite {
                # Some modules enforce no slash (/) at the end of the URL
                # Else this rewrite block wouldn&amp;#39;t be needed (GlobalRedirect)
                rewrite ^/(.*)$ /index.php?q=$1;
        }

        location ~ \.php$ {
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
                fastcgi_pass unix:/var/run/php-fpm.sock;
        }

}

Make sure you set correct value for "fastcgi_pass unix" as per PHP version installed in your system.

How to add content between view rows using twig in Drupal 8?

There is sometimes a situation in which you have to theme each and every element displayed in a different way. There isn't any twig to override in case you need overriding till row level. To solve this problem can you can use for loop and iterate using row variable.

{% for row in rows %}

  {% if loop.index == 1 %}

    <!-- Do Some Changes -->

  {% endif %}

  {% if loop.index == 2 %}

    <!-- Do Some Changes --> 

  {% endif %}

{% endfor %}