Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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
45 votes
9 answers
153k views

How do I get the raw field value in a twig template?

myfield is a plain text field. {{ content.field_myfield }} renders the field with HTML markup and the field label. How do I render the raw value of the field? {{ content.field_myfield.value }} doesn'...
Pim's user avatar
  • 1,055
45 votes
10 answers
104k views

How can I programmatically display a block?

I'm developing a site using Drupal 8 beta-14. I've created a view block of different terms and now I want to display it using code. How can I display it programatically? I used to do it in Drupal 7 ...
rashidkhan's user avatar
62 votes
14 answers
136k views

How to get the valid URL of a Link field from within a Twig template?

I have a Link field named field_my_link (machine name). Within a Twig template I can get the link's URL value with this: {{ node.field_my_link.uri }} If inside the Link's URL there is an external ...
Andrea's user avatar
  • 1,267
81 votes
4 answers
87k views

What does "The provided host name is not valid for this server" mean?

I am running a Drupal 8 website, and when I browse to any page, I get a plain white page with just the following error message on it. The provided host name is not valid for this server. What does ...
29 votes
3 answers
62k views

Programatically render a block in a twig template

I need to render a views block in my page.html.twig template. In D7 I would do this: <?php $block = module_invoke('module_name', 'block_view', 'block_delta'); print render($block['content']); ?...
Rick Bergmann's user avatar
6 votes
2 answers
11k views

How to safely render node body on a custom variable?

I created a custom template for my programmatically created block. I defined a variable for the template called test so in my render array I have '#test' => $node->get('body')->value ...
No Sssweat's user avatar
  • 31.3k
29 votes
3 answers
82k views

Add a custom submission handler to a form

How can I add a custom form submission handler? I tried adding $form['#submit'][] = 'mymodule_form_submit'; or $form['actions']['submit']['#submit'][] = 'mymodule_form_submit'; to hook_form_alter(). ...
Yuseferi's user avatar
  • 22k
19 votes
7 answers
45k views

How do I programmatically log in a user with a POST request?

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.in ...
niksmac's user avatar
  • 7,692
46 votes
15 answers
46k views

How do I remove a configuration object from the active configuration?

I'm writing a custom module for Drupal 8. It defines a field storage configuration object, in a file called modules/mymod/config/install/field.storage.bloom_stem.bs_twitter_ac.yml. The problem is ...
artfulrobot's user avatar
  • 4,019
29 votes
3 answers
9k views

How can I programmatically change the active theme?

How can I programmatically change the active Drupal 8 theme? In Drupal 6, we used the following code. global $custom_theme; $custom_theme = 'garland'; In Drupal 7, we used hook_custom_theme(). In ...
visabhishek's user avatar
  • 1,346
19 votes
19 answers
34k 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?
johnchan's user avatar
  • 371
16 votes
3 answers
26k views

How can I implement AJAX form submission?

My task is to submit a contact form via AJAX and then to show a "Thank you for submission!" message, loaded in place where the form was. So I need to ajaxify existing contact form. I found ...
Sergey Kravchenko's user avatar
9 votes
2 answers
8k views

Change a normal submit 'input' type to 'button' type with button tag

Is it possible in Drupal 8 to change a regular form submit button from Drupal into a button tag? This is the output I need to achieve: <button type="button" class="search-box__button"> <...
Kevin's user avatar
  • 27.9k
71 votes
9 answers
63k views

How can I import the configuration on a different site?

I have this error when I try to import the config of an another site: The staged configuration cannot be imported, because it originates from a different site than this site. You can only ...
Axel Briche's user avatar
  • 1,971
69 votes
6 answers
161k views

How do I get the current node ID?

In Drupal 7, if I wanted to get the node id of the currently displayed node (e.g. node/145) I could get it with the arg() function. In this case, arg(1) would return 145. How can I achieve the same ...
dbj44's user avatar
  • 4,764
24 votes
7 answers
58k views

Get the image URL

I want to render an image as a background image via an inline style in twig. I created a field called bg_image and attached it to the standard plain page. After fiddling around for hours I was able ...
Jannis Hell's user avatar
20 votes
5 answers
8k views

