Hi,

I was getting a strange error on my drupal instance:
Fatal error: [] operator not supported for strings in [my site]/includes/common.inc on line 2353

I've traced it down to this module. I thought it had to do with line 39, which is:
$item['options']['attributes']['class'] = 'menu-node-unpublished';

and probably should be:
$item['options']['attributes']['class'][] = 'menu-node-unpublished';

(I believe that entry should always be treated as an array). I tried it, but that conversion didn't help the problem. I felt it best to hand it back at this point to see if you had insight before I delved further.

The problem shows up when I'm attempting to view leaf nodes in my menu, and nowhere else.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

Final rewrite of that function (what works for my instance, anyways):

/**
 * Implements hook_menu_link_alter().
 */
function menu_view_unpublished_menu_link_alter(&$item) {
  // Add a class to menu links that link to unpublished nodes.
  if (preg_match('@^node/(\d+)$@', $item['link_path'], $matches)) {
    $node = node_load((int) $matches[1]);
    if (is_string($item['options']['attributes']['class'])) {
      $item['options']['attributes']['class'] = explode(' ', $item['options']['attributes']['class']);
    }
    if ($node && $node->status == NODE_NOT_PUBLISHED) {
      $item['options']['attributes']['class'] = array_unique(array_merge($item['options']['attributes']['class'], array('menu-node-unpublished')));
    }
  }
}
azinck’s picture

Priority: Critical » Normal
Status: Active » Needs review
FileSize
970 bytes

Here's a patch. In my experience there's some inconsistency in that sometimes the classes come through as an array and sometimes as a string.

casey’s picture

Status: Needs review » Fixed

Thanks, committed half of your patch. Unique classes is not our deal.

lahode’s picture

Status: Fixed » Active

Hi

I opened this ticket again because I noticed in version beta3, the patch proposed by azinck was not completely applied.

In fact, when I save my node several times, sometimes, the class "menu-node-unpublished" is added everytime, so that after 4 times (menu-node-unpublished menu-node-unpublished menu-node-unpublished menu-node-unpublished), I receive an error from Drupal telling me that the "class" field cannot have more than 128 caracters.

In azinck's patch #2, he wrote the following line which solve this problem:

      $item['options']['attributes']['class'] = array_unique(array_merge($item['options']['attributes']['class'], array('menu-node-unpublished')));

instead of

$item['options']['attributes']['class'][] = 'menu-node-unpublished';

Any reason not to integrate this line to the module ?

Cheers

Anonymous’s picture

Hi again. I made some other modifications to this function: the line from #2, as well as code to remove the class from the menu item once it actually is published.

Anonymous’s picture

Hi,

I noticed that the class "menu-node-unpublished" sometimes is not removed from a menu item after publishing the node belonging to the menu item. I applied the patch from #5, you state it might fix exactly that. But it didn't work. I cleared caches multiple times, un-/published nodes multiple times but the class is still being added to the item.

I'm using the module "menu_attributes". Editing a menu item directly actually shows the class in menu_attributes form. I think a conflict might be there, maybe it's the module weight, since menu_attributes always seems to override menu_view_unpublished?

I disabled menu_attributes and tried again, but the menu item's class still didn't change upon publish/unpublish.

gooddesignusa’s picture

genox, I actually got this working by changing the module a little. I will post a patch in the next day or so.

gooddesignusa’s picture

Issue summary: View changes

Removing domain name, irrelevant to the issue.

gooddesignusa’s picture

Issue summary: View changes
FileSize
3.87 KB

Sorry it took so long. I totally forgot about this issue.
This fixes the issues genox mentioned by using hook_node_presave(). This fixes the issue with menu_attributes and the menu-node-unpublished class issue.

Matroschker’s picture

I have implemented patch 'menu_view_unpublished-1696836-8-7.patch' - now the class will not be duplicated if I'm save as unpublished and publish later - that's good.

But how can I delete all the other old entries?

DaleTrexel’s picture

I found this issue because of the same problem genox mentioned in #6: publishing a node did not lead to the menu-node-unpublished class being removed. I wanted to report that I've tried the patch in #8, menu_view_unpublished-1696836-8-7.patch, and while it appears to be a step in the right direction, it's not quite there yet for my problem. After adding the patch, you have to save TWICE for the class to appear/disappear as expected.

My experience after applying the patch (this is consistent, repeatable behavior):
1. Create a new node in draft state, WITH a menu link set. Upon saving, the menu link does NOT have the menu-node-unpublished class present.

2. Save the node again, keeping the draft state, and the menu-node-unpublished DOES appear.

3. Publish the node, and the menu-node-unpublished class REMAINS.

4. Re-save the node as published and the menu-node-unpublished disappears.

In my case, I'm running a site with Workbench Moderation using Draft, Needs Review and Published states, though I don't suspect that this is involved.

Side note: I have also noted that menu-node-unpublished can be added multiple times to the same link (though I haven't dug into this to see if the number of repeats is related to, for example, the number of drafts). This is a behavior also noted in https://www.drupal.org/node/2414653 so these bug reports may be related.