Questions tagged [8]

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

19
votes
6answers
20k views

How do I render a form?

What is the alternative to drupal_get_form('user_login'). in Drupal 8? I am using the following code in theme_name.theme, but it doesn't work. // Include namespace for userLoginForm. use \Drupal\...
19
votes
2answers
17k 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 ...
19
votes
3answers
16k views

How do I render nodes, now that node_view() has been deprecated?

In Drupal 7 I frequently use node_view() for rendering nodes (using view modes) in blocks or pages as follows: $nids = array(123,456,789); $nodes = node_load_multiple($nids); foreach ($nodes as $node)...
19
votes
4answers
6k views

When should I use Backdrop CMS rather than Drupal 8?

From what I've heard, Backdrop is basically an alternative to Drupal 8. Is deciding which one to use simply a matter of preference and ideology, or are there specific use cases for each? If I'm ...
19
votes
1answer
29k views

Untrusted Host “localhost” in [duplicate]

From Drupal 8 beta 6 (released today), I have this error : Error message UnexpectedValueException: Untrusted Host "localhost" in Symfony\Component\HttpFoundation\Request->getHost() (line 1221 of ...
18
votes
6answers
28k views

Get path alias from NID (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 ...
18
votes
3answers
21k 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 \...
18
votes
3answers
11k views

Non-Clickable Menu Items

In a Drupal 8 site, I want to add menu items with "no link", I was thinking that nolink is allowed but when I entered this, I got error Manually entered paths should start with /, ? or #. Is ...
18
votes
7answers
16k views

How do you implement a breadcrumb?

I've tried to define a new breadcrumb override, but I'm still getting the site default. I've created a custom module, foo_breadcrumb: - modules/custom/foo_breadcrumb - foo_breadcrumb.info....
18
votes
3answers
9k views

Import a single yml configuration file

Is it possible to import one single yml configuration file by specifying its path? Or a group of yml files contained in a specific module?
18
votes
1answer
4k views

How can I change the active theme programmatically?

How can I change the active Drupal 8 theme programmatically? In Drupal 6, we used the following code. global $custom_theme; $custom_theme = 'garland'; In Drupal 7, we used hook_custom_theme(). In ...
18
votes
5answers
14k views

How to filter a View by Date Range (start, end)?

In Drupal 7 with Views an Date module enabled, it was pretty easy to filter a view by Date: The UI just gave you the option to do so. In Drupal 8, however, the date field and views are part of core, ...
17
votes
5answers
15k views

How to show a date in the correct timezone?

I have a Datetime Range field (field_date) in a content type. Once I create my content type I set the Start Date as: 2017-02-27 19:30:01 Now I want to get the value and show the date in other format,...
17
votes
1answer
3k views

What are the equivalent functions that I should use for HTTP handling?

Looking at the functions listed in the HTTP handling page for Drupal 7, I noticed that the following functions don't exist anymore in Drupal 8. (The links are for the Drupal 7 documentation pages, ...
17
votes
3answers
833 views

How to install a module which includes a different version of Symfony?

I am a developer and maintainer for the CiviCRM project. We've been trying to make a Drupal 8 version CiviCRM, and have a come a long way. We're beating our heads against our collective keyboards ...
16
votes
7answers
17k 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; ...
16
votes
4answers
12k views

Access user fields?

$account = \Drupal::currentUser() loads the user object into $account, but how do I access custom fields I've created on the user object (via the admin UI)? With dpm($account) I can't see any of my ...
16
votes
3answers
20k views

How do I programmatically render a view with contextual filters?

How do I programmatically render a view with contextual filters in Drupal 8? In Drupal 7, I generally use the following code. $view = views_get_view('test_view'); //test_view is the view machine ...
16
votes
7answers
7k views

Exclude configuration from import/export

I thought this was a simple use case of the new Configuration management system, but I haven't had any luck finding out how to solve this: Problem I want to store configuration in git and use drush ...
16
votes
5answers
18k views

Programmatically updating a node

I can create a node using the following code: $node = \Drupal::entityTypeManager()->getStorage('node')->create($array); But if I have the node ID, how can I edit a node?
16
votes
9answers
10k 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 ...
16
votes
4answers
20k 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 ...
16
votes
5answers
10k views

How do I perform a redirect to a custom URL after login?

I am trying to redirect users to a custom URL after they logged in, but my code doesn't work. How do I redirect users after they logged in?
16
votes
3answers
3k views

“Drupal calls should be avoided in classes, use dependency injection instead”

In my module using below code for getting url alias of given url: $alias = \Drupal::service('path.alias_manager')->getPathByAlias($_POST['url']); But one I run Automated Review(http://pareview.sh/...
16
votes
3answers
7k 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 ...
15
votes
3answers
32k 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(). ...
15
votes
2answers
15k 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' ...
15
votes
2answers
20k views

Redirect after form submission

I create custom Form in Drupal 8 ,I remember we can redirect to custom path in Drupal 7 with set $form_state['redirect'] = 'mycustompath'; in custom submit handler, but how can redirect User after ...
15
votes
2answers
6k 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

Render plain text as HTML in Drupal 8 twig

I've run into a wall trying to render a svg image markup code into html from a plain text field. I'm looking to render the svg code from a plain text field without overriding the plain text field ...
15
votes
3answers
27k 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
9answers
14k views

Add class to content field (link)

I want to add a class to the <a> tag of a field that consists of a link and text. (It's a field of type Link.) The name of the field is content.field_c_button_link. In the template file, I ...
15
votes
2answers
25k 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
1k views

What is the difference between “Language: Not specified” & “Language: Not applicable”?

What is the difference between Language: Not specified and Language: Not applicable in Drupal 8 In the context of multilinguality ? There was no such distinction in Drupal 7 (and 6, IIRC) and it ...
15
votes
2answers
3k views

Send email notification

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
313 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
9answers
13k views

How do I set the theme logo?

How can I set the logo in my custom theme in Drupal 8? I can see that Drupal looks for the logo.svg file. I have a PNG logo.png file that I used in Drupal 7 and I would like to re-use it. I cannot ...
14
votes
4answers
17k views

Verify a field is not empty

What is the best method to check if a field is empty in a node template. In node--example.html.twig we can use "content" variable. I can't use check like checking a block {% if content....
14
votes
5answers
19k 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: ...
14
votes
4answers
11k views

Check if the user is logged-in in a theme

In Drupal 7 we can simply check if the current user is logged-in in a theme by checking $GLOBAL['user']->uid or using user_is_logged_in(). How can I check if a user is logged-in in a page ...
14
votes
4answers
16k 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
5answers
6k views

Release cron lock

I ran cron and aborted the execution. Now I get this message: Attempting to re-run cron while it is already running. In Drupal 7, I could delete the cron_semaphore variable. What's the way to ...
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
1answer
11k views

Currently logged in user's roles

How to get the currently logged in user's roles in Drupal 8 ? For example in D7 we have $GLOBALS['user']->roles;
14
votes
2answers
9k views

How can I get the nid of a node from the page alias?

I want to be able to get the nid but the only information I have is http://example.com/this-is-the-alias. Is there a drupal function to get either the internal path of /node/* for that alias if I am ...
14
votes
1answer
8k 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
3k 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
1answer
4k 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
1answer
2k 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 ...
14
votes
1answer
6k 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. ...