【问题标题】:Drupal: count(): Parameter must be an array or an object that implements CountableDrupal:count():参数必须是数组或者实现了Countable的对象
【发布时间】:2019-10-04 12:52:47
【问题描述】:

警告:count():参数必须是在 invTranslate_translated_menu_link_alter() 中实现 Countable 的数组或对象(来自 \sites\all\modules\custom\invTranslate\invTranslate.module 的第 55 行)。

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'))) {。 请帮忙。

【问题讨论】:

  • arg() == 3 && (arg(1) == 'add' || arg(2) == 'edit') 是一个逻辑表达式,它会导致真或假。对 true 或 false 使用 count 只会产生零意义。
  • 您能告诉我如何将该代码更改为正确的代码吗?
  • @04FS 只是删除“计数”?所以结果是if (arg(0) == 'node' && (arg() == 3 && (arg(1) == 'add' || arg(2) == 'edit'))) {
  • 这是最有可能的猜测,是的。 (猜猜看,因为我们还没有被告知这首先应该实现什么。)
  • 取决于arg() 返回的内容,它可能是count(arg()) == 3

标签: php drupal


【解决方案1】:

arg() 返回当前 Drupal 路径的一个组件。 例如,当查看路径为“admin/structure/types”的页面时,arg(0) 返回“admin”,arg(1) 返回“structure”,arg(2) 返回“types”。 https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/arg/7.x

在 durpal 中,节点的路径是这样的

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

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

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

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

【讨论】:

    【解决方案2】:

    对我来说,似乎有一个简单的错字,但这取决于您的代码应该做什么。我已将代码拆分为多行以提高可读性:

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

    【讨论】:

    • @sailormoon 请考虑将此答案作为最终答案投票(左侧的绿色标记),如果它有助于您解决此案。谢谢
    • 感谢您的帮助,最终答案是您的。
    猜你喜欢
    • 2019-07-23
    • 2018-06-28
    • 2018-09-05
    • 2019-08-03
    • 2019-06-02
    • 2018-06-08
    • 2018-09-22
    • 1970-01-01
    • 2018-11-18
    相关资源
    最近更新 更多