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
9
votes
1answer
1k views

Returning alternate HTTP codes for unpublished node

I'm trying to return the 404 page instead of 403 response for unpublished nodes in Drupal 8. I tested kernel response subscriber, but found the code I was using would only change the status code to ...
7
votes
2answers
10k views

How do I alter a language item link?

I am looking to modify the default output of Drupal 8 of links in a certain situation, the language switcher. Ideally I want to be able to do that in a twig template and not have to produce a massive ...
7
votes
4answers
7k views

How can I programmatically change the view mode?

In Drupal 7 I did something like: function mymodule_views_pre_render(&$view) { //dpm($view); if ($view->name == 'services') { $view->style_plugin->row_plugin->options['...
2
votes
3answers
1k views

Export Paragraph bundles

I know drupal config:export:content:type is for exporting a content type. Is there a way to do this for a Paragraph bundle? drupal config:export:paragraph:type doesn't work/exist.
2
votes
1answer
6k views

How to correctly use cookies? (Twig and cache context)

In my current project I need to alter node templates depending on previously visited landing page. My idea is that, when a visitor visits one of these landing pages, a cookie will be set, and ...
8
votes
4answers
17k views

Adding a class to “body”

I'm trying to add a custom CSS class to the <body> tag, How can I modify or pre-process the <body> tag to add the class body, Is it right to style the <body> tag with css file?
5
votes
2answers
2k views

Cannot deal with multiple iterations of same form on one page

I am designing a site that has a variety of points in which I need to render "workflow" buttons that are used to send actions to an external application. I used the form API to generate these ...
1
vote
2answers
1k views

How do I call legacy PHP files?

I am unable to call PHP files inside the Drupal directory. My task is maintaining staff's old pages on the new platform. With pages written on clear HTML, there is no problem: I place files in a ...
1
vote
1answer
516 views

Adding Constraint to entity referenced Paragraph field

I'm attempting to follow this documentation to add a custom constraint to a field in a Paragraph but I'm having trouble in adding constraint to that field. Currently I have book content type with ...
6
votes
3answers
6k views

How do I export and import content types (including fields) between environments?

I've been trying to find a smart way of exporting content type and all its fields from one installation of Drupal to another. The core "Configuration synchronization" export only export the content ...
2
votes
1answer
311 views

How can I theme individual fields in a row?

I'm having trouble doing something that I think should be relatively simple Drupal 8 views. I have a content type called Countries. I would like to display the 3 latest country nodes on my homepage ...
27
votes
11answers
6k views

How to prevent Devel module to be installed on production environments

Using the new Drupal 8 Configuration manager, how can I prevent it from installing the Devel module on certain environments? As far as I know, installing it on my local means the next time I export ...
28
votes
8answers
50k views

Iterate through multiple value content field in Twig template

I need to take control over the rendering of a field_admin_tags field in a node.html.twig template. This WORKS: {{ content.field_admin_tags }} — Renders everything (label + all field values) {{ ...
22
votes
6answers
44k 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: ...
30
votes
4answers
28k 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)...
15
votes
5answers
23k 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: .....
25
votes
5answers
41k 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
3answers
21k views

How do I create menu links programmatically?

I need to add some links to existing menu (or to new) programmatically in Drupal 8.
15
votes
2answers
9k 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. ...
10
votes
5answers
16k 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 ...
10
votes
2answers
16k 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 $node->field_tags->getValue(); But the problem is, I don't know what entity the target_id ...
15
votes
5answers
7k 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. ...
7
votes
3answers
11k 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 ...
6
votes
3answers
13k 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"]-&...
2
votes
3answers
2k 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 ...
21
votes
3answers
19k views

How can I get the node ID from a path 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 way to get either the internal path of /node/* for that alias if I am not on the ...
19
votes
2answers
37k 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?
10
votes
1answer
4k 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 ...
7
votes
2answers
4k views

How can I override or disable a parent theme's CSS file?

I am trying to override the menu.css file in the default Classy theme through my library file. I have read through the documentation, but anything I tried doesn't work. What would be the correct ...
4
votes
1answer
11k 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( '...
9
votes
2answers
17k views

How to customize the user login form?

I'm trying to customize user login form in /user/login page and I want to use a hook_form_alter function to add some awesome style and placeholder to each of user login form fields and remove or hide: ...
7
votes
2answers
2k views

How to migrate multilingual image alt fields from CSV?

I'm migrating multilingual content to Drupal 8 from a group of CSV files, and I'm about 95% of the way there. I have an image field attached to an article content type that shares the image among all ...
5
votes
1answer
2k views

Handle database connection exception via custom handler

I want to catch the database connection exceptions with a custom handler. I have already built an handler which is overriding the default logger factory, but now I want to use that custom handler to ...
5
votes
1answer
11k views

How to display a block with menu child items programmatically?

I need to create block with a list of links menu child items (only first level) of a given menu like this: Item 1 Item 2 Item 3 With this code I can have the menu items but I don't know how to ...
5
votes
2answers
3k views

Passing current node ID to the block template

I have a module with a custom block MyCustomBlock.php that get the nid of the current page and pass the value to the block template: $node = \Drupal::routeMatch()->getParameter('node'); $nid = $...
4
votes
2answers
659 views

Development with git branches/ gitflow and new modules

I wonder if somebody found a solution for the following issue related to Drupal 8 development with Git branches: You work on a new feature in a local feature branch for that you activate a ...
3
votes
1answer
2k views

Drupal 8 create a template override for a view block within a custom module

The Question: In Drupal 8: How do I add a template override for a view block and have that twig file reside in a custom module - not my theme? Further Information: I have already created a custom ...
3
votes
1answer
2k views

Programmatically get current multi-site instance name

Let's say I have the following multi-site setup: $sites['hello-world-a.com'] = 'a'; $sites['stage.hello-world-a.com'] = 'a'; $sites['dev.hello-world-a.com'] = 'a'; $sites['hello-world-a....
6
votes
1answer
11k views

Programmatically update node image field (using URI of the image) and get URL in twig

I am trying to update a node image field programmatically. I currently loading the node like this: $node = Node::Load(1); have the URI if the image that I want to set. I have tried this but no luck: ...
5
votes
2answers
3k views

How do I migrate users passwords?

I want to migrate some users from my Drupal 7 installation to a new Drupal 8 installation. I do not want to migrate the whole site or any content. And I also do not want to migrate all users. The ...
5
votes
1answer
6k views

How do handle exceptions?

I have a service provider that does stuff(for lack of a better word). Several functions in that service provider deal with a custom table in the database. Lets assume that in a function an id is ...
4
votes
3answers
5k views

How to sort on different columns in the same view?

I'm trying to build a Glossary View in Drupal 8. Let's say I have 3 content types: Person, Article, Event. I want sort by title ASC for Article and Event, but sort by Last Name for Person. So that ...
4
votes
2answers
3k views

How can I dynamically add URL or taxonomy to body class

Am creating my own theme and have successfully added the URL into the node class in node.html.twig with {% set classes = [ 'url' ~ url|clean_class, ] %} but I need to be able to style each page ...
3
votes
1answer
862 views

How to get cache metadata from nested render array when returning response?

I have a controller action that renders a list nodes and returns them, and only them. It's invoked via AJAX and the JS code just takes the result and inserts it into the page somewhere. My problem is ...
2
votes
4answers
8k views

Inserting the value from Datetime field form

I have a form where the user can input datetime and then I want to store it in the database. Here's my schema : 'start' => array( 'type' => 'varchar', 'mysql_type' => 'datetime', ...
12
votes
7answers
20k views

Is it possible to programmatically add a path alias to a programmatically created node?

I have created pre save hook in module which is populating my node by fetching information from various API's. However I can't get my mind around how to add node alias to newly created node. Is there ...
12
votes
4answers
6k 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 ...
8
votes
1answer
4k views

Working with template suggestions on Drupal 8

I need Drupal to use a custom page template if the URL contains a particular _GET parameter. In D7 I would do something like this in the theme's template.php: function mytheme_preprocess_page(&$...
8
votes
3answers
9k views

How to test for a boolean field in a twig template?

field_boolean is a boolean field. {% if content.field_boolean == true %} or {% if content.field_boolean == 1 %} or {% if content.field_boolean == 'true' %} or {% if content.field_boolean == '1' %} ...
6
votes
1answer
6k views

form_state->setRedirect(to_a_new_node_page) from the submitForm function, route "does not exist" issue

I created a custom module, It generate a custom Form in a page. When I submit my form, I create a new node. So, I am redirect to the form page but I would like be redirect to the new node page. I add ...

1 2
3
4 5
39