Drupal: count(): 参数必须是数组或实现 Countable 的对象

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 是自定义模块。

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;
      ...

第 55 行是: if (arg(0) == 'node' && count(arg() == 3 && (arg(1) == 'add' || arg(2) == 'edit'))) {。 请帮忙。

对我来说,这似乎是一个简单的错字,但这取决于您的代码应该做什么。为了更好的可读性,我将代码分成多行:

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'))
) {

它应该看起来像这样:

 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

在 durpal 中,节点的路径如下所示

  • /node/add/{node-type}/
  • /节点/{nid}/编辑
  • /节点/{nid}

回顾一下代码: if (arg(0) == 'node' && count(arg() == 3 && (arg(1) == 'add' || arg(2) == 'edit')))

我认为,该条件应该只适用于我提到的前两条路径。因此,将代码更改为以下内容应该会产生预期的行为: if (arg(0) == 'node' && count(arg()) == 3 && (arg(1) == 'add' || arg(2) == 'edit'))

count() 应该只检查路径中是否有足够的组件。