8 – How to override nid filter to accept multiple values on views

I have read a dozen of open tickets on this issue, but none of them explains how to solve this.
Lets say I have a raw array of nids and wanting to override the query in a hook for the nid filter.
The nid filter allows only 1 value or some of the operators given in the image below.

enter image description here

There isnt an operator “is one of” as in the taxonomy reference filter. So how could this be overriden and allow multiple values for the nids?
Is this possible perhaps within the hook_views_query_alter()?

8 – How do I make views pagination work with views_pre_render?

I have the following in order to reduce duplicates from my view – note that I’ve tried everything before using views_pre_render (aggregation, distinct, countless modules).

function rinksco_views_pre_render(DrupalviewsViewExecutable $view) {
  if ($view->id() == 'rinks') {
    $unique_nids = array();
    foreach($view->result as $key => $result) {
      if(!in_array($result->nid, $unique_nids)) {
        $unique_nids() = $result->nid;
      }
      else {
        unset($view->result($key));
      }
    }
  }
}

While it works – the second page doesn’t take into account the removed duplicates from the first page, and outputs older, irrelevant results.
How can I make pagination work with this?

views – Template suggestion does not work for table in a custom block / Might there be a way around?

I created a block on pages of the “Author” content type that lists all the articles the author has contributed to in a table (via contextual filter). Now I want to customize the table layout. I already did this before in a similar case, where I show the table on a page, not in a block. There I get file name suggestions for the template inside HTML comments like that:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_table__articles' -->
<!-- FILE NAME SUGGESTIONS:
   x views-view-table--articles.html.twig
   x views-view-table--articles.html.twig
   * views-view-table.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/MYTHEMEFOLDER/templates/views/views-view-table--articles.html.twig' -->

But in my latest case, the table in a block, I only get this suggestion:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_table' -->
<!-- BEGIN OUTPUT from 'themes/MYPARENTTHEMEFOLDER/templates/views/views-view-table.html.twig' -->

Note that it outputs the template of my parent theme, not my actual theme.

I did some research and found this issue, which currently is not resolved and the proposed patch not included inside the Drupal code. Is this the reason I cannot create a custom template or is there something else I can do?

If not, how would I create that table of referenced articles on the author’s page?

8 – Display members with same taxonomy using Views

Users have a “school” term reference field. When user visits his own user page, I want him to see his classmates listed.

For example, imagine that I have 3 students from the same school. When student A id:4 visits user/4, I want all student B and student C listed.

I can do this on term’s page, term/10, but I could not figure out how to do it on user’s page.

I believe it would have to be configured through contextual filter but I could not find a way yet.

Any help will be greatly appreciated!!

views – Get the user’s current language code

How can I get the user’s current language code in a view as a raw value, for example en or fr-ca?

This isn’t for filtering content. I need the raw value of the users’ current language to pass into a URL for a third-party tool. I added a custom text field. Then placed in some Twig code. The code is just failing to the fallback else value.

Based on other forum posts, I tried this code.

{% if language.getId() == 'en' %}
  en
{% elseif language.getId() == 'es-us' %}
  es-us
{% elseif language.getId() == 'en-us' %}
  en-us
{% else %}
  failed
{% endif %}

I get failed.

{% if language == 'en' %} 
  en
{% elseif language == 'es-us' %}
  es-us
{% elseif language == 'en-us' %}
  en-us
{% else %}
  failed
{% endif %}

I still get failed.

{% if language.id() == 'en' %}
  en
{% elseif language.id() == 'es-us' %}
  es-us
{% elseif language.id() == 'en-us' %}
  en-us
{% else %}
  failed
{% endif %}

I still get the same output.

postgresql – Having Updatable Views that obey initial select conditions

Let’s say I have a table like this:

+--------------+----------+-------------+
| name         | is       | in          |
|--------------+----------+-------------|
| Study        | <null>   | <null>      |
| Sport        | <null>   | <null>      |
| Biology      | Study    | <null>      |
| Linguistics  | Study    | <null>      |
| Art          | <null>   | <null>      |
+--------------+----------+-------------+

And I want to build a view as such:

CREATE VIEW v AS SELECT name FROM t WHERE "is" = 'Study';

Now if I want to insert into this view, it does not automatically set “is” to ‘Study’, I have to write a trigger and a function to adjust this behavior. However, this is a highly repeated pattern in my application, and I was wondering if there is a way to automatically achieve this (an updatable view that obeys the select condition).

8 – Views filter with additional form element

I have a custom views filter extending DrupaluserPluginviewsfilterName that I am trying to add an additional form element to. The existing filter is an autocomplete field that I need to add a checkbox element to. I’ve tried updating ::valueForm to have:

 // (...)
 // Working code that creates the autocomplete field.

    $existingElement = $form('value');

    $form('value') = (
      '#tree' => TRUE,
      'existing_element' => $existingElement,
      'new_element' => (
        '#type' => 'checkbox',
        '#title' => t('Checkbox'),
        '#default_value' => FALSE,
      ),
    );

Which is throwing the error:

Error: Cannot create references to/from string offsets in DrupalComponentUtilityNestedArray::setValue() (line 155 of core/lib/Drupal/Component/Utility/NestedArray.php).

It seems to be related to the form state, but I am having trouble figuring out exactly what is missing.

8 – How to get a dynamic delta token in a multi-value field via views

Here’s the situation…

  • I have a multi-value PHOTOS field in views. The image ALTs are all unique.
  • These photos are output into a slideshow, and I’m building a custom caption for each slide (photo) by combining HTML and TOKENS.
  • Unfortunately, I’m only able to grab specific Delta numbers within the token list… such as (node:field_photos:0:alt) or (node:field_photos:1:alt). But this obviously only passes that single Delta image’s ALT to all of the multi-value images.

**It’s required that these images remain in the same multi-value row and NOT in their own individual rows.

Is there any way to define a dynamic Delta based on the actual image shown? Something like (node:field_photos:?:alt)

In Drupal 7, there was a Compound Token module, which seemed like it may work. But in Drupal 8, I haven’t been able to find anything close to a solution.

Any help would be greatly appreciated. Thanks!

views – Newbie Taxonomy and Content Question

I’m new to Drupal (having used Perch CMS and PHP for years). Forgive me if I bumble the terminology a bit. I’ve been furiously going over documentation and tutorials. Sorry in advanced for being verbose.

Using Drupal 9 with a Bootstrap 5 template I’m developing – I’m looking to create a page that has a listing of downloadable PDFs that are categorized into groups and subgroups – these will eventually be placed in nested accordions on a page:

DOCUMENT LIBRARY

== Category A

==== Sub Category AA

====== Content: Has title, image and file link

==== Sub Category AB

====== Content: Has title, image and file link

== Category B

==== Sub Category BA

====== Content: Has title, image and file link

====== Content: Has title, image and file link

==== Sub Category BB

What I’ve done so far: I’ve setup a vocabulary tree that matches the structure I need. I also created a content type that meets the needs of each content item and have added taxonomy to the content form display. Created a view that show the content type.

What I need to do: Cycle through the taxonomy tree structure to create the accordions, showing the taxonomy term name and then nest each content item that matches the taxonomy term.

What I need from the community: Make sure I’m going down the right path and that I’m not misusing Drupal in this way. Also, any tips and/or things I should search for would be helpful. I’m pretty comfortable with theming (twig).