Adding the contextual filter throws an error

Created on 9 March 2023, over 1 year ago
Updated 14 March 2023, over 1 year ago

Problem/Motivation

On a Content Based View (on latest D9 - 9.5.4) I can see 'Menu Children' as an option in Contextual filter, but selecting results in no change (it fails to save). if i try via 'open in new tag' I get error. not sure what i can have done wrong up to this point.

composer require 'drupal/views_menu_children_filter:^3.0@RC'

drush en -y views_menu_children_filter

drush cr

fact i see it under Contextual Filters gave me hope. here are first 12 lines or so

The website encountered an unexpected error. Please try again later.
ParseError: syntax error, unexpected '|', expecting variable (T_VARIABLE) in Composer\Autoload\includeFile() (line 272 of modules/contrib/views_menu_children_filter/src/Plugin/views/argument/MenuChildren.php).
Composer\Autoload\includeFile() (Line: 428)
Composer\Autoload\ClassLoader->loadClass()
spl_autoload_call()
class_exists() (Line: 96)
Drupal\Component\Plugin\Factory\DefaultFactory::getPluginClass() (Line: 17)
Drupal\Core\Plugin\Factory\ContainerFactory->createInstance() (Line: 76)
Drupal\Component\Plugin\PluginManagerBase->createInstance() (Line: 121)
Drupal\views\Plugin\ViewsHandlerManager->createInstance() (Line: 106)
Drupal\views\Plugin\ViewsHandlerManager->getHandler() (Line: 470)
Drupal\views_ui\ViewUI->submitItemAdd()
call_user_func_array() (Line: 114)

🐛 Bug report
Status

Fixed

Version

3.0

Component

User interface

Created by

🇳🇿New Zealand petednz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @petednz
  • Assigned to Grevil
  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks @petednz, I guess the issue is your PHP version.

    The affected line is:

    public static function filterByPage(Sql $query, MenuLinkContent|NULL $link)
    

    and the |NULL type declaration isn't supported by your PHP version. Which one are you using?

    This is a bug in the module, as the module doesn't define a minimum PHP version.
    But instead of introducing such a limitation, I'd suggest to check if we can't simply use

    public static function filterByPage(Sql $query, MenuLinkContent $link = NULL)
    

    here instead with the same results.

    BTW @Grevil shouldn't it be

    public static function filterByPage(Sql $query, ?MenuLinkContent $link)
    

    also in latest PHP versions?

    Is the |NULL syntax correct anyway? I guess that's only for comments?

    Please also check all other parts of the code for this issue.

    Thanks :)

  • 🇳🇿New Zealand petednz

    Many thanks for prompt response

    PHP config : /etc/php/7.4/cli/php.ini

  • 🇩🇪Germany Grevil

    Whoops, sry NULL is generally wrong for variable typing. As @Anybody correctly said it should be ?MenuLinkContent $link, which is compatible with php 7.1. MenuLinkContent|NULL is even wrong for php 8.0 where this kind of typing (Union Types) got introduced. The correct Syntax would be MenuLinkContent|null instead for php 8 (or ?MenuLinkContent).

  • Status changed to Needs review over 1 year ago
  • @grevil opened merge request.
  • 🇩🇪Germany Grevil

    Should be fixed. I haven't tested it myself yet though.

  • Issue was unassigned.
  • Status changed to RTBC over 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks @Grevil! Nullable types were introduced with PHP 7.1 so this should be fine. I'll tag a new release.
    https://www.php.net/manual/en/migration71.new-features.php

    @petednz could you please confirm it works now?

  • 🇩🇪Germany Anybody Porta Westfalica
  • Status changed to Fixed over 1 year ago
  • 🇳🇿New Zealand petednz

    many thanks for your prompt attention to this. much appreciated

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024