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.
15,791
questions
15
votes
2
answers
20k
views
How to set value for multivalue field in drupal 8 programmatically
I was saving poll choices Programmatically in Drupal 8. I have used Drupal8 Poll choices module.
$node_poll = Poll::load($pollid);
//print_r($node_poll);exit;
$i = array(13,14,15);
foreach($i as $key ...
15
votes
3
answers
25k
views
How can I implement AJAX form submission?
My task is to submit a contact form via AJAX and then to show a "Thank you for submission!" message, loaded in place where the form was. So I need to ajaxify existing contact form.
I found ...
15
votes
6
answers
18k
views
How to embed a view in a specific node?
I try to embed a view in a specific node. I found tutorials for doing this with Drupal 7 and PHP-Templates:
<?php print views_embed_view('view_name'); ?>
But I look for a way in Drupal 8 with ...
15
votes
3
answers
18k
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 ...
15
votes
2
answers
8k
views
How to create a empty module? [closed]
What is the most basic valid module one can create in Drupal?
It would be useful to have a simple blueprint available, for anyone just getting started with Drupal.
15
votes
5
answers
12k
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 ...
15
votes
9
answers
14k
views
How do I properly detect if region is empty? [closed]
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 ...
15
votes
1
answer
5k
views
Composer keeps overwriting .htaccess (and other files) every time I do anything
Every time I try to update anything using Composer, it overwrites a load of files. One of the files it overwrites is .htaccess, which I have customised, so I have to keep patching it all the time. ...
15
votes
3
answers
9k
views
How do I get a list of bundles?
In Drupal 8+, how do I programmatically get a list of bundles for an entity type, for example the list of content types. Specifically, I'm looking for the machine names.
15
votes
4
answers
12k
views
Looking for database insert example
According to db_insert manual page this function is deprecated and it is better to use Drupal 8 database connection to perform insert.
Deprecated
as of Drupal 8.0.x, will be removed in Drupal ...
15
votes
1
answer
9k
views
How get empty record on date field with entityQuery?
I am on a custom entity with custom fields and i try to get all empty record on date field :
Field was create with CCK, so the field storage has a table.
$query = \Drupal::entityQuery('test_entity'...
15
votes
3
answers
14k
views
How do I save uploaded file permanently in file_manged table?
How do I save a uploaded file with status equal to 1 in the file_managed table, in Drupal 8?
Whenever I upload a file, it is stored in the file_managed table with status value 0.
I have used File::...
15
votes
3
answers
30k
views
How do I access field data in Twig?
I have a list field for a custom block type that looks like this:
left|Align left
right|Align right
I can access the key in Twig by doing something like this:
{% block content %}
{{ content....
15
votes
3
answers
13k
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 ...
15
votes
5
answers
8k
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. ...
15
votes
3
answers
12k
views
Prevent node from being saved in hook_node_presave
When saving / updating node, i need to validate if certain fields combination is unique. Unique field module is supposed to allow this, but that feature is not working (validating only single field). ...
15
votes
4
answers
18k
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 ...
15
votes
2
answers
4k
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 ...
15
votes
1
answer
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 ...
15
votes
4
answers
13k
views
Which tables should be emptied when clearing database-stored cache manually?
Prior to D8 to clear cache manually one could truncate all tables beginning with cache_.
In Drupal 8 there are still tables starting with cache_, but there's also a cachetags table.
Is the advice in ...
15
votes
1
answer
4k
views
How to disable modules using config split?
I have created different split for each environement (local, dev, test, prod) using the Configuration Split module. How can manage modules (enable/disable) in each environment?
Should I keep core....
15
votes
2
answers
5k
views
Send email notification when a new comment is posted
I'm using Drupal 8.1 with Rules 8.x-3.0-alpha1.
I want to create a rule to send an email to the author of a node when a new comment is posted to this node. Problem is I didn't get the email address ...
15
votes
3
answers
433
views
How do I deploy/manage similar sites from an unique profile, without dumps?
I do not like the "cloning website" solutions which implies dumping a database and importing this dump in another environment. This does not look like a real world way of deploying several instances ...
14
votes
10
answers
18k
views
How do I fix a "Mismatched entity and/or field definitions" error on my configuration?
I'm getting the following error on my Drupal 8 configuration page.
Entity/field definitions
Mismatched entity and/or field definitions
The following changes were detected in the entity type and field ...
14
votes
3
answers
55k
views
How can I get the taxonomy term name from a term ID?
I'm trying to get a taxonomy term name by term ID. According to another post, I should load the term first:
$term = \Drupal::entityTypeManager()->getStorage('taxonomy')->load($tid);
But then I ...
14
votes
9
answers
11k
views
Moving module location [duplicate]
I have a test module located under site/all/modules/custom.
First, I did uninstalled the module, cleared cache.
then , I did moved the test module to modules/custom folder which is a new
location to ...
14
votes
3
answers
25k
views
"RuntimeException: Unable to determine class for field type found in the configuration"
I got this error while updating from Drupal 8.3.7 to Drupal 8.4.0.
RuntimeException: Unable to determine class for field type 'webform' found in the 'field.storage.node.webform' configuration in ...
14
votes
8
answers
27k
views
Is it possible to programmatically add a path alias to a programmatically created node?
I implemented a pre-save hook in a module, which populates nodes by fetching information from various APIs. I can't get my mind around how to add a path alias to newly created nodes.
Is there a ...
14
votes
2
answers
20k
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'...
14
votes
5
answers
24k
views
How can I add a CSS class to the body tag?
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?
14
votes
5
answers
27k
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 ...
14
votes
8
answers
14k
views
How to redirect an anonymous user to the login form after a 403 error?
I want to redirect an anonymous user to the login form if such user encounters a 403 error.
I have created event subscriber and this is my code, but I end up on loop on the current page.
/**
* ...
14
votes
4
answers
28k
views
How to load content in a div with ajax?
After validate with javascript that I'll be able to use the client location:
if (navigator.geolocation) {
...
I need to load the content of a view block with arguments (based in the position coords)....
14
votes
3
answers
12k
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).
14
votes
3
answers
52k
views
How do I retrieve the query parameters from a path?
How do I retrieve the query parameters from a path. Here's my path:
/search?keys=hello
Here is what I've tried:
$test1 = \Drupal::request()->request->get('name');
$test2 = \Drupal::request()-&...
14
votes
4
answers
14k
views
How to get all node entities tagged with a specific term?
For a given taxonomy term in D8, is there any API function/method that returns all node entities that reference it?
14
votes
5
answers
19k
views
How do I programmatically set a taxonomy term for a node?
I got some difficulties setting a taxonomy for a node. Currently I am using this code.
$node = Node::create(array(
'type' => 'detailseite',
'title' => $data[$headlineIndex],
'...
14
votes
5
answers
22k
views
Using Twig how can I get the summary of the body field to use in a template?
So I have a textarea with summary and I want to display the summary before the content.
So if I just use {{ content.body }} the text is displayed without the summary.
How can I get the summary ?
{{ ...
14
votes
2
answers
25k
views
How to prevent a block from being cached?
After login. I'm adding one sidebar block. Once I clear cache then only updates variable or it's value.
namespace Drupal\test_demo\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides '...
14
votes
4
answers
19k
views
Add content type field programmatically
My module provides a configuration form which allows to select a content type and on submit 2 new fields will be added to the selected content types.
I want to add these fields to the content types ...
14
votes
6
answers
15k
views
config-import error: These entities need to be deleted before importing
I have two Drupal environments with the same system.site:uuid
In the main Drupal I export the configuration with command:
drush config-export deploy -y
In the destination drupal I import the ...
14
votes
3
answers
8k
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 ...
14
votes
2
answers
15k
views
How do I include a custom class file in a module?
In Drupal 7 module development, I could include a custom inc, api, or php class file using the file[]= directive in the module.info file or the drupal_load_included() function.
With D8 using PSR, ...
14
votes
2
answers
22k
views
Get the public path directory
I want to save file programmatically in public path, and I want to know how can I get Public Directory path in Drupal 8. In Drupal 7, I would use the following code.
drupal_realpath(...
14
votes
2
answers
7k
views
Should I learn Symfony 2?
I saw this, How much of Drupal 8 will rely on or be compatible with Symfony? but I don't understand if it applies to my question. Should I learn Symfony 2 to develop with Drupal?
14
votes
5
answers
4k
views
How do I find available public methods?
I'm finding that the biggest problem in working with Drupal 8 is I can't get the data I need. Drupal 8 wants me to use public methods rather than manually drilling down thru an object. The problem ...
14
votes
6
answers
6k
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 ...
14
votes
1
answer
16k
views
How do I set the cookie lifetime?
I'm having trouble setting the cookie lifetime in my D8 instance. I'd like to set it to zero so that closing the browser logs-off the user.
I've added ini_set('session.cookie_lifetime', 0); to the ...
14
votes
3
answers
27k
views
Check content type of node in preprocess hook
I have a series of variables I want to make available on twig templates for specific content templates.
In MYTHEME.theme I have:
function MYTHEME_preprocess_node(&$variables) {
if (isset($...
14
votes
5
answers
10k
views
How do I alter the entity type class?
In Drupal 8 you can load an entity with:
$node = \Drupal::entityManager()->getStorage('node')->load(123);
This looks for the entity definitions and finds that node is defined by Drupal\node\...