Drupal StackExchange

Location's Distance/Proximity exposed filter without Node Location Submodule but with Location CCK Submodule instead

3 days 19 hours ago

I would like to stay a little bit more flexible and use Location CCK fields (from Location CCK Submodule) instead of Node Locations (from Node Locations submodule) on my nodes. However, in my view of nodes the "distance/proximity" exposed filter (distance from a user-entered zip code) only works when my nodes have a location attached to them via Node Locations Submodule. If I disable Node Locations Submodule and attach a location to my nodes via a Location CCK field, the "distance/proximity" exposed filter never returns any results.

Does anyone know how to get around this issue?

Best, deinqwertz

deinqwertz

How to quickly delete thousands of nodes?

3 days 20 hours ago

This appears to be the prescribed way of deleting multiple node entities:

$nids = \Drupal::entityQuery('node') ->accessCheck(FALSE) ->condition('field_my_field', 'something') ->execute(); $storage_handler = \Drupal::entityTypeManager()->getStorage('node'); $entities = $storage_handler->loadMultiple($nids); $storage_handler->delete($entities);

However, this takes a long time to execute. On my machine, the above code takes about 15 minutes to delete 100 entities. The same machine can programmatically create 100 entities in about 40 seconds -- not blazingly fast, but acceptable.

I also tried looping through the $nids to load and delete them one at a time. That runs about 10% faster, perhaps because the memory is released after each load. Still, it's too slow.

The relevant content type has about 30 fields. Perhaps this is a factor.

What if my application requires regularly deleting hundreds of entities, and it isn't practical to wait over an hour for the script to execute? Is there some other option for deleting large numbers of entities?

Drupal 10.2.3. PHP 8.1.16. MariaDB 10.4.28.

arnoldbird

How upload a file\image via json api?

3 days 20 hours ago

I'm trying to upload a file or image on a D9 site via json api. The test is namely to upload the user picture on the user entity.

I've been following the documentation or read questions about, but I can't get it working.

The front-end is done with Next.js.

One the test done is the following:

The front-end next calls the next.js api server:

