With PHP 7.1+, lines 54 and 60 of special_menu_items.module are throwing an error: '[] operator not supported for strings'.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jmcerda created an issue. See original summary.

jmcerda’s picture

jmcerda’s picture

Assigned: jmcerda » Unassigned
Status: Active » Needs review
DamienMcKenna’s picture

Priority: Major » Normal
FileSize
787 bytes

I suspect this might be caused by another module or even the theme. It's common for people to make $variables['options']['attributes']['class'] a string when it really needs to be an array.

Please try this.

mark_fullmer’s picture

FileSize
736 bytes
1.19 KB

As described in the comment, the patch in #4 attempts to fix the problem of a site which is adding a class attribute as a string rather than an array.

However, the problem remains for PHP 7.1 and above for this line:

$variables['options']['attributes']['tabindex'][] = '0';

This line is still applying an empty index operator to a string, which is disallowed per https://www.php.net/manual/en/migration71.incompatible.php

Applying the empty index operator to a string (e.g. $str[] = $x) throws a fatal error instead of converting silently to array.

An additional problem with this line is that it will override any tabindex values sent to it by the l() function. There are valid scenarios where the <nolink> render should not be focusable.

The design of adding a tabindex to a span tag is understandable, so that it behaves similar to a link. However, this behavior should be the fallback, not the the only possibility.

We have a use case, for example, where we have adjacent links that point to the same destination, so for accessibility reasons, we want to set the tabindex to -1, as shown below:

  $link_options = [
    'attributes' => [
      'class' => ['cta'],
      'aria-hidden' => 'true',
      'tabindex' => '-1',
    ],
  ];
  $variables['read_more'] = (!empty($variables['link'])) ? l($default, $variables['link'], $link_options) : FALSE;

In short, this module should respect the tabindex value, if it is already provided, and if not, it can fallback to a 0 value that is not cast to array.

The attached patch includes the fix in #4 (see interdiff) and additionally fixes the tabindex fallback, while wrapping it in a conditional that will respect an existing value.

poker10’s picture

Thanks for reporting this.

Regarding the class attribute: This is not a bug of this module. Class must be an array as specified in the core documentation of theme_link function: https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_link/7.x

class: must be declared in an array. Example: 'class' => array('class_name1','class_name2').

I prefer not to "babysit" other modules/themes which does not follow documentation.

However I will take a look at the second problem (tabindex), which seems valid.

  • poker10 committed 55fc2d1b on 7.x-2.x authored by mark_fullmer
    Issue #3024643 by mark_fullmer: Fatal error: Uncaught Error: [] operator...
poker10’s picture

Status: Needs review » Fixed

Committed and fixed the problem with tabindex. Thanks all!

I plan to roll-out a new release with few fixes from today soon.

Status: Fixed » Closed (fixed)

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