I am using {{ fields.field_foo }} as a parameter to contextual filter. The value of {{ fields.field_foo }} is 1321 and prints out nicely as a separated field. Yet, when used as parameter to another views it prints nothing. If I put plain number 1321 as a parameter it works well.

In the field_foo I am rewriting it as{{ field_foo__target_id }} and stripping all the tags.

How can get this number from variable to work as a static number, which seems to work well? What is the problem here?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ErnoVanhala created an issue. See original summary.

codesmith’s picture

Same issue. I'm trying to pass the nid. Neither of the suggested Replacement Patterns below work in the "Contextual filters" field

{{ raw_fields.nid }}
{{ fields.nid }}

If I put an integer in the field like "123" then it works.

codesmith’s picture

Title: Parameter is not working » Contextual filter parameter is not working
lincolnbergeson’s picture

codesmith’s picture

The issue in #4 isn't really related. This issue is about the value not being passed correctly. The issue in #4 is about the name of the token not being passed correctly.

Just a follow up - I have a multiple value field that contains 1 or more node ids. I wanted to pass those node ids to the child view, separated by a + sign. The field contextual filter wasn't working. I looked at the code and there's a lot of white space around the values. I tried stripping out the white space ala:

      case 'fields':
        $value = $view->field[$id]->last_render;
        $value = preg_replace('/\s/', '', $value);
        break;

and it worked. Of course this only works for my use case of NIDs but maybe this will help others...

dev3.addweb’s picture

FileSize
49.64 KB

The issue is passing the parameter for Contextual filters in view field.

You can pass parameters as:

Static parameter: "123"
Using token: "{{ raw_fields.nid }}"

See attached screenshot for passing a parameter using token.

TrevorBradley’s picture

I similarly can't make this work. I tried adding my term {{ raw_fields.tid_1 }} in quotes as @kishna.addweb suggests, and I couldn't make it work either. If I add in the term staticly, it works fine.

If you can't pass in dynamic contextual filters based on the parent row's data, what's the point?

I must be missing something, plugging away at it.

EDIT: Appears to be fixed in 8.x-1.x-dev...

boinkster’s picture

I'm passing a portion of an Address field, Neither tokens work, but a static value does:

{{ fields.field_address_address_line1 }} returns no result
{{ raw_fields.field_address_address_line1 }} returns no result
Static Value - "111 3rd St" returns a result

EDIT: This works on the DEV version

gg24’s picture

Hi,

I tried reproducing this issue but couldn't do so.

I followed following steps:-

  1. Created a view of Article content type with contextual filter content ID.
  2. Passed it as a field to other view of Article content type.
  3. Passed contextual filter as {{ raw_fields.nid }} inside view global field.
  4. Works as expected.

Please let me know if I missed any step to reproduce.
Please check the attached screenshot.

Thanks!

purushotam.rai’s picture

rconstantine’s picture

Even with the DEV version, I still had issues. Applying several outstanding patches worked for me. Hopefully, they'll be rolled in at some point.

johncionci’s picture

@rconstantine

Could you post the patches that made this work?

andileco’s picture

Removing this post, as things changed after updating to dev. Still a problem, but I'll look for a different issue.

andileco’s picture

mholloway’s picture

To clarify, it's the regular expression in the function getTokenArgument in src/Plugin/views/field/View.php that is the problem.

The regexp allows only lower case letters in the argument name. It should allow upper case too.

The patches referred to above may not match line numbers in the current source so may fail to apply.

The fix is to add A-Z into the regexp:

-    preg_match('{{\s?(?<type>[a-z_0-9]+)\.(?<id>[a-z_0-9]+)\s?}}', $token, $match);
+    preg_match('{{\s?(?<type>[a-zA-Z_0-9]+)\.(?<id>[a-zA-Z_0-9]+)\s?}}', $token, $match);
ThirstySix’s picture

I want to pass multiple Contextual filters in the View Settings.

I passed the Contextual parameters like this using the replacement tokens.
{{ raw_fields.tid }}{{ raw_fields.tid_1}}

I tried with the comma separated and slash value also
{{ raw_fields.tid }},{{ raw_fields.tid_1}}
{{ raw_fields.tid }}/{{ raw_fields.tid_1}}
But, It can't pass the value in the particular embedded view.

how can i pass the more than contextual parameters?

ThirstySix’s picture

I applied the patch. now it's working fine. Thanks.

Path: \modules\contrib\views_field_view\src\Plugin\views\field\View.php

 protected function getTokenArgument($token) {
     // Trim whitespace and remove the brackets around the token.
-    preg_match('{{\s?(?<type>[a-z_0-9]+)\.(?<id>[a-z_0-9]+)\s?}}', $token, $match);
+   preg_match('{{\s?(?<type>[a-zA-Z_0-9]+)\.(?<id>[a-zA-Z_0-9]+)\s?}}', $token, $match);
 
     return [
       'type' => $match['type'],
 public function getTokenValue($token, ResultRow $values, ViewExecutable $view) {
......
 case 'fields':
        $values = $view->field[$id]->last_render;
    +   $value = preg_replace('/\s/', '', $values);
        break;
      case 'raw_arguments':
.....
isalmanhaider’s picture

@ThirstySix, which patch did you use? could you please provide the exact link to it.
Thanks

ThirstySix’s picture

the above code, just replace in the view.php file
https://www.drupal.org/project/views_field_view/issues/2873359#comment-1...

 public function getTokenValue($token, ResultRow $values, ViewExecutable $view) {
......
 case 'fields':
    - $value = $view->field[$id]->last_render;
    +    $values = $view->field[$id]->last_render;
    +   $value = preg_replace('/\s/', '', $values);
        break;
      case 'raw_arguments':
......
protected function getTokenArgument($token) {
     // Trim whitespace and remove the brackets around the token.
-    preg_match('{{\s?(?<type>[a-z_0-9]+)\.(?<id>[a-z_0-9]+)\s?}}', $token, $match);
+   preg_match('{{\s?(?<type>[a-zA-Z_0-9]+)\.(?<id>[a-zA-Z_0-9]+)\s?}}', $token, $match);
 
     return [
       'type' => $match['type'],

and then check. Thanks

shoebob’s picture

@ThirstySix, this patch results in this error for me:

Warning: preg_match() expects parameter 2 to be string, array given in Drupal\views\Plugin\views\HandlerBase::breakString() (line 725 of core\modules\views\src\Plugin\views\HandlerBase.php).

....
(errors continue for 100+ lines)

When I view a page on the site I get a different kind of error message:

The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">InvalidArgumentException</em>: Placeholders must have a trailing [] if they are to be expanded with an array of values. in <em class="placeholder">Drupal\Core\Database\Connection-&gt;expandArguments()</em> (line <em class="placeholder">735</em> of <em class="placeholder">core\lib\Drupal\Core\Database\Connection.php</em>). <pre class="backtrace">Drupal\Core\Database\Connection-&gt;query(&#039;SELECT users_field_data.uid AS uid
FROM
{users_field_data} users_field_data
...
(error message continues...)

Appreciate any help you can give.

thomasmurphy’s picture

This still seems to be a bug on the lastest release. Also, there is mentions of a patch but no attached patch file. I'm switching this to a bug report as I think that's a better fit.

thomasmurphy’s picture

Category: Feature request » Bug report
BioMac411’s picture

Thank you ThirstySix, this fixed my problem last year...
Needing to manually having to remember I did this, and spending hours re-remembering... I figure I'll just add the patch here so I can reuse on composer updates. It should be added to the module. Now last time module was updated the 'raw_' was added. It works great with a single element. I've got complicated ones that I'm actually running through a number of elements. With this patch, it allows me to sent multible through, not independently, but all at the same time.

shoebob I believe this is what you want to do. So with this patch, you can. Don't use the 'raw_' version but use just the fields version.

Edit to add:
I collect the Entity IDs, with the multiple settings (comma only separator, with no space) in one field first, then take that field to the next Global: View (field view) and for the Contextual filters I do not use the 'raw_fields.fieldname' I use the 'fields.fieldname',
this is where 'shoebob' above was going wrong. If one only has 1 element going to the Contextual filter, then the 'raw_fields.fieldname' works fine.

I just realized there is a major difference between 'Views Field View' and the 'Twig Tweak' module one could assume they do similar things. I can run my views templated fields through both, 'Views Field View' doesn't strip the 'style' tags that are needed, and the whole reason for having the template in the first place. 'Twig Tweak' still allows the template to work, but then strips out the 'style' tags on display. I'm also routing a lot of views through other views to easily build what is needed, a lot of rest apis using 'rest views' through 'rest_export_nested'. I have been following the Contenta project for years, but fine it easier to get exact output I need this way. when you wrap your mind around being able to push bootstrap code to a complicated angular app, and just render it with out complicated front end logic... one last issue I'd like maintainers to think about, I don't send multiple arguments along to get multiple views back, I send multiple arguments along, to get one object back, think JSON. this patch allows that.

With out the patch I'll get errors like below (for future Google searches)

Notice: Array to string conversion in Drupal\Core\Routing\UrlGenerator->doGenerate() (line 203 /core/lib/Drupal/Core/Routing/UrlGenerator.php) 
Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 266 of /core/lib/Drupal/Core/Entity/EntityStorageBase.php) 
Warning: Illegal offset type in Drupal\Core\Entity\EntityStorageBase->getFromStaticCache() (line 172 of /core/lib/Drupal/Core/Entity/EntityStorageBase.php)
BioMac411’s picture

Version: 8.x-1.0-beta2 » 8.x-1.0-beta3
FileSize
959 bytes

Sorry, missed an 's' in the above patch, this one should fix this.

BioMac411’s picture

fixed again with better file name too

joseph.olstad’s picture

Status: Active » Reviewed & tested by the community

I haven't done a nit-pik on the patch but empirically patch 25 is working very well for us, thanks so much!

jukka792’s picture

I applied the patch 25#, but just to clarify, is the passing of the 2 filters like this in the Contextual filters -field:
{{ raw_fields.nid_1}} / {{ raw_fields.id_1 }}

I can't make it working, but trying to find where is the problem.
It only works with one, {{ raw_fields.nid_1}}

johnsicili’s picture

25 is working for me. Thank you contributors :)

kazah’s picture

Patch #25 works fine.

I use view with one argument. Before I applied this patch only fixed value works (no token).

But I need to use {{ fields.MYTOKEN }}, not {{ raw_fields.MYTOKEN }}.

What is the difference between these two tokens? I don't undrestand at all.

douggreen’s picture

Updated patch for latest for beta4