简体   繁体   中英

Drupal: count(): Parameter must be an array or an object that implements Countable

Warning: count(): Parameter must be an array or an object that implements Countable in invTranslate_translated_menu_link_alter() (line 55 from \\sites\\all\\modules\\custom\\invTranslate\\invTranslate.module).

invTranslate.module is a custom module.

function invTranslate_translated_menu_link_alter(&$item) {
  static $nodeMenu;
  if ($nodeMenu === NULL) {
    if (arg(0) == 'node' && count(arg() == 3 && (arg(1) == 'add' || arg(2) == 'edit'))) {
      $nodeMenu = true;
      ...

Line 55 is: if (arg(0) == 'node' && count(arg() == 3 && (arg(1) == 'add' || arg(2) == 'edit'))) { . Please help.

To me it seems there is a simple typo, however it depends on what your code should do. I've split the code to multiple rows for better readability:

if (
    arg(0) == 'node'
    && count(arg() == 3   //the count method takes as param the bool from the row below too
    && (arg(1) == 'add' || arg(2) == 'edit'))
) {

Instead it should look like this:

 if (
    arg(0) == 'node'
    && count(arg()) == 3   // add right bracket after arg()
    && (arg(1) == 'add' || arg(2) == 'edit')   // remove right bracket from here
) {

arg() Returns a component of the current Drupal path. When viewing a page at the path "admin/structure/types", for example, arg(0) returns "admin", arg(1) returns "structure", and arg(2) returns "types". https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/arg/7.x

In durpal the paths for nodes are like these

  • /node/add/{node-type}/
  • /node/{nid}/edit
  • /node/{nid}

Looking back at the code: if (arg(0) == 'node' && count(arg() == 3 && (arg(1) == 'add' || arg(2) == 'edit')))

I think, that condition should only pass for the first 2 of the paths I mentioned. So changing the code to the following should result in the expected behaviour: if (arg(0) == 'node' && count(arg()) == 3 && (arg(1) == 'add' || arg(2) == 'edit'))

The count() should only check if we have enough components in the path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM