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.

2
votes
3answers
4k views

Load external php library in custom module

I’ve read a lot of topic on this but i can’t figure why it is not working for me. I’ve built a custom module. It is working fine since return $output; in my controller class is working : I can see in ...
2
votes
1answer
2k views

Adding custom block problems

I've created my own module in drupal 8. I also created block in my module. I tryied to add my custom block (which is type of custom form) to view. But when I am trying to add custom block in view, its ...
1
vote
1answer
2k views

Custom variable in custom block template

I'm trying to create a block that will pass a variable to its template. For now just some text but it will be an array later. The default block variables such as label are working but I'm unable to ...
1
vote
2answers
1k views

Programmatically create a node with a menu link

I created a node programmatically with the following code. $data = array( 'type' => 'inception_article', 'title' => $msg->name, 'body' => array( 'value' => $msg->body, ...
1
vote
1answer
1k views

How to display a field twice in a view mode?

I'd like to display a field_address in a view mode twice, once as the displayed address and once as a google maps directions link. I suppose I could create one custom field formatter that outputs ...
1
vote
2answers
95 views

file pattern name for content type

What is Drupal 8 file pattern name for a specified content type? Suppose I have a Movie content type. What is the pattern name for its page? Is it page--node--movie.html.twig?
1
vote
2answers
438 views

Adding script conditionally

In the module I am developing I need to add script which require jQuery.The will be added depending on configuration settings. Hence I am using template_preprocess_module(&$variables) where I am ...
0
votes
2answers
647 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 --...
0
votes
1answer
423 views

How do I grant to roles dynamically created permissions?

In my module, I am dynamically creating user permissions for vocabularies. I would like to also grant all roles the view permission, but I cannot figure out a proper way where to do it. I cannot use ...
0
votes
2answers
5k views

How do I render a Twig template and load a CSS file from a controller?

Is this considered a good practice for rendering a Twig template and loading a CSS file from a controller? DefaultController.php namespace Drupal\acme\Controller; use Drupal\Core\Controller\...
0
votes
1answer
1k views

How do I update core to 8.5?

After I realized that the thing I mostly enjoyed using Drupal for the last 10 years, Drush, is being axed, I started to set up all new Drupal sites with Composer. Unfortunately I can't update core as ...
-1
votes
1answer
171 views

Drag and drop nodes in a node list (to later use that sorting inside a view) [closed]

Under example.com/en/admin/content I show all site content filtered by content type and then I get a list of all nodes from that content type. The nodes will appear in some sorting (usually date). ...
10
votes
3answers
20k views

Perform a query with an entity field condition with multiple values

I have a content type that has a entity reference field that allows users to add multiple taxonomy terms in that field. I'm attempting to perform queries that grab nodes that have a specific set of ...
27
votes
4answers
39k 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 ...
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\...
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 ...
15
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 ...
13
votes
4answers
5k 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 ...
11
votes
3answers
7k views

How do I theme a link from a template file?

A twig template is rendering a list of links that come with classes. The basic: {{ mylink }} twig code will output something like <a href="#" class="someclass" >the text</a> Not all ...
9
votes
2answers
8k views

RuntimeException: Unable to determine class for field type found in the configuration

My website got this error, while updating from Drupal 8.3.7 to Drupal 8.4.0, and I am unable to solve it. The website encountered an unexpected error. Please try again later. RuntimeException: ...
30
votes
6answers
18k 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 ...
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?
11
votes
2answers
2k views

When should I create a service or a utility function?

I had this question in mind along all the last week: When I should create a service or an utility function? In the Drupal Core we have both Services and Utility functions, but I can't find the ...
8
votes
2answers
9k views

Programmatically get all names of an entity reference field

Example: I want to get all tag names that are inside field_tags of node 5. I can get the target_id with getValues(); But the problem is, I don't know what entity the target_id value belongs to. ...
7
votes
2answers
5k views

How to get the property of a content object in Drupal 8

I am trying to get the moderation_state property from a node object in Drupal8 (see attached image); however, I cannot work out how to do this. In Drupal7 it was pretty straight forward $node->...
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,...
12
votes
1answer
6k views

Implementing Custom Cache Tags

I would like to use the new Cache Tagging in D8 to help keep related content of different types (entities, nodes, blocks, etc) in sync. But from reading the currently available documentation of this ...
10
votes
3answers
6k views

Multistep/wizard forms

I'm trying to create a multistep/wizard form for Drupal 8. User fills in a first name, last name fields Clicks on the next button Fills out more information Clicks on the submit button There are ...
10
votes
4answers
6k views

Get menu link siblings

I'm trying to create a menu in Drupal 8 that is just sibling links of the current page. So if the menu is: Home Parent 1 Sub-parent 1 Child 1 Sub-parent 2 Child 2 Child 3 Child 4 Parent 2 When ...
10
votes
3answers
15k views

Pass Variable to Custom Block template file

I want to create my custom block with custom content in Drupal 8,in start I create Block with just simple text on it, and work correctly, my Problem is how can I pass my custom variable to it? I saw ...
8
votes
3answers
10k views

How do I add CSS & JavaScript to <head>?

I want to add the following code in my section of the front page using a Drupal 8 sub theme of Bartik. Can anybody help how to do it? What is the best way to do it in this new twig version of theme?...
7
votes
1answer
3k views

Field template for display mode

I have a template for my field: field--field-category.html.twig. Is there a way to override this for a specific Display Mode? A have a mode called teaser block. I tried field--teaser-block--field-...
6
votes
1answer
3k views

How do I get Views content items in “my language” but fall back to “default” if the content isn't translated?

This is probably very easy but endless searching has proved fruitless - there's lots of UI changes between the D7 and D8 versions of these modules it seems. What I want to do is create a blog in two ...
5
votes
1answer
1k views

How do I construct a thin controller with a dependent service?

For the first time I am trying to architect a reasonably complex Drupal 8 module, and not sure of best practice for the controller. I need to route paths to a controller. I intend the controller to ...
5
votes
1answer
3k views

Drupal 8 - “Implement menu links as plugins”, trying to build a menu tree

In a previous Drupal 8 menu question here, we determined that menu tree building was now a service. In my D8 contrib theme, I was then able to render the main menu tree as: function ...
3
votes
1answer
3k views

Override image-formatter.html.twig

I'm looking for an option to override the image-formatter.html.twig template in my own theme. But in the HTML page output (twig debug is set to true) there's no option listed to override the formatter ...
3
votes
1answer
7k views

How to get Media entity image (Entity Reference Field) URL in TWIG theme

Apologies if this question has already been asked. However, in my research I found the answers have been for simple image fields which are in default install of Drupal 8. However, in order to have ...
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 ...
11
votes
4answers
10k views

Are there any performance comparisons?

Are there any reliable performance comparisons between vanilla Drupal 8 versus Drupal 7? I have searched the web but only find the post "Drupal 7 vs Drupal 8 Performance Comparison", which is far ...
11
votes
1answer
14k views

How to render a template in Drupal 8

I am trying to render a template in Drupal 8. In Drupal 7, I would create a theme with hook_theme, make a templates directory in my module, drop the template file in and I could display it with theme()...
9
votes
3answers
7k views

If you update a routes file in Drupal 8 how do you clear the cache?

Having worked with Drupal 7 since its launch, I'm learning Drupal 8. At the moment I'm finding that whenever I change anything I have to do drush cache-rebuild to get things working again. On my VPS ...
9
votes
5answers
16k 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) { ...
8
votes
5answers
9k views

Adding JS to a Drupal 8 theme (replacement for drupal_add_js)

In Drupal 7, I can use drupal_add_js within a theme's template.php file as a theme_preprocess_html(&$vars) function: drupal_add_js(drupal_get_path('theme', 'mytheme') . '/js/scripts.js', ...
8
votes
4answers
5k views

Import CSV to nodes/entities in Drupal 8 including a UI

What’s the best solution for importing CSV files into nodes or entities in Drupal 8 that offers a UI so content editors can import regularly? I’ve heard D8 Migrate works well but I understand there’s ...
8
votes
4answers
10k views

How to create modal windows (pop-ups)?

I am trying to use Drupal 8's core function to open a page in a modal window. Unfortunately it is very hard to find some official documentation about it, and most of the blogs covering this topic seem ...
7
votes
2answers
2k views

How to assign blocks to particular regions on theme install?

Drupal 8 provides some blocks by default. Eg. Site Branding, Main Navigation, Powered by Drupal, Search etc. I have created a theme that has Site branding & Main Navigation named regions in which ...
5
votes
1answer
2k views

Query webform submissions

I'm attempting to perform a query on webform submissions so I can display all submissions submitted by a specific user. However I'm not sure how to make this query. I made a guess that webform ...
5
votes
3answers
8k views

How do you print View fields on Twig?

I have a view and added the title as a field. For example: How do I print the title? In Drupal 7 it was as simple as views-view-fields--[viewname].tpl.php <?php print $fields["title"]-&...
5
votes
1answer
5k views

Twig Template for a Custom Block Plugin

I'd like to have a custom twig template for custom block plugins from my custom module. My custom block HeaderText contains public function build() { return array( '#title' => 'Test', ...
4
votes
2answers
6k views

How to add Pager to a custom page?

I have a list of items which is fetched from an REST API and displayed in a page 10 at a time. These items are not stored in Drupal database in any way. I managed to have a basic pager which is just a ...