Drupal StackExchange

EntityQuery where multi-value field contains a value

1 month 3 weeks ago

I've been programming for 15 years, 10 with PHP, but on my 1st Drupal project. Using Drupal 9.

I'm trying to retrieve an entity (custom content type) that has 2 multi-value entity (user) fields. If the value I have is in one field I want to get the other one. The problem is I can't figure out how to ask for the entity when the scalar value I have is in the list. Here's what I've got:

return \Drupal::entityQuery('node') ->condition('type', 'osj_relationships') ->condition('field_supervised_reps', [$userId], 'IN') ->execute();

Which generates this SQL:

'SELECT "base_table"."vid" AS "vid", "base_table"."nid" AS "nid" FROM "node" "base_table" INNER JOIN "node_field_data" "node_field_data" ON "node_field_data"."nid" = "base_table"."nid" INNER JOIN "node__field_supervised_reps" "node__field_supervised_reps" ON "node__field_supervised_reps"."entity_id" = "base_table"."nid" WHERE ("node_field_data"."type" = 'osj_relationships') AND ("node__field_supervised_reps"."field_supervised_reps_target_id" IN ('2458'))'

The 2nd part of the WHERE clause is looking for the field value in my parameter. I need it to look for my parameter in the field. I've also tried CONTAINS but that seems to assume the field value is a string rather than an array. If I could turn that WHERE clause around it would be perfect. ie.

(('2458') IN "node__field_supervised_reps"."field_supervised_reps_target_id")

I suppose I could use 'CONTAINS' if I could implode the field list but that would risk false hits.

Any suggestions?

Ed Coakley

How to delete a file attachment from the apachesolr index

1 month 3 weeks ago

My file attachments are indexed using apachesolr attachments 7.x-1.x-dev. I have a custom module that physically deletes a file using file_delete(), but file still remains in Apachesolr index even after re-indexing. The only way to remove from search results is to delete the index then reindex.

I tried this to no avail:

// Remove file from apachesolr index $env_id = apachesolr_default_environment(); $entity_type = 'file'; $entity_id = apachesolr_document_id($file->fid, 'file'); apachesolr_remove_entity($env_id, $entity_type, $entity_id); drupal_set_message('Deleting '.$file->uri.' from private attachments.'); file_delete($file);

The file is physically removed, but not removed from index.

Any suggestions on how to remove file from apachesolr attachments index/search results after it is deleted?

user1866032

custom field - How to get saved term selected for entity_autocomplete

1 month 3 weeks ago

I've looked all over, and for my level of understanding, I can't figure out how to get the stored value of an entity autocomplete field targeting taxonomy term type. I thought I could do something similar to what I have for select field, but it breaks the page. This is for a custom field...