const filename = selectedFile.name; const fr = new FileReader(); fr.readAsArrayBuffer(selectedFile); fr.onload = async function () { const arrayBuffer = fr.result; if (arrayBuffer && typeof arrayBuffer !== 'string') { const base64String = _arrayBufferToBase64(arrayBuffer); const cookieSession = await getUpdateCookie(); const res = await fetch(`/api/users-test/user?cookie=${cookieSession}&name=${filename}`, { method: 'POST', body: base64String, }); } } function _arrayBufferToBase64(buffer: ArrayBuffer) { let binary = ''; const bytes = new Uint8Array(buffer); const len = bytes.byteLength; for (let i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return window.btoa(binary); }

The api next.js server post then to Drupal:

const res = await fetch( `${process.env.NEXT_PUBLIC_DRUPAL_BASE_URL}/jsonapi/user/user/{userID}/user_picture`, { method: 'POST', headers: { 'Content-Type': 'application/octet-stream', Accept: 'application/vnd.api+json', 'Content-Disposition': `name="file"; filename="${filename}"`, Authorization: `Bearer ${token}`, }, body: req.body, } );

I tried different ways to convert the file to "binary file data", but none work: the user picture is always a blank square with an X.

However, if I try the same call with postman, the file is correctly loaded:

So, for now, I hypothesize the problem is on the javascript code, while Drupal has been configured correctly.

Giuseppe

How do I change hostname after installing?

3 days 20 hours ago

I am a silly goose. I did something stupid and updated apache, php, and mysql before i had a chance to upgrade drupal, when I an much older versions. I had been updating my server and now the newer installed stuff breaks when I tried to uninstall php8 to php7. Thus, once I saw it was too late, I couldn't revert because it would break those other updates.

I got the novel idea of mysqldumping to a second server and made the unfortunate error of putting into foobar.com/drupal. Because I was incrementally upgrading I didn't want to mess other things up. Then I redumped, re-imported, and then I noticed I can't remove foobar.com/drupal because it is all in the root foobar.com.

How do I remove the subdirectory? It seems I can't find any file when grepping or searching the tables besides some cache tables.

How do I fix this so foobar.com/drupal can become foobar.com? Drush seems to crash for no reason too as I can't seem to clear cache, unless there is an easy way to drop tables/files to do so?

Jon Weinraub

Adding a variable to a menu twig template

3 days 21 hours ago

Im trying to use bootstrap for an accordion menu. My issue is that I need unique ID's for each accordion item. I found some similar topics here: Add Unique ID to Menu Item and here: How to add a custom variable that can be accessed on bootstrap's menu--account.html.twig?

Id like to know how I can get a unique ID for each item. Preferably an ID I can store within a variable which I can then pass to the data target attribute. As you can see in my code below, I'm trying to achieve something like that. The issue is that I cannot use the variable in a twig template that I declared within the preprocess function. Help me understand what I'm doing wrong.

Here is my code in my .theme file:

function my_theme_preprocess_menu__main(array &$variables) { $id = 0; foreach ($variables['items'] as $key => $item) { $variables['items'][$key]['accordionNav']=[]; $key_hash = hash('crc32b', $key); $variables['items'][$key]['accordionNav'] = $key_hash ; } }

Here is my twig file menu--main.html.twig:

{% import _self as menus %} {# We call a macro which calls itself to render the full tree. @see http://twig.sensiolabs.org/doc/tags/macro.html #} {{ menus.menu_links(items, attributes, 0) }} {% macro menu_links(items, attributes, menu_level) %} {% import _self as menus %} {% if items %} {% if menu_level == 0 %} <ul{{ attributes.addClass('nav navbar-nav').setAttribute('id', 'accordion') }}> {% else %} <ul class="sub-menu card-body"> {% endif %} {% for item in items %} {% set classes = [ menu_level ? 'sub-item' : 'nav-item', item.in_active_trail ? 'active', item.below ? 'card', ] %} <li{{ item.attributes.addClass(classes) }}> {% set link_classes = [ not menu_level ? 'nav-link', item.in_active_trail ? 'active', item.url.getOption('attributes').class ? item.url.getOption('attributes').class | join(' '), 'nav-link-' ~ item.url.toString() | clean_class, ] %} {% if item.below %} {% if item.in_active_trail %} <div class="card-header" id="heading{{ip}}"> <button class="btn btn-link" data-toggle="collapse" data-target="{{accordionNav}}" aria-expanded="true" aria-controls="collapse{{ip}}"> {{ item.title }} </button> </div> <div id="collapse{{ip}}" class="collapse show" aria-labelledby="heading{{ip}}" data-parent="#accordion"> {{ menus.menu_links(item.below, attributes, menu_level + 1) }} {% else %} <div class="card-header" id="heading{{ip}}"> <button class="btn btn-link" data-toggle="collapse" data-target="{{accordionNav}}" aria-expanded="false" aria-controls="collapse{{ip}}"> {{ item.title }} </button> </div> <div id="collapse{{ip}}" class="collapse" aria-labelledby="heading{{ip}}" data-parent="#accordion"> {{ menus.menu_links(item.below, attributes, menu_level + 1) }} {% endif %} </div> {% else %} {{ link(item.title, item.url, {'class': link_classes}) }} {% endif %} </li> {% endfor %} </ul> {% endif %} {% endmacro %}
mfvill

How to load a node with a webform through Ajax (Views) and make it work? [closed]

3 days 21 hours ago

I have a view that loads node content through AJAX, based on some controls (radio buttons). Sometimes the content can have a webform in it (as entity reference), which is embedded through a paragraph. The webform shows up, but of course the webform libraries are not attached to the page and submissions either fail, or send me an ajax response in the browser.

Anyone knows how to fix this?

See related issue: https://www.drupal.org/project/drupal/issues/2934463

Ludo

Taxonomy terms with associated nodes: display page by page navigation in node page?

3 days 22 hours ago

Hi i have a content type called Product and have fields(title,description,code,image) and also have vocabulary categories like below formate

parent term 1 -child term 1 --sub child term 1.1 --sub child term 1.2 --sub child term 1.3 --sub child term 1.4 --sub child term 1.5 parent term 2 --child term 2 --sub child term 2.1 --sub child term 2.2 --sub child term 2.3 --sub child term 2.4 --sub child term 2.5 . . parent term 5 --child term 5 --sub child term 5.1 --sub child term 5.2 --sub child term 5.3 --sub child term 5.4 --sub child term 5.5

so i want to display my product node page first parent terms list when i click parent term 1 then it's display all related child terms in next page after that when click child term 1 display all related sub child terms list in another page after that when i click sub child term 1.1 need to display related products list with above fields.Please find below flow of screen shots.

Taxonomy Parent Terms list Step 1

Step 2 when i clk Parent term 1 need to display all related child terms in another page like below

step 3 when i clk child term 1 need to display all related sub child terms in another page like below

step 4 when i clk sub child term 1.1 need to display all related node i.e product items list with above fields info. Please advice Thanks.

Parvateesam

Drupal Planet