Order of drush commands for automated deployment?

What order should the following drush commands be ran? config-import updatedb entity-updates Also, I see entity-updates fail a lot due to field_delete_data* tables existing. How can I delete them as ...
Paul Canning's user avatar
17 votes
6 answers
20k views

How to embed drupal content in other sites (remove X-Frame-Options: SAMEORIGIN)?

I tried to make a iframe with a drupal page but I received the following message: Multiple "Refused to display (page adress) in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'." Then, I ...
Victor Pereira's user avatar
14 votes
4 answers
20k views

Add content type field programmatically

My module provides a configuration form which allows to select a content type and on submit 2 new fields will be added to the selected content types. I want to add these fields to the content types ...
Kpone's user avatar
  • 503
48 votes
7 answers
77k views

How can I programmatically create a node?

How can I programmatically create nodes with date and image fields? I know I can do it in Drupal 7 with the following code. global $user; $node = new stdClass(); $node->title = "YOUR ...
Yuseferi's user avatar
  • 22k
44 votes
7 answers
98k views

How to get $_POST and $_GET parameters

I call a route via AJAX and pass data to it. In a controller, I want to get $_POST['var'], but it does seem to work on Drupal 8. How I can get values contained in $_POST from a controller?
Yuseferi's user avatar
  • 22k
38 votes
11 answers
60k views

How can I change the page title?

In Drupal 7, I could change the page title with drupal_set_title(t('New page title')). When I try it in Drupal 8, I get a There is no such function error. How can I change the page title?
Jeff Madison's user avatar
21 votes
3 answers
23k 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 ...
apaderno's user avatar
  • 97.5k
13 votes
5 answers
19k views

How do I programmatically update the allowed values of a list field?

I have created a field_time_zone field for the Article content type. I want to set the allowed values for that field to the values returned from system_time_zones(). For this, I implemented ...
Sugandh Khanna's user avatar
12 votes
5 answers
34k views

How to print paragraphs in twig template?

I have a content type, basic, where i have added a Paragraph field field_pr that has two fields: field_text, field_color. I have a twig template node--basic--full.html.twig where I want to print the ...
usernameabc's user avatar
  • 2,726
80 votes
11 answers
233k views

How do I access a field value for an entity (e.g. node) object?

node_load((int) $nid) loads a fully-populated node entity. $node->title->value gets the title, but how do I access field values in Drupal 8?
dbj44's user avatar
  • 4,764
54 votes
10 answers
73k views

Manually disable a module

In Drupal 7, I could manually edit the {system} table in the database to disable a stubbon module. In my Drupal 8 site, this table is gone. How can I manually disable a module in Drupal 8?
bumpaw's user avatar
  • 738
20 votes
6 answers
18k 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, ...
Nic's user avatar
  • 1,215
11 votes
5 answers
41k views

Get current language in template

In Drupal 7 the current language is a global variable we can access with global $language; $langcode = $language->language; I want to know how can we access the current language in a Twig file ...
Yuseferi's user avatar
  • 22k
8 votes
5 answers
2k views

How to make use of Devel debugging functions on large or complex objects

I am having trouble making real use of Devel's debugging functions kint() and dpm() in my modules and twig templates. They work fine for primitive variables, flat arrays and such, but if I want to ...
Hendrik's user avatar
  • 365
6 votes
3 answers
14k views

What is easiest way to debug variables in nodes, views, blocks and page twig templates?

What is easiest way to debug variables in nodes,views,blocks and page template. Earlier we used devel module in drupal 7 ,by using dpm() function it shows very neat and clean structure ,it was so ...
user avatar
5 votes
1 answer
2k views

Disabling Page Cache for Redirect Response

I've implemented a conditional redirect on my site via an event subscriber and returning a redirect response. It's working but, I believe due to the page_cache module is getting cached for anonymous ...
Vecta's user avatar
  • 603
4 votes
2 answers
4k views

How to render specific form elements from the *.theme file

I am using Drupal 8 and would like to customize how form elements are being displayed. Specifically, I don't like how uneditable, populated textfields are displayed as plain text. I would have it ...
user5013's user avatar
  • 1,497
88 votes
11 answers
197k views

How do I get the current path alias or path?

How can I get the current alias and/or path in Drupal 8? In Drupal 7 I did this with drupal_get_path_alias(). What is the equivalent code I should use in Drupal 8?
Yuseferi's user avatar
  • 22k
51 votes
9 answers
94k views

How do I enable developer/debugging mode?

I'm installing Drupal 8 core (using the standard profile). Whenever I get an exception/error, I get a blank page with "The website has encountered an error. Please try again later." (As ...
vijaycs85's user avatar
  • 2,394
37 votes
10 answers
98k views

How do I create a link?

Say I am extending The blockBase class to create a custom block, and I am implementing the blockBuild method to create some markup for my block. Something like this: class YourModuleBlock extends ...
dbj44's user avatar
  • 4,764
10 votes
1 answer
6k views

drush/drush dev-master requires codegyre/robo ~1.0.0-beta1

I'm transitioning a Drupal 6 site to Drupal 8.1 so following the official instructions, I set up a new install in its own directory. Since this is only one site, I followed the instructions to put my ...
sam452's user avatar
  • 451
9 votes
2 answers
10k views

Override theme template from module without implementing a theme

I have a project on D8 where I don't use any custom themes, I use default seven theme for everything because there is no need of any visual theming. So My question: Is it possible to override theme ...
otarza's user avatar
  • 954
3 votes
1 answer
4k views

Custom registration twig template

What would be the best way to make a custom registration twig template ? I have implemented the hook : function THEMENAME_theme_suggestions_form_alter(array &$suggestions, array $variables) { ...
Aporie's user avatar
  • 355
55 votes
17 answers
129k views

Getting the image URL from a field_image on a node

So I have this Node: object(Drupal\node\Entity\Node)[1862] protected 'values' => array (size=17) 'vid' => array (size=1) 'x-default' => string '7' (length=1) ...
Rias's user avatar
  • 723
31 votes
8 answers
78k views

How do I add a custom validation handler to an existing form/field?

How do I add a custom validation handler to an existing form (or form field) in Drupal 8? I have a form that I haven't created. I want to add my own validation rules on some fields when the form is ...
AngularChef's user avatar
  • 1,005
23 votes
3 answers
29k views

Get all fields defined in a bundle

In Drupal 7, to get all fields used in a content type, I use the following code. $fields = field_info_instances('node', 'article'); What is the equivalent for Drupal 8?
Pravin Ajaaz's user avatar
  • 3,542
20 votes
9 answers
47k 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?
Anu Mathew's user avatar
17 votes
3 answers
9k views

KernelEvents::REQUEST is not fired on cached pages

I need to implement an event subscriber to perform some actions when the page is loaded. The event should be fired whether the requested page exists in the Drupal cache or not. It seems that the ...
15 votes
8 answers
15k 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. /** * ...
user avatar
15 votes
2 answers
8k 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.
10 votes
3 answers
8k views

How do I load a form with a specific form display (form mode)?

On a custom page I'm trying to load an user edit form with a specific form display. I'm able to load the form however I can't figure out how to load the form with the specific display. This is what I ...
Tijmen's user avatar
  • 101
10 votes
4 answers
7k views

Anonymous user cache control

We have written some custom blocks that render dynamic content; unfortunately, the caching defaults cache the first render of this block (and don't refresh for anonymous users). Cache control seems to ...
gavz's user avatar
  • 201
10 votes
6 answers
22k views

How do I programmatically place a block?

From UI you can add a block in a region in that way: Go to Admin -> Structure -> Block layout (/admin/structure/block). Click on "Place block" button besides the region name. I want to do the ...
user3554770's user avatar
33 votes
4 answers
58k views

Programmatically update an entity reference field

I know I can update (or set a value for) an entity reference field in Drupal 7 with code similar to the following. $node['field_code_used_by'][LANGUAGE_NONE][0]['target_id'] = $id; I want to ...
user avatar

1
2 3 4 5
42