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.

Filter by
Sorted by
Tagged with
10
votes
2answers
8k 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
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
6k 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 ...
10
votes
1answer
407 views

Does Drupal 8 core by default collect visitors data?

I am searching a lot since last few days but not getting an answer. I am in process of preparing Privacy Policy of site. I am not providing any user registration or user login facility but anonymous ...
9
votes
2answers
9k 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: ...
9
votes
2answers
3k views

How to add a variable to a twig field template?

On a node page I would like to add the node title to the output alongside another another field (the example field). I have created a new field--field-example.html.twig but am unable to access the ...
9
votes
4answers
7k views

How can I programmatically get the theme name?

In Drupal 8, is there a way to figure out what theme you are currently using in a module? I need to get the name programmatically within the code of a module.
9
votes
5answers
6k views

How can I control block visibility with code?

In Drupal 6, I could control block visibility using PHP code in the block UI configuration like: <?php return MYMODULE_abc() || MYMODULE_def() ; ?> These functions looked up the current node, ...
9
votes
4answers
13k views

How to trim body text (field_body)?

I've created a view for a node which shows the content as rendered entity. Among others, I output the body field in the twig template: {{ content.field_body }} Now, I'd like to limit the text to 200 ...
9
votes
4answers
11k views

How can I re-install a module?

I am creating on Drupal 8 module. I have used a custom table to store the data collected from forms. Somehow during installation, tables are not created due to an issue in the install file. Now I have ...
9
votes
2answers
11k views

What is the equivalent of hook_menu_alter() to change a menu item type?

I want to change the menu type in Drupal 8. In Drupal 7 we can achieve this using hook_menu_alter function module_menu_alter(&$items) { $items['admin/config/people/ip-blocking/default'] = array(...
9
votes
5answers
3k views

How to display more than 10 items in link widget autocomplete?

This is a question about the Link module. Since with the Link module you can enter external or internal links both, we strongly rely on it. Unfortunately the number of items to be displayed from its ...
9
votes
2answers
8k views

How to test if current page is an admin page?

At some point I started using the following code to test if current page is an admin page: $route = \Drupal::routeMatch()->getRouteObject(); $is_admin = \Drupal::service('router.admin_context'...
9
votes
5answers
18k views

How do I add a CSS class to a form label?

I want to add a CSS class to a label on a page that is using Drupal 8 Form API. I have been unable to find a reference online on how to apply that. I have used the following workaround, but it ...
9
votes
4answers
7k views

count multivalue field values in twig

I want to count the number of values of unlimited field in twig tempalate I tried {{ node.field_mytext.count }} => faced with error and in another try {{ content.field_mytext.count }} => nothing ...
9
votes
2answers
16k views

Preprocess variables only for certain blocks

Is it possible preprocess variables only for certain blocks? I have created such function: mytheme_preprocess_block__aggregator(&$vars) but it doesn't works. -- EDIT -- It seems to be fixed in ...
9
votes
3answers
6k views

Retrieve translated taxonomy term in current language programatically

How do I retrieve a specified taxonomy term translated (if translation exists for this term) from the current language context programmatically with D8?
9
votes
4answers
12k views

Get the value of a custom user field

I have a custom form that needs to display a default value from a custom user field. The field was created under /admin/config/people/accounts/fields and its machine name is field_r_period_length_sec. ...
9
votes
2answers
5k views

Configuration export/import doesn't work

I did a drush config-export staging on a local version of my site then a drush config-import staging on the server. It imported all the taxonomy vocabulary but no terms. I also tried it via the UI (...
9
votes
1answer
5k views

How do clean_class and clean_id work?

I am new to Drupal 8 and Twig syntax. I am not able to understand how its working filter clean_class "region|clean_class"? What is clean_class here? I have added following code in my page.html.twig ...
9
votes
2answers
1k views

Image and Media Best Practices

I am looking for suggestions on Best Practices for D8 and files / media entities. I originally added this as a answer but kiamlaluno evidently felt that it should be a new question in itself so here ...
9
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 [ ...
9
votes
1answer
4k views

Add tab / local task to a node

I've created a module called hotel_reviews. I've created the route node/{node}/reviews using hotel_reviews.routing.yml. hotel_reviews.content: path: '/node/{node}/reviews' defaults: ...
9
votes
4answers
1k views

How to rerun hook_post_update_NAME()

Drupal 8 introduced hook_post_update_NAME() which has some benefits over hook_update_n, for updating modules. Each hook_post_update_NAME() should only run once, but sometimes I want to rerun it, like ...
9
votes
2answers
3k views

What is the appropriate way to get items from an entityqueue?

I was having trouble finding API examples for entityqueue in Drupal 8. I ended up writing a query to get the items. $query = $this->database->select('entity_subqueue__items', 'esi') ->...
9
votes
3answers
5k views

Bootstrap from external script

I am trying to create Drupal entity instances from an external script that is not part of Drupal or a Drupal module. For Drupal 7 there was drupal_bootstrap to load the required Drupal dependencies. ...
9
votes
2answers
6k views

Disable cache for specific blocks

I would like to let pages continue to benefit from Drupal 8 caching mechanism for anonymous users, except for a specific blocks which should update on every page refresh knowing that they are ...
9
votes
1answer
3k views

Add a class to a Drupal table cell containing ['data']

In Drupal 8, rendering Tables is still a lot like Drupal 7. You build out multi-dimensional arrays of rows and columns in PHP that Drupal transforms into a <tr> and <td>s respectively. ...
9
votes
1answer
589 views

How do I add a column to the table shown in the vocabulary listing page?

I need to add a column to the table shown in the page listing the vocabularies defined for a site. How can I achieve this?
9
votes
2answers
5k views

Print image field with multiple entries from field in twig template

I have a twig template for a custom content type. And it is able to render most fields just fine, but I am unable to print an image field with multiple images. node--mycontenttype.html.twig contains ...
9
votes
1answer
2k views

What order should configuration import and module updates be run?

Since the schema version of modules is stored in the key-value storage, it is unique to each site and is not synced through configuration management. That makes sense, since updates may need to make ...
9
votes
5answers
4k views

How can I deploy user interface translation?

Strings that I translate at "User interface translation" /admin/config/regional/translate are not exportable by "Configuration management" /admin/config/development/configuration for me. Am I missing ...
9
votes
1answer
6k views

How to use #element_validate and #value_callback in Drupal 8

I have followed the official Form API Documentation, but this code don't work for me on Drupal 8: public function buildForm(array $form, FormStateInterface $form_state) { $form['upload'] = ...
9
votes
1answer
2k views

How do I create custom user tabs?

I am trying to create a new custom tab that appears on all routes that are descendants of entity.{entity_type}.canonical. I have tried extending the DeriverBase class, specifically overriding the ...
9
votes
5answers
7k views

How to perform a redirect to custom page after node save or delete?

What I'm trying to do doesn't work, I still get redirected to /node/{id}: function hook_form_alter(&$form, $form_state, $form_id) { $form['actions']['submit']['#submit'][] = '...
9
votes
1answer
6k views

Trying to add an html head tag in Drupal 8, beta 2

With Drupal 8.0 beta2, drupal_add_html_head() has been deprecated in favor of #attached. So the old way was: function MYTHEME_page_build(&$page) { $viewport = array( '#type' => '...
9
votes
4answers
3k views

How do I migrate file entities into media entities?

I am using the Migrate module for a D7 to D8 migration and I am writing the whole migration manually in code (rather than use the built in D7 migrate module, as I wanted more granual control on the ...
9
votes
1answer
3k views

Managing module library dependencies with composer

I've found Dependencies in a custom module with drupal-composer, How can I include a third party library in my custom module without using Composer Manager and How to manage contrib modules composer ...
9
votes
5answers
2k views

“Source database does not contain a recognizable Drupal version.”

I have installed two Drupal sites in my local Ubuntu desktop 15.10 Apache2 (2.4.12) environment: One is a fresh install of Drupal 8 and The second is a copy of an existing working site built with ...
9
votes
4answers
5k views

Updating core from 8.2 to 8.3 via composer

I am trying to update Drupal core to 8.3 but am running into difficulties. At the bottom of the page is the composer.json file I am starting with. If i change "drupal/core": "~8.2" => "drupal/core"...
9
votes
1answer
810 views

How can I install composer packages with drush make?

How does one include composer package in a drush make file? The more general answer, the better. There's already a composer drush module allowing drush dl to handle composer packages, but that ...
9
votes
3answers
2k views

Get title of page

When I get the title on some pages, I get an empty title like Home. The code I am using in my subscriber is the following one. class HistorySubscriber implements EventSubscriberInterface { public ...
9
votes
2answers
1k views

Drupal Services OAuth

I have been working on this issue for a couple weeks now, and I can't find a solution for the life of me. The fact that I have found no decent documentation (on Drupal's website, or anywhere else) is ...
9
votes
0answers
7k views

Drush : Missing $settings['hash_salt'] in settings.php [closed]

I just setup a new Drupal environment with Mamp. I can access my website just fine, but whenever I want to run drush cr I get the following error : exception 'RuntimeException' with message 'Missing $...
8
votes
9answers
9k views

How do I override the admin theme in my own theme?

I created my own Drupal 8 theme: mytheme.info.yml: name: My Theme description: 'Drupal 8 Theme' type: theme base theme: bartik core: '8.x' mytheme.libraries.yml global-styling: version: 1.x ...
8
votes
5answers
8k views

How to embed drupal content in other sites (remove X-Frame-Options: SAMEORIGIN)?

I tried to make a iframe with a drupal page but I received the following message: Multiple "Refused to display (page adress) in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'." Then, I ...
8
votes
2answers
6k views

why are Twig template variables with markup not being rendered?

In my template preprocess function I have a variable: $test = "<p>this is a paragraph</p>"; and I pass it to my template: $variables['test'] = $test; and output it in twig: {{test}} ...
8
votes
2answers
10k 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. ...
8
votes
3answers
10k views

How to find image style URL from within twig template?

I'm implementing a node template that uses an image twice; once in a 'normal' way (i.e. using the image style defined in the field's display configuration for that view mode), and the other is as a ...