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.
15,791
questions
88
votes
11
answers
187k
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?
79
votes
11
answers
225k
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?
79
votes
4
answers
83k
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 ...
69
votes
6
answers
153k
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 ...
69
votes
9
answers
60k
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 ...
59
votes
14
answers
126k
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 ...
59
votes
7
answers
99k
views
How to get the base URL of a site
My site is on http://drupal8.local/. How do I get the drupal8.local part of that URL?
Url::fromRoute('<'current'>') or base_path() returns the path parth of the URL; For example, for http://...
55
votes
17
answers
125k
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)
...
55
votes
1
answer
33k
views
What are all the directories for?
Drupal 8 re-arranged all the directories and locations of modules/themes etc. Where do things go now?
54
votes
10
answers
72k
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?
51
votes
6
answers
98k
views
How to get instance of referenced entity?
I have a node object with image field named field_image. When I do
$node
->get('field_image')
->first()
->getValue()
I get array with some properties instead of entity object. The ...
50
votes
10
answers
91k
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 ...
48
votes
7
answers
75k
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 ...
46
votes
15
answers
44k
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 ...
44
votes
7
answers
94k
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?
44
votes
9
answers
99k
views
How do I translate strings in Twig templates?
I am working on a (my first) Drupal 8 project witch is a multilingual site. Drupal 8 really delivers in terms of native support for content translation.
However, I have a few strings set directly in ...
44
votes
9
answers
143k
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'...
43
votes
10
answers
99k
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 ...
42
votes
9
answers
88k
views
How can I get the path alias from a node ID or node object?
I have a node object, and I am trying to use it to get a given path in Drupal 8.
There was drupal_lookup_path() in Drupal 7 but I'm not seeing any equivalent or documentation for Drupal 8.
What's ...
42
votes
6
answers
38k
views
Programmatically create a term?
I'm attempting to add a lot of terms (~200) to a vocabulary, but I can't find any import modules that are updated for Drupal 8, and it seems the functions for doing this in Drupal 7 don't exist in ...
41
votes
3
answers
75k
views
Get the route name of the current page
The route name of the current page is available in page.html.twig?
The page is generated by the default feedback form.
40
votes
7
answers
23k
views
How do I update the configuration of a module?
I'm building a custom module in Drupal 8. It includes some YAML config files.
As I develop I need to change and add to the config, e.g. to add another field onto my custom entity.
At present the only ...
40
votes
4
answers
2k
views
What is the suggested workflow for migrating (CMI) configuration from dev -> stage -> production?
We had a drupalcamp a few months back and someone asked about managing deployments with the new config (CMI) system. One possible ideal workflow would involve keeping the config in version control and ...
39
votes
3
answers
88k
views
How do I get the user ID for the currently logged-in user?
I want to get current user object (user information) in Drupal 8.
I know in Drupal 7 there was a global $user variable; I want to know how I can get current user object in Drupal 8.
39
votes
4
answers
40k
views
Pass raw html to #markup
I have some html content, it comes from a external library so i can not change it, it has many <span> and <div> tags with attributes like:
<span style="color: #0000ff;">, but when i ...
38
votes
4
answers
26k
views
How do I re-run updates (change module schema version)?
I'm working on a module, and I changed a couple field types in the schema. I didn't take a backup before testing it and messed up the DB (it's just a dev site). Anyway, I reinstalled everything, and ...
37
votes
7
answers
71k
views
Get all nodes of given type
I would like to get all nodes that are of my_custom_type type in Drupal 8.
I know I can get all the nodes (of any type) with \Drupal\node\Entity\Node::loadMultiple() and the list of all types by \...
37
votes
5
answers
85k
views
Get path to theme assets in Twig template
I have a question about getting a path to an image in a Twig template. The image is not assigned to a field or something. Just a static image which is stored in "MYTHEME/image/icon/my-icon.png".
In ...
37
votes
11
answers
58k
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?
36
votes
10
answers
94k
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 ...
35
votes
8
answers
63k
views
Iterate through multiple value content field in Twig template
I need to take control over the rendering of a field_admin_tags field in a node.html.twig template.
This WORKS:
{{ content.field_admin_tags }} — Renders everything (label + all field values)
{{ ...
35
votes
3
answers
37k
views
Import a single yml configuration file
Is it possible to import one single configuration file by specifying its path, or a group of Yaml files contained in a specific module?
34
votes
5
answers
27k
views
How know if user is visitor or logged member?
How can I determine programatically whether the current user is anonymous or authenticated?
33
votes
2
answers
29k
views
How can I check if a module is enabled?
In Drupal 7, you can use module_exists() to check whether a module is enabled (as well as whether it exists).
For example,
if (module_exists('devel')) {
How can you check whether a module is ...
32
votes
4
answers
31k
views
How do I export/import views?
I know how to do it in Drupal 6/7, but I cannot find where to export/import views in Drupal 8. From which admin page is possible to do it, in Drupal 8?
32
votes
1
answer
35k
views
How can I get the roles for the currently logged-in user?
How can I get the roles for the currently logged-in user, in Drupal 8?
In Drupal 7, it could be achieved by accessing $GLOBALS['user']->roles;.
31
votes
8
answers
75k
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 ...
31
votes
7
answers
37k
views
How do I correctly setup caching for my custom block showing content depending on the current node?
I have this very basic block which just shows the current node's ID.
<?php
/**
* @file
* Contains \Drupal\mymodule\Plugin\Block\ExampleEmptyBlock.
*/
namespace Drupal\mymodule\Plugin\Block;
...
31
votes
4
answers
56k
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 ...
29
votes
5
answers
40k
views
Load term by name
In Drupal 7, we can load the term using name for ex. taxonomy_get_term_by_name($name)
Is there any way to load term via given name in Drupal 8?
29
votes
3
answers
78k
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().
...
29
votes
3
answers
60k
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']);
?...
29
votes
9
answers
25k
views
How do I alter the behavior of the form fields contained within the paragraph field?
Using the Paragraphs module in Drupal 8, I am attempting to alter the behavior of the form fields contained within the paragraph field. This is so I can add behavior like AJAX callbacks, hide show ...
29
votes
3
answers
8k
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 ...
29
votes
8
answers
12k
views
How do I migrate block content from a dev to a production site?
I've finally started looking at Drupal 8 seriously and I'm particularly interested in the configuration management. I've come across something that could be a little bit problematic and that's ...
28
votes
12
answers
74k
views
Get taxonomy terms
I wanted to retrieve taxonomy terms from a certain vocabulary in Drupal 8.
Apparently I can still do it using taxonomy_get_tree but it's deprecated.
I now have to use TermStorageInterface::loadTree
...
28
votes
7
answers
57k
views
How to get each field value in views-views-unformatted--view-machine-name.html.twig?
I have an twig file: views-view-unformatted--view-machine-name.html.twig
{#
/**
* @file
* Default theme implementation to display a view of unformatted rows.
*
* Available variables:
* - title: ...
28
votes
3
answers
33k
views
How do I pass parameters to the form builder?
I have the following routes in module_name.routing.yml.
module_name.usergroup_delete:
path: 'module_name/usergroup/delete/{arg1}'
defaults:
_form: '\Drupal\module_name\Form\DeleteUserGroup'
...
28
votes
11
answers
8k
views
How to prevent Devel module to be installed on production environments
Using the new Drupal 8 Configuration manager, how can I prevent it from installing the Devel module on certain environments? As far as I know, installing it on my local means the next time I export ...
27
votes
3
answers
39k
views
How to load a node by a field value?
I want to load a certain node. I don't know the id of the node but I do know that it has a certain hash field that is unique and I know that hash.
I want to load the node via the hash field. How can ...