$element['description_term'] = array( '#type' => 'entity_autocomplete', '#target_type' => 'taxonomy_term', '#selection_settings' => [ 'target_bundles' => [ 'descriptors' ], ], '#title' => t('descriptor'), 'default_value' => previously-entered-value, // this didn't work... '#default_value' => isset($items[$delta]->descriptor) ? $items[$delta]->descriptor : NULL, '#required' => FALSE, );
turpentyne

How do I use hook_preprocess_page_title() on a Bootstrap subtheme?

1 month 3 weeks ago

I've just discovered that Drupal 8 Bootstrap theme doesn't implement all existent hooks. So, for example, hook_theme_suggestions_page_alter() works, but hook_theme_suggestions_node_alter() doesn't work.

I'm interested on hook_preprocess_page_title(&$variables) and I wonder if it's possible to incorporate this hook into my custom Bootstrap sub-theme. I have created the page-title.html.twig template and cleared the cache.

I saw https://drupal-bootstrap.org/api/bootstrap/docs!plugins!Preprocess.md/group/plugins_preprocess/8.x-3.x but it seems this is for extending an already existent hook in Bootstrap theme, not for adding a new one.

How do I add hook_preprocess_page_title() in a Bootstrap theme?

leandro713

History of nodes viewed by a user

1 month 3 weeks ago

I'm developing a site using Drupal 8.2 and I have to create a page where the user can see which nodes he or she has viewed in the last 30 days. The goal is to provide a per user "browsing history" view with the recently read content ordered by date (ideally filtered by content type).

I've checked other similar questions about this topic and I've found the following suggested modules:

Unfortunately these modules don't have Drupal 8 releases at the time of writing this question (except the Message module which has a pre-release/alpha version).

I've also reviewed the official docs and found there's a core History module that tracks which nodes have been viewed by each user and when. I have this module enabled (it was enabled by default) and I've checked that it's actually collecting user interactions in the history table. According to the docs:

In Drupal 8 and later, the History module is included in Drupal core. When enabled, the History module records which user has read which content.

This only applies to authenticated users. Entities older than 1 month will always be shown as "read".

There is no UI provided, but history data is exposed to the Views module. You can add history data as a field to show "new" or "updated" node marks. You can also use a filter to only show content that is new or updated.

this module provides some kind of interaction with Views, so I assume it should be posible to create a view to show a list with links to each node viewed by the user. I've reviewed all the options in Views (filters, contextual filters and so on) and haven't found anything related to this history.

Is it really posible to create such a view? Any pointers on how to do that? Is there any other way to do this? How would you implement this funcionality in Drupal 8?

Cheers.

Mikel

Getting fatal error when trying to install Umami test site

1 month 3 weeks ago

I'm using Windows 10. I have installed composer and PHP.

I run the command composer create-project drupal/recommended-project mysite to create a recommended project. That works with no errors. Then I try to run the command php web/core/scripts/drupal quick-start demo_umami to install a sample Drupal site using the Umami Maganize demo:

C:\drupal\mysite>php web/core/scripts/drupal quick-start demo_umami 9/17 [==============>-------------] Install site PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in C:\drupal\mysite\web\core\lib\Drupal\Core\TypedData\Plugin\DataType\ItemList.php on line 274 Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in C:\drupal\mysite\web\core\lib\Drupal\Core\TypedData\Plugin\DataType\ItemList.php on line 274 C:\drupal\mysite>

It makes no sense that I'm trying to allocate only 20480 bytes and that's causing it to throw a FatalError. How do I fix this?

MrSnrub

hook_cron(): PHP output does not appear

1 month 3 weeks ago

I am (finally) migrating our Drupal 7 sites to Drupal 10.

The one problem I have not been able to resolve is output from the hook_cron().

Everything appears to execute, whether running on the browser using the special link, clicking on the 'Run cron' button, or as a crontab job. (This is verified by the logger messages, or informational messages which will subsequently appear on the site.)

However, none of the PHP output (for example, echo or print_r()) which worked under Drupal 7 appear on the browser or as an external crontab task. The web page doesn't even refresh. This is problematic because our cronjobs output a lot of debug information that allow us to verify that the tasks ran correctly, and this output is redirected into an email which can be reviewed without accessing the Drupal site. So it is not practical to use the logger or other Drupal mechanisms.

Interestingly, there are scenarios where the output does appear, which might provide a clue as to what the problem is:

  • if I include a call to phpinfo()
  • on certain (but not all) PHP errors, which causes all preceding output to appear

Note that this issue also occurs with the example cron code (cron_example.module) if I try to add echo statements; I have found one other user (on the drupal forum) who also ran into this as part of other questions, but there was no any information on how to resolve it.

I tried flush() in case it's some sort of buffering issue, but to no avail.

function cron_daily_cron() { // This output is not shown. echo "cron_daily_cron started..."; // The messenger message is shown. \Drupal::logger('cron_daily')->notice('cron_daily_cron started'); \Drupal::messenger()->addMessage('cron_daily_cron started...', 'info'); \Drupal::logger('cron_daily')->notice('cron_daily_cron exited'); // The error output caused by the following line is shown. inexistentfunctioncall(); // This message is not shown. echo "cron_daily_cron exited"; }

thank you!

backtothefuture

How can I rename uploaded files?

1 month 3 weeks ago

Using Drupal 8, I want to rename and clean all the uploaded filenames. I want to convert all the spaces to dashes and make all the characters lowercase. A file named Screen Shot 2016-03-29 at 1.13.21 PM.jpg would become screen-shot-2016-03-29-at-1.13.21-pm.jpg.

I want to do this on the image upload dialog form used by CkEditor.

otarza

Drupal Planet

Droptica: Drupal and Other CMS Systems Used by Polish Universities. The Droptica Report

1 month 3 weeks ago

Drupal is a CMS that colleges from different countries eagerly use. According to a report by The Drop Times, up to 80% of the world's top 100 universities chose Drupal for at least one of their websites. We wanted to find out how this technology is used in Poland and what other content management systems are popular among Polish higher education institutions - both public and private. This report presents the results of our analysis. 

LN Webworks: How to Implement Google Analytics In Drupal : All You Need To Know

1 month 3 weeks ago

Google Analytics 4 has taken the world of web analytics by storm. It is an incredible tool that helps you understand how users interact with your business website. You can use these insights to deliver personalized experiences to your customers, enhance loyalty, and boost your revenue. If you have availed of Drupal services and run a Drupal-powered website, this blog will help you delve into how you can successfully combine Drupal and GA4 to accomplish phenomenal results. 

How to Combine GA4 With Your Drupal Website

Given the fantastic user insights provided by GA4, its combination with Drupal can take your business website on an upward spiral of growth. As Drupal is a cutting-edge content management system, its combination with GA4 can result in the creation of an incredible marketing strategy and improvement in your site’s performance.