Questions tagged [8]

Version tags should be used for questions that apply to a version only, not to merely say what version a site is using.

Filter by
Sorted by
Tagged with
15
votes
2answers
7k views

How to create a empty module? [closed]

What is the most basic valid module one can create in Drupal? It would be useful to have a simple blueprint available, for anyone just getting started with Drupal.
15
votes
3answers
19k views

How can I implement AJAX form submission?

My task is to submit contact form via AJAX and then to show "Thank you for submission!" message, loaded in place where the form was. So I need to ajaxify existing contact form. I found some examples ...
15
votes
3answers
9k views

What is the command that replaces 'drush variable-set'?

In Drupal 8 the Drush command drush variable-set no longer works. From reading (here) it appears that Drupal 8 has a new "configuration" system that replaces the old (Drupal <=7) "variables". But ...
15
votes
3answers
18k views

How do I show the current node title in page.html.twig OR in node.html.twig?

If I want to show the current node title in page.html.twig (by default it seems to only be available in node.html.twig) I have to create a preprocess function like so: function theme_preprocess_page(&...
15
votes
3answers
28k views

How do I access field data in Twig?

I have a list field for a custom block type that looks like this: left|Align left right|Align right I can access the key in Twig by doing something like this: {% block content %} {{ content....
15
votes
4answers
21k views

How do you implement a custom submit handler in hook_form_alter()?

I've been updating one of my Drupal 7 modules to 8 and I'm having an issue with my form alter. I'm adding a validation function and a submit handler. The validation function is getting called just ...
15
votes
1answer
4k views

How do I use dependency injection on \Drupal::moduleHandler() to a Controller?

I am using the following line in a controller class. \Drupal::moduleHandler()->alter('mymodule_myfunction', $plugin_items); Pareview.sh is giving me this error message. \Drupal calls ...
15
votes
2answers
31k views

How do I change the base URL?

In Drupal 7, we can quickly change the base URL of the website. How can I do this, in Drupal 8?
15
votes
1answer
7k views

Implementing Custom Cache Tags

I would like to use the new Cache Tagging in D8 to help keep related content of different types (entities, nodes, blocks, etc) in sync. But from reading the currently available documentation of this ...
15
votes
2answers
4k views

Send email notification when a new comment is posted

I'm using Drupal 8.1 with Rules 8.x-3.0-alpha1. I want to create a rule to send an email to the author of a node when a new comment is posted to this node. Problem is I didn't get the email address ...
15
votes
3answers
369 views

How do I deploy/manage similar sites from an unique profile, without dumps?

I do not like the "cloning website" solutions which implies dumping a database and importing this dump in another environment. This does not look like a real world way of deploying several instances ...
14
votes
18answers
20k views

Redirect after user login

I would like to redirect users after they log in. Is it possible to use hook_user_login() to make the redirection? How do I add the parameter for the redirection?
14
votes
5answers
27k views

Check if it's the front page in the template

Im trying to show content when I'm on the front page of my Drupal 8 test website, but the content shows on every page: {% if front_page %} <header role="banner"> {% if logo %} <a ...
14
votes
5answers
19k views

How do I disable Twig and block cache for a specific module?

I'm working on a module in Drupal 8 which retrieves time sensitive data from an external REST api. The data is processed and forwarded to a Block. The final data is sent to a Twig template like so: .....
14
votes
3answers
26k views

How to create a redirection programmatically? [closed]

I try to create a redirection 301 with the redirect module. For example, I want to redirect an URL source to an internal URL through my custom module. I want to create a redirection when I create a ...
14
votes
4answers
12k views

Programmatically redirect users to the default “access restricted”

I would like to programmatically redirect my users to the default "access restricted" page. I know that I need a "RedirectResponse" object for a simple redirect, but how do I specify the default "...
14
votes
3answers
16k views

How do I create menu links programmatically?

I need to add some links to existing menu (or to new) programmatically in Drupal 8.
14
votes
2answers
7k views

Should I learn Symfony 2?

I saw this, How much of Drupal 8 will rely on or be compatible with Symfony? but I don't understand if it applies to my question. Should I learn Symfony 2 to develop with Drupal?
14
votes
3answers
14k views

How do I alter the route defined by another module?

In other words, what is the Drupal 8 equivalent of hook_menu_alter()? Drupal 8 still uses hook_menu(), but for what I can see, the information returned by the hook is different from what the hook ...
14
votes
3answers
31k views

Perform a query with an entity field condition with multiple values

I have a content type that has a entity reference field that allows users to add multiple taxonomy terms in that field. I'm attempting to perform queries that grab nodes that have a specific set of ...
14
votes
2answers
9k views

How to get the human readable content type name in a node template

I have created node template for my node, and its working fine. I need to print content type name, which is like "Whitepaper" and has machine name "w1" in my node template. If I use variable {{ node....
14
votes
1answer
10k views

How do I translate a view page title?

Is there a way to translate a view page title in Drupal 8? How can I achieve it, if it is possible?
14
votes
4answers
17k views

Get the value of a custom user field

I have a custom form that needs to display a default value from a custom user field. The field was created under /admin/config/people/accounts/fields and its machine name is field_r_period_length_sec. ...
14
votes
4answers
4k views

Delete a node after X weeks it was created

How can I delete a node after, for example, 2 weeks it has been created?
14
votes
2answers
6k views

KernelEvents::REQUEST is not fired on cached pages

I am trying to implement a KernelEvents::REQUEST event subscriber to perform some action on page load. I need this event to fire regardless of whether the requested page exists in the Drupal cache - ...
14
votes
2answers
8k views

What is the purpose of drush entity-updates?

After updating Drupal 8 modules, I have been warned on the Drupal 8 status page that: Entity/field definitions: The following changes were detected in the entity type and field definitions. ...
14
votes
1answer
5k views

How to make a theme hook suggestion for blocks according to region?

Since Drupal 8 it is not possible with template naming conventions to load a specific block template according to the region it is placed in ("Per-region block markup and per-region block templating ...
14
votes
2answers
4k views

Multilingual content and single language admin interface

I want my site's content to be multi-lingual. I have a language switcher that can toggle between both the English and Spanish translations of a page. I want the administrative interface to always be ...
13
votes
6answers
24k views

What function/method can I use to redirect users to a different page?

In Drupal 7, I use the following code. function my_goto($path) { drupal_goto($path, array(), 301); } What code should I use in Drupal 8?
13
votes
7answers
29k views

User login REST format

Do anyone have a working REST Login on Drupal 8? This is what I tried. POST /user/login HTTP/1.1 Host: 8.d8.local Content-Type: application/x-www-form-urlencoded Accept-Encoding: http://nikhilmohan....
13
votes
6answers
10k views

How to redirect an anonymous user to the login form after a 403 error?

I want to redirect an anonymous user to the login form if such user encounters a 403 error. I have created event subscriber and this is my code, but I end up on loop on the current page. /** * ...
13
votes
3answers
8k views

REST GET without caching

Using this tutorial Drupal Console's drupal gprr command, I've created a custom REST resource in Drupal 8. I'm able to use this resource to retrieve a simple JSON object that says hello to the current ...
13
votes
5answers
10k views

How can I control block visibility with code?

In Drupal 6, I could control block visibility using PHP code in the block UI configuration like: <?php return MYMODULE_abc() || MYMODULE_def() ; ?> These functions looked up the current node, ...
13
votes
2answers
15k views

Get the real path of a file

I am creating an Excel reading module in Drupal 8. I want to get the real path of a file for reading it from a path like public://2016-03/Places.xls. What function should I call for getting the real ...
13
votes
2answers
20k views

Render a node field inside page.html.twig

I have an image field on my content type (field_hero_image). I don't want it to render in the node.html.twig file. However, I DO want to render it in the page.html.twig file. Specifically, I want to ...
13
votes
6answers
15k views

How to embed a view in a specific node?

I try to embed a view in a specific node. I found tutorials for doing this with Drupal 7 and PHP-Templates: <?php print views_embed_view('view_name'); ?> But I look for a way in Drupal 8 with ...
13
votes
6answers
23k views

How do I add a CSS class to a form label?

I want to add a CSS class to a label on a page that is using Drupal 8 Form API. I have been unable to find a reference online on how to apply that. I have used the following workaround, but it ...
13
votes
2answers
10k views

How to make custom fieldset collapsed by default in Drupal 8

In Drupal 7 there was option to have fieldset collapsible or not, and to restrict default state of this form element (collapsed, or uncollapsed). Example: $form['contact_data'] = array( '#...
13
votes
3answers
7k views

How to disable aggregation from either drush or PHPmyadmin?

I've been searching for this and ALL I found deals with Drupal 7. Old drush command drush vset preprocess_js 0 doesn't work for Drupal 8 and the "variable" table in DB doesn't exist in 8... Yet, I ...
13
votes
4answers
8k views

How to access a selected label instead of value

I have a select list of states: AL|Alabama AK|Alaska AZ|Arizona AR|Arkansas In code to access the selected state value I can run code like this: ... $entity->get('field_state')->value; ... I ...
13
votes
2answers
11k views

How can I add items to the Admin Toolbar?

In Drupal 8, I want to add a menu item to the Admin Toolbar with a submenu, with different links. How can I do it?
13
votes
3answers
18k views

Check content type of node in preprocess hook

I have a series of variables I want to make available on twig templates for specific content templates. In MYTHEME.theme I have: function MYTHEME_preprocess_node(&$variables) { if (isset($...
13
votes
3answers
9k views

Getting the node object in hook_form_FORM_ID_alter

Follow up to this question but I don't have enough reputation to ask clarification in a comment. I am porting a contributed module to Drupal 8. The sugested code on the above mentioned CR and post ...
13
votes
4answers
13k views

How can I get the page title?

drupal_get_title() has been removed from Drupal 8. How I can get the page title in Drupal 8?
13
votes
1answer
5k views

Load a file entity by URI

Is there a way to load a file by URI in Drupal 8 or Drupal 9? In Drupal 7 it is possible by the next example: $uri = 'public://file.xyz'; // Take a look at: file.inc::file_load_multiple $files = ...
13
votes
3answers
13k views

How can I setup multiple database connections?

Can anyone help me to setup connection with multiple databases connection in Drupal 8? I have database on the same server and I want to access it along with default Drupal 8 database.
13
votes
3answers
5k views

What versions from the Symfony components are used from core?

Which Symfony version does Drupal use for the Symfony components? Is it a goal to support the latest versions of the components or is this simply not possible?
13
votes
2answers
18k views

How do I create table in custom module

In Drupal 7 we used to use hook_schema() in the .install file to create a table. I believe that, in Drupal 8, you can do that as well, but I was under the impression that you should create .yml files ...
13
votes
1answer
7k views

How do clean_class and clean_id work?

I am new to Drupal 8 and Twig syntax. I am not able to understand how its working filter clean_class "region|clean_class"? What is clean_class here? I have added following code in my page.html.twig ...
13
votes
5answers
4k views

How can I deploy user interface translation?

Strings that I translate at "User interface translation" /admin/config/regional/translate are not exportable by "Configuration management" /admin/config/development/configuration for me. Am I missing ...