New answers tagged

0 votes

Drupal REST cookie authentication, Post 403

how can I get the cookie authentication to work with jQuery? This is not possible. See this SO answer; the browser only allows you to use JS to access the cookies for the current domain; you cannot ...
user avatar
2 votes

Anonymous users can edit all anonymous users content

The anonymous user is a single user. Other systems have a concept of "anonymous users", but in Drupal it is called the anonymous user because from the standpoint of the system, in most ...
user avatar
0 votes

Mocking the response of XmlResponse::getResultCode not working

So this is how I resolved this a while ago: Create a new fixture, let's say tests/fixtures/payment-response.xml that'll contain the following response: <?xml version="1.0" encoding="...
user avatar
0 votes

How do I remove a configuration object from the active configuration?

You can remove all the configs you imported from config/install by implementing HOOK_uninstall(): function my_module_uninstall() { $module_path = \Drupal::service('extension.list.module')->...
user avatar
0 votes

cant get file uri using field preprocess

In a template don't use a content variable to get references to objects. This is what the template is rendering. The structure depends on the field formatter and its configuration. Use it only if you ...
user avatar
  • 88k
0 votes

Image styles - How to resize uploaded images and force an aspect ratio

If you want to resize the image while maintaining the desired aspect ratio, this is actually two steps. Get the image in the appropriate aspect ratio. Resize the image. One way to do this is with ...
user avatar
0 votes

How do you disable the CSS from a core module in Drupal 8 using libraries-override?

It was difficult to identify the correct library to exclude, using hook_css_alter made it easy for me to exclude a specific file. function THEME_css_alter(&$css) { $cssfile= "core/themes/...
user avatar
  • 131
0 votes

How do I build Breadcrumbs for a certain node?

For Drupal 9 you don't need to extend anything. Working code here: $routeName = $entity->toUrl()->getRouteName(); $routeParameters = $entity->toUrl()->getRouteParameters(); $route = Drupal:...
user avatar
1 vote

Get all parent term taxonomy id and name of vocabulary

The methods you are mentioning accept as argument a term ID and return parents of this term. You can use loadTree() providing the vocabulary name and for the second argument 0 to load the entire tree, ...
user avatar
  • 88k
0 votes

How to use helper/private methods with a .module file?

I would either put the code in a service, as leymannx said in his answer, or do like two Drupal core modules do: Put the code in a class that is retrieved and instantiated with \Drupal::service('...
user avatar
  • 94.1k
1 vote

Where to place the utility and internal functions for a custom module?

Facing same issue, I went as implemented inside tabledrag_example module, where a getSampleItems() function is called from a couple classes of the same module: a@cubo:~/tabledrag_example$ grep -R &...
user avatar
  • 707
0 votes

How do I print an exposed filter block in a template file?

I almost always use the twig_tweak module, there is 3 ways to render a block. You can see the doc here Twig tweak Block render, you should probably use drush ev "print_r(array_keys(\Drupal::...
user avatar
  • 366
0 votes

Load css and js assets as absolute paths during twig render

The folwing hook implemention will change the URL's to absolute URL's /** * Implements hook_js_alter(). * * Pass the JavaScript array to the optimizer service. */ function HOOK_js_alter(&$js, $...
user avatar
0 votes

Dependency injection for FormElement #process callback

Technically it's possible with the help of ContainerFactoryPluginInterface. The #process is just an array of callables, and it's initialized in getInfo() which is non-static. So, you have access to ...
user avatar
0 votes

How do i change date format in View Exposed Date filter?

I'm expanding on Wasim Khan's response, as it worked well for me. First, the Better Exposed Filters module needs to be enabled, and the exposed form display needs to be set as BEF rather than the ...
user avatar
  • 14.7k
0 votes

How do I programmatically add fields to a custom entity bundle?

An entity class can implement FieldableEntityInterface::bundleFieldDefinitions() to add fields to a bundle, but as that documentation page says, the recommended approach is another one. This function ...
user avatar
  • 94.1k
0 votes

How to access taxonomy term field?

if the taxonomy is a entity reference field : $current_entity_lang = $entity->get('langcode')->value; //version and so current revision $translated_entity = $entity->getTranslation($...
user avatar
  • 2,775
0 votes

Getting two errors when trying to click Extend and Config tabs

You can fix this with Drush. Please go to command line. Change directory to document root. Run this command: drush eval "\Drupal::service('extension.list.module')->reset()->getList();" ...
user avatar
  • 137
0 votes

How do I filter by year?

I think we should use the regular expression operator because it is easier to change the year:
user avatar
0 votes

How add conditional required field

In Drupal 9, there's no need to add so much custom code like the other answer. Here's an easy example, with the condition being a checkbox checked/unchecked: public function buildForm(array $form, ...
user avatar
  • 367
0 votes

Programmatically check the checkbox of an exposed filter

You can use hook_form_alter() by adding view id condition. The use the code below, replacing FILTER_NAME with the name of your filter and set the necessary options. $request = \Drupal::request(); if (!...
user avatar
  • 1
0 votes

Get the node ID value after submitting an attached Webform to that node

I found only the real Node-ID with the referenced webform submission with this code: $source_entity = $request_handler->getCurrentSourceEntity('webform'); $arrayData = $source_entity->toArray(); ...
user avatar
  • 1
0 votes

How to generate templates suggestion for media.html.twig for a specific field in a content type?

I found a workaround for this by using the view mode along with the content type. So the new code would be /** * Adds theme suggestions for media.html.twig */ function ...
user avatar
  • 804
0 votes

Determine which region a menu block is rendered in

The $variables["content"]["#attributes"] seem not to work anymore in all cases. The attribute tag has to be targeted to the specific content within the block. Hence $variables[&...
user avatar
0 votes

TrustedResponseRedirect failing - how to prevent cache metadata?

Since Drupal's TrustedRedirectResponse object extends CacheableSecuredRedirectResponse, I believe that is the source of the issue. This object is adding cache-related metadata to the response, which ...
user avatar
0 votes

remove cache in hook_preprocess_node

In THEME_preprocess_node hook you can add the below code to disable the variable cache for particular content type. if($variables['node']->getType() == 'content_type_machine_name') { $...
user avatar
  • 105
0 votes

Should I ignore the "vendor" directory?

I would say no. The composer.json file has version requirements and composer.lock specifies the exact versions that are installed. Having the composer.lock in version control effectively defines what ...
user avatar
  • 162
1 vote

How to use date_date_callbacks

Checking the official documentation of DateTime class, it appears that it calls all the function names passed as string in the #date_date_callback array (check processDatetime function). // Allows ...
user avatar
0 votes

How to disable core JS aggregation and AdvAgg just for admin paths

We have two options: Option 1: Disable advagg for admin routes: https://www.drupal.org/project/advagg/issues/3226448 Option 2: [PP-1] Drupal.ajax does not guarantee that "add new JS file to page&...
user avatar
0 votes

Site UUID in source storage does not match the target storage

Solved! Using the Drush command. Get the site uuid from existing site the Set the UUID. That's it. Get UUID from Source: drush cget system.site uuid Set UUID to target: drush cset system.site uuid &...
user avatar
0 votes

How can I safely move a module to another folder?

This worked for me By Admin UI Navigate to /admin/config/development/performance and ​click the button "Clear all caches".
user avatar
  • 1
0 votes

Splitting exposed form filters amongst several blocks

Although not perfect, a possible solution is to modify the exposed form. For me, the file was views-exposed-form--search.html.twig. You can then display your separate form elements using things like: {...
user avatar
  • 323
0 votes

How can I import the configuration on a different site?

Using the solution given by apaderno, I was able to import my config from another site which is an easy way to copy content types from a project to another for instance. Copy UUID from another site. ...
user avatar
0 votes

Watchdog table growing out of control

As mentioned by Clive you need to reclaim the diskspace, however when I had a similar issue in the past OPTIMIZE was too slow and crashed the site. According to the MariaDB knowledge base it should be ...
user avatar
0 votes

What could not allow me to log in?

You can use Flood Control Module. Flood Control provides an interface for hidden flood control variables (e.g. login attempt limiters) and makes it possible for site administrators to remove IP ...
user avatar
  • 157
0 votes

Generate Link based on custom content field types

Sorry for the late response but, haven't able to find myself some time to elaborate a good sample that may serve you as copy/paste answer. Said that, first of all you need to register your extra field....
user avatar
  • 856
0 votes

How to alter output of markup on a specific views field

To anyone experiencing a similar situation, you can have a look at: https://drupal.stackexchange.com/a/264191/106121 For Global Text Fields in Views (nothing) using HOOK_views_pre_render(\Drupal\views\...
user avatar
1 vote

Programmatically load node edit form

In Controller - if ($node) && !empty($node->id())) { $node = \Drupal\node\Entity\Node::load($node->id()); $build = \Drupal::service('entity.form_builder')->getForm($node); ...
user avatar
0 votes

How do I display the language settings (Site language field) on the user registration form?

This is an old question but there's a current issue opened on Drupal core for this here https://www.drupal.org/project/drupal/issues/3163758
user avatar
2 votes

Link in URL field to Media File

I think that the issue is that public:// is a PHP file stream wrapper defined by Drupal, which is a PHP language feature, and is not a URL, which is a publicly defined standard. The field is ...
user avatar
  • 870
0 votes

Get Video URL from Media Entity reference field

To display the raw url of a remote video field in twig (Drupal 9): {{ node.field_media.entity.field_media_oembed_video.0.value }}
user avatar
0 votes

How do I print an exposed filter block in a template file?

I used another approach to solve the problem. I overrode the views template to print the exposed filter where I wanted. You can get it as {{ exposed}} in the twig template.
user avatar
  • 804
3 votes
Accepted

How to update taxonomy field with array of terms?

You probably want to append the field items: $user = \Drupal\user\Entity\User::load($userid); foreach ($territories_id as $territory_id) { // full version (works with autocomplete in an IDE) $user-...
user avatar
  • 88k

Top 50 recent answers are included