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
4
votes
5answers
4k views

How to programmatically create a title for a new node?

I want the title field of each new node of the Agenda content type to be constructed automatically from a date field in that same node. I have variously tried setting the #default_value and #value on ...
3
votes
1answer
914 views

Update a field without changing the node

I need to change a field in a node without altering the 'changed' timestamp. I know this was possible in Drupal 7 using field_attach_update(), but that has been removed since then. I could use an ...
2
votes
1answer
629 views

How can I exclude API keys, passwords, and so on from configuration management?

I am using Drupal 8 and drush cex to commit my configuration to my repository. However, I want to keep passwords and API keys out of the repository. How do I do that? (selectively exclude sensitive ...
1
vote
1answer
2k views

Make canonical URL in document HEAD absolute

I have a requirement to use absolute URLs for the canonical URL element generated by Drupal 8. hook_page_attachments_alter doesn't seem to have access the HTML element. Is there any other way I can ...
0
votes
2answers
1k views

On a content page that references a taxonomy term, how can I display only content that references that taxonomy term's immediate children?

In Drupal 8, I have a hierarchical taxonomy vocabulary called "Living Things" containing these terms: Animals --Fishes ----Blennies ------Yellow Blenny ------Green Blenny ----Gobies ------Blue Goby --...
40
votes
5answers
59k 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 ...
23
votes
4answers
965 views

With current D8 status, what's the decision tree for creating a new content entity type versus creating a Content Type for the “Node” content entity?

We have seen four years and Drupal 8's first release since the accepted answer was written for the question "When is it appropriate to create an Entity versus just adding a new content type?" And, ...
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 ...
15
votes
2answers
21k 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 ...
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 ...
43
votes
4answers
57k 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://...
23
votes
10answers
17k views

Change article date format

I'd like to change the date format that is used for articles in Drupal 8. It looks like it uses the Default medium date format by default. I added my own format at admin/config/regional/date-time, but ...
22
votes
4answers
33k 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 ...
14
votes
5answers
20k 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: .....
23
votes
6answers
27k 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\...
16
votes
10answers
19k 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 ...
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?
7
votes
1answer
11k views

Get referenced node fields in twig

In Drupal 8, I have a content type, that has an "entity_reference" field and I wish to access the referenced node's fields in twig template. the kint() output for the field is: In this list I can't ...
29
votes
3answers
18k 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?
19
votes
1answer
33k 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 ...
15
votes
3answers
9k views

Redirect Anonymous user to login page

I want if user not logged in and visit any page of my website except my REST VIEWS routers I redirect him/her to login page in drupal 8, I found this solutions for drupal 7 but didn't find any ...
14
votes
5answers
14k 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?
5
votes
1answer
16k views

Pass query string to Webform text field

I have a webform (www.mysite.com/myform) ad I want to be able to pass a URL string to a text field in the webform when a user visits the page and submits it. For instance, the URL provided to "...
20
votes
5answers
27k 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?
13
votes
6answers
25k 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?
11
votes
1answer
9k views

How to create custom entity in Drupal 8 [closed]

I have a big personal project build on Drupal 7 that has many custom entities. I want to start upgrading the project into Drupal 8 but the completly rewritten Drupal 8 core is too big for me to get my ...
10
votes
5answers
23k views

How can I programmatically render a node's field respecting the view mode settings?

I want to render a node's field inside a block. It works like this: <?php if ($node) { if (isset($node->field_body_secondary) && $field = $node->field_body_secondary->value) { ...
5
votes
1answer
7k views

Custom entity operation links

I've built a couple of custom entities in Drupal 8 Vendor, defined as such: /** * Defines the vendor entity. * * @EntityType( * id = "vendor", * label = @Translation("Vendor"), * module ...
2
votes
3answers
1k views

Updated >= Drupal 8.0.5+: Rendering forms/entities programmatically on theming level

There are several attempts and questions already asked about near to that topic in combination with the new Drupal core (8), but non of them came down to the bottom of the basic question. And non of ...
12
votes
1answer
10k views

How do I use a jQuery version different from the default one used by core?

I need to use a different jQuery version. Currently, Drupal is using jQuery 3.2.1, but I need a previous version. In Drupal 7 you could easily change jQuery version with the jQuery Update module. How ...
10
votes
4answers
5k views

Why doesn't “composer update drupal/drupal” work?

I installed a Drupal 8.2.0 site with composer create-project drupal/drupal testsite. Now I would like to upgrade it to Drupal 8.2.1. However, running composer update drupal/drupal doesn't work. It ...
8
votes
4answers
8k views

How can I embed a View inside a content of a node?

How can I embed a view inside a content of a node in Drupal 8? I found something similar, for example in the question about "How do I embed a view inside a node?" or the Viewfield module, but it is ...
8
votes
2answers
12k views

How do I redirect to an external URL?

Drupal 7 has drupal_goto() that would allow redirecting to an external url. The change record indicated that ControllerBase::redirect is the method to be called in D8. https://www.drupal.org/node/...
8
votes
1answer
5k views

Restrict content to a user role in a template file

I want to restrict a single node to anonymous users in Drupal 8. I don't want to use any module for it as its useless for single restriction as per me. I know that I can do it with something like ...
7
votes
2answers
11k views

How do I get a field translated in Drupal 8

I have a multilingual site in Drupal 8, and several node, some translated and others not. As I can programmatically get the translated field or by default they give it back from the original language. ...
6
votes
1answer
5k views

How do I theme a taxonomy term page?

I am working on Drupal 8, trying to get a page template for each vocabulary. I have tried using hook_theme_suggestions_HOOK_alter() to achieve this, but I was not able to do it. I tried using Devel ...
5
votes
4answers
7k views

Switch a language programatically

Looking for a way to change between languages on our site, the Drupal 8s LanguageManager doesn't seem to have a straightforward method for this. We cannot use URL-switching, just want to do this ...
4
votes
2answers
4k views

How to extend the basic user object upon installation?

I need to build a module to handle Users profiles, and need to extend automatically the basic Drupal user object upon installation. For Drupal 7 I found a way with: field_create_field($field); But ...
4
votes
2answers
6k views

how to get the original entity on hook_entity_presave?

i actually need it for the content_moderation_state entity function hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity) { if ($entity->getEntityTypeId() !== 'content_moderation_state' ...
4
votes
1answer
10k views

How to Create twig template files in drupal 8 for customizing user login,profile edit and forget password forms?

Earlier we use the below code for creating custom template of user login ,user profile edit and forget password forms: function Yourtheme_theme($existing, $type, $theme, $path){ return array( '...
3
votes
2answers
7k views

How to create a custom Twig Template for Menu Block

I'm using a menu block to output a section of my main menu into a region so I can have a "In This Section" type of sub menu on every page. Unfortunately, I can't find a way to pinpoint in menu.html....
3
votes
1answer
3k views

Programmatically append multiple paragraphs to entity reference field on node

I have a paragraph type that contains a text field and a text format field. I also have a content type that takes an unlimited number of paragraphs of this type. The paragraph type is called '...
11
votes
1answer
5k views

How to remove mymodule configurations on uninstall [duplicate]

I have created a custom module and created a yml file to create a menu system.menu.custom_menu.yml id: custom_menu label: 'Custom menu' description: 'Custom menu.' langcode: en locked: true ...
10
votes
4answers
23k views

Rendering an image field in page.html.twig with a custom image style

I'm trying to set up a slideshow that's unique to each page. The code for this needs to be in page.html.twig. I've got the slideshow mostly working, but I can only call the full image URL, not a ...
9
votes
4answers
6k views

How to Reduce/Increase module's weight on the module install process?

I'm developing one custom module and I want to increase my module weight when the module is installed. How can I achieve this? Otherwise, does anyone know in which table the module's weight is stored?...
7
votes
4answers
3k views

Cannot update: “No available releases found”

I cannot update some of the modules and the Drupal core itself from "Available Updates" menu. The error is "No available releases found". However, I can list the updates with drush ups. Why?
7
votes
4answers
6k views

How do I make a block that pulls the current node content?

Ok, so I know blocks in Drupal 8 have these things called "contexts" which allow Core to clear cache on a fine-grained basis. I can't find any decent explanation of how they work, or how to get page ...
5
votes
3answers
1k views

How to use helper/private methods with a .module file?

Let's say I use a hook with a lot of code inside. To improve readability and reusability I'd like to use helper/private methods. However, a Drupal .module file is not a class. The idea is to split ...
5
votes
1answer
3k views

Use a twig template instead of a render array in a custom module admin page

I have a Drupal 8 custom module, and I want to display its configuration page using the admin theme. Usually, for this kind of pages I use, in the _controller property of my route, rather : a class ...
4
votes
1answer
8k views

Render image field from node in page.html.twig

I make my first steps with Drupal 8. I have a site with a header image. On a specific content type "Blog" I want to replace the standard header image with a node specific header image. So in the ...

1 2 3
4
5
35