Skip to main content

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
88 votes
11 answers
199k 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
  • 22.1k
81 votes
4 answers
88k 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 ...
80 votes
11 answers
235k 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
73 votes
9 answers
64k 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,991
69 votes
6 answers
162k 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
62 votes
14 answers
138k 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
62 votes
7 answers
107k 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://...
Jasodeep Chatterjee's user avatar
55 votes
17 answers
130k 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
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
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
53 votes
6 answers
105k 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 ...
SiliconMind's user avatar
  • 3,211
51 votes
9 answers
95k 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
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
  • 22.1k
46 votes
15 answers
47k 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,029
45 votes
10 answers
105k 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
45 votes
9 answers
107k 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 ...
Henkealg's user avatar
  • 629
45 votes
9 answers
155k 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
44 votes
7 answers
99k 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
  • 22.1k
44 votes
9 answers
97k 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 ...
Jack Ryan's user avatar
  • 2,307
44 votes
3 answers
83k 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.
Oana Hulpoi's user avatar
  • 1,418
42 votes
6 answers
39k 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 ...
Samsquanch's user avatar
41 votes
4 answers
43k 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 ...
Yukare's user avatar
  • 541
41 votes
7 answers
24k 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 ...
artfulrobot's user avatar
  • 4,029
40 votes
5 answers
29k 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 ...
wizonesolutions's user avatar
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 ...
btmash's user avatar
  • 568
39 votes
3 answers
94k 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.
Yuseferi's user avatar
  • 22.1k
38 votes
13 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
37 votes
10 answers
99k 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
37 votes
7 answers
76k 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 \...
PolGraphic's user avatar
  • 1,100
37 votes
5 answers
89k 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 ...
Yetispapa's user avatar
  • 697
36 votes
8 answers
67k 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) {{ ...
AngularChef's user avatar
  • 1,005
36 votes
5 answers
41k 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?
David Riccitelli's user avatar
35 votes
2 answers
31k 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 ...
Patrick Kenny's user avatar
34 votes
5 answers
28k views

How know if user is visitor or logged member?

How can I determine programatically whether the current user is anonymous or authenticated?
Axel Briche's user avatar
  • 1,991
33 votes
4 answers
59k 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
33 votes
1 answer
39k 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;.
graceman9's user avatar
  • 672
32 votes
4 answers
33k 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?
albo's user avatar
  • 697
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
31 votes
7 answers
39k 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; ...
Alex's user avatar
  • 2,523
29 votes
12 answers
80k 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 ...
Romain Braun's user avatar
29 votes
5 answers
42k 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?
Amit Sharma's user avatar
29 votes
3 answers
83k 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
  • 22.1k
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
29 votes
3 answers
35k 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' ...
KTM's user avatar
  • 2,362
29 votes
10 answers
28k 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 ...
bart_88's user avatar
  • 469
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
29 votes
8 answers
13k 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 ...
user avatar
28 votes
7 answers
59k 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: ...
Sugandh Khanna's user avatar
28 votes
3 answers
41k 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 ...
Andrew's user avatar
  • 731
28 votes
8 answers
25k views

Taxonomy term name as contextual filter for view

In D7 I was able to filter a view according to a taxonomy term name taken from the URL. An example would be this URL http://www.gbyte.co/tags/drupal-planet/rss.xml "drupal-planet" would be converted ...
gbyte's user avatar
  • 787

1
2 3 4 5
316