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.

11
votes
3answers
5k views

How to access a selected label instead of value

I have a select list of states: AL|Alabama AK|Alaska AZ|Arizona AR|Arkansas In code to access the selected state value I can run code like this: ... $entity->get('field_state')->value; ... I ...
11
votes
2answers
13k views

How do I create table in custom module

In Drupal 7 we used to use hook_schema() in the .install file to create a table. I believe that, in Drupal 8, you can do that as well, but I was under the impression that you should create .yml files ...
11
votes
1answer
5k views

Drush dl not working with PHP 7 and ubuntu 16.04

When i try to do: drush dl drupal-8 i get the following error: PHP Fatal error: Uncaught Error: Call to undefined function Drush\UpdateService\simplexml_load_file() in /home/alexander/.config/...
11
votes
3answers
6k views

Getting the node object in hook_form_FORM_ID_alter

Follow up to this question but I don't have enough reputation to ask clarification in a comment. I am porting a contributed module to Drupal 8. The sugested code on the above mentioned CR and post ...
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 ...
11
votes
2answers
363 views

When should I use methods, get() and magic offset

There are three different ways to get an entity value, for example the node content type. $node->type->value $node->getType() $node->get('type') Which one should I use?
11
votes
1answer
2k 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 ...
11
votes
1answer
8k 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 ...
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 ...
11
votes
5answers
5k views

Creating comments and nodes with Services 3.x and JSON

I'm having trouble getting services to recognize a POST to /comment.json. I have a similar issue with creating nodes as well. The following is the error I get: HTTP/1.1 406 Not Acceptable: Missing ...
11
votes
2answers
3k views

Sub folders and how to namespace classes

In Drupal 7 I can have a module inside another module. Is this still the case in Drupal 8 and how would I namespace classes inside the sub module? Another thing I can do in Drupal 7 is to create sub ...
11
votes
4answers
3k views

How do I add custom tab in the user profile page?

I need to add a custom tab in user profile page. I defined my route like below: mymodule.routing.yml mymodule.account: path: '/user/{user}/custom' defaults: _form: '\Drupal\mymodule\Form\...
11
votes
2answers
5k views

Exporting fields for content type

The configuration export feature of Drupal 8 would seem to be a very useful feature. However, I'm not sure I completely understand how it works. For example, if I do a single export, choose "Content ...
11
votes
7answers
18k views

Alter an existing form

We just starting out with Drupal 8 and very quickly we ran into our first problem. How should I alter an existing form in Drupal 8? We need to alter the save method for the node form to do a ...
11
votes
4answers
8k views

How to configure an exposed filter in a view using a date picker?

How can I configure an exposed filter so that users can filter my site content by creation date, preferably using a date picker? I tried using Content: Authored on as filter, but it gets me a text ...
10
votes
2answers
19k views

Programatically print blocks in twig [duplicate]

In drupal 8 I want to print the block programatically. For this I have tried the below code but not working. function theme_preprocess_page(&$variables) { $block = \Drupal\block\Entity\Block::...
10
votes
3answers
5k views

Get the list of all content types

How do I get a list of all the content types defined in a Drupal 8 site? In Drupal 7 it used to be node_type_get_types(), but it has been deprecated. I tried Entity::loadMultiple() but it doesn't ...
10
votes
3answers
15k views

How do I remove page title/header?

In Drupal 8, is there an easy way to configure a page so that its page header/title is not displayed on the page? I have created a Basic Page, with /home as path alias, and Home as title. I set it as ...
10
votes
2answers
9k views

Theme a contact form

I'd like to theme a contact form in Drupal 8. I'd like to put divs around form elements (using bootstrap). I also want to put some classes on certain elements (submit button, the form itself).
10
votes
5answers
13k views

How to override page title by content type

I'm using the pagetitle core module to render a page title. However, for certain content types I want the page main title to be the content type label (e.g. "News") and not the node label (e.g. "...
10
votes
1answer
4k 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
4k views

How to delete a content type, programmatically?

I programmatically created a content type during a module installation (using field storage configuration). I want to delete that content type while uninstalling the module. Is there any way to do ...
10
votes
2answers
9k views

Get the real path of a file

I am creating an Excel reading module in Drupal 8. I want to get the real path of a file for reading it from a path like public://2016-03/Places.xls. What function should I call for getting the real ...
10
votes
2answers
14k 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 ...
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
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 ...
10
votes
5answers
10k views

How to add fields to menu items

I need to build a main menu that is a subset of the regular menu. The design team thinks it would be better for user to have only the most important links in the main menu and not have everything. ...
10
votes
4answers
8k views

How to get the list of field-types?

I'm porting a module from Drupal 7 to Drupal 8. In this module's configuration settings I need to get the list of the currently existing field-types. In Drupal 7, this was achieved using the ...
10
votes
1answer
6k views

drush/drush dev-master requires codegyre/robo ~1.0.0-beta1

I'm transitioning a Drupal 6 site to Drupal 8.1 so following the official instructions, I set up a new install in its own directory. Since this is only one site, I followed the instructions to put my ...
10
votes
3answers
4k views

How to disable aggregation from either drush or PHPmyadmin?

I've been searching for this and ALL I found deals with Drupal 7. Old drush command drush vset preprocess_js 0 doesn't work for Drupal 8 and the "variable" table in DB doesn't exist in 8... Yet, I ...
10
votes
3answers
7k views

Loop through child elements in Twig like Element::children()

When dealing with a renderable array in PHP, I can use Element::children() to access the elements that are not # properties but subordinated renderable elements (form items inside a fieldset, items ...
10
votes
1answer
3k views

What is the difference between {{ |t}} and {% trans %}?

I notice that Drupal 8 support {% trans %} which, according to the Symfony documentation (The i18n Extension), adds gettext support to Twig. {{ |t }}, as far as I know, is the legacy way to add ...
10
votes
3answers
2k views

Embed one entity form into another and save both

I have a custom entity, that is depending on the user entity. In fact, it is even so depending that I felt it made sense to display my entity form within the user profile form: The problem I am now ...
10
votes
2answers
7k views

How can I add items to the Admin Toolbar?

In Drupal 8, I want to add a menu item to the Admin Toolbar with a submenu, with different links. How can I do it?
10
votes
2answers
17k views

How do I load taxonomy terms? [duplicate]

I want to load taxonomy terms by tid, but I didn't found how. Only how to load taxonomy by name, but now I have term id and I need to get value of fields. I call this on public function build() for ...
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 ...
10
votes
3answers
5k views

How do I disable modules in Drupal 8 in admin interface or via Drush?

In Drupal 7 it was possible to disable modules via admin interface or via Drush drush pm-disable. In Drupal 8 the checkboxes are greyed out in the admin interface and Drush is not able to disable them ...
10
votes
9answers
8k views

How do I properly detect if region is empty?

I have condition for printing regions in my page template as is not empty, but it will always print empty regions no matter what. What is the proper way to detect empty regions? For example, {% if ...
10
votes
4answers
5k views

Why does “composer update drupal/drupal” not work?

I installed a drupal 8.2.0 site with composer create-project drupal/drupal testsite. Now I would like to upgrade this to drupal 8.2.1. However, navigating into testsite and running composer update ...
10
votes
2answers
4k views

Reset user password without using “Forgot password?”

I know that in Drupal 7 I could reset the user #1 password via code. define('DRUPAL_ROOT', getcwd()); require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); ...
10
votes
1answer
12k views

Send a form to twig template

How I can pass a form to twig template in Drupal 8? For example, I get this: a form: mymodule/src/Form/MyForm.php Then on my Controller: public function myController() { ?????? return [ ...
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
3answers
2k views

Is hook_init() still used? If it is not anymore used, how do I convert code implementing hook_init()?

Looking on Drupal.org API, I noticed that hook_init() is not documented for Drupal 8. Looking at the change records, I found Bootstrap hooks no longer exist, which points to Get rid of all 'bootstrap' ...
10
votes
1answer
6k 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
1answer
10k views

“It is not safe to rely on the system's timezone settings.”

I'm finding the following error in the Recent log messages report: Warning: DateTime::createFromFormat(): It is not safe to rely on the system's timezone settings. You are required to use the date....
10
votes
2answers
7k views

How to get the url of a file field of a given node?

$node->field_file gives me a FileFieldItemList while the first entry seems to be a FileItem. $node->field_file->getValue() gives me an integer (I guess the file id). How can I actually get ...
10
votes
1answer
2k views

Theme folder in drupal 8

In Drupal 8, the theme folder for contributed themes seemed to have moved. Where do themes go in drupal 8?
10
votes
3answers
7k views

What is the correct way to set cache contexts on custom blocks?

I have run into an issue where a block that should be unique per page isn't for logged-out users. The issue is a custom block plugin I have on a views search page that contains custom filters (sort ...
10
votes
1answer
5k views

How do I update core with Composer?

Ok, admittedly I'm a composer novice. So, in my previous site, I used composer manager to update Drupal's core. That was pretty straight forward to use (just run "composer drupal-update"). However, ...
10
votes
2answers
5k views

How do I create and use a custom hook?

I posted a question here, which had one suggestion, but no answer. Does anyone have any advise on how to implement a custom hook in Drupal 8 which will allow other modules to update a table of ...