Get this error after add a menu.

[Tue Oct 16 03:31:41.773149 2018] [proxy_fcgi:error] [pid 2240] [client 127.0.0.1:59155] AH01071: Got error 'PHP message: Uncaught PHP Exception Drupal\\Core\\Database\\IntegrityConstraintViolationException: "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id' cannot be null: INSERT INTO {menu_tree} (id, menu_name) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1); Array\n(\n [:db_insert_placeholder_0] => \n [:db_insert_placeholder_1] => \n)\n" at /home/203238.cloudwaysapps.com/gqcbnhmwqm/public_html/core/lib/Drupal/Core/Database/Connection.php line 683\n'

Thanks for ruining my life.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alanyong created an issue. See original summary.

Steven Buteneers’s picture

Yeah I'm also getting this issue when I add a menu link with node context. Then I get this error on evert node/add page.

This makes this module completely un-usable

Steven Buteneers’s picture

Title: The website encountered an unexpected error. Please try again later. » Integrity constraint violation: 1048 Column 'id' cannot be null
Steven Buteneers’s picture

Also seeing this log just before the FATAL error:

Notice: Undefined index: id in Drupal\Core\Menu\MenuTreeStorage->doSave() (regel 293 van /var/www/html/web/core/lib/Drupal/Core/Menu/MenuTreeStorage.php)
#0 /var/www/html/web/core/includes/bootstrap.inc(600): _drupal_error_handler_real(8, 'Undefined index...', '/var/www/html/w...', 293, Array)
#1 /var/www/html/web/core/lib/Drupal/Core/Menu/MenuTreeStorage.php(293): _drupal_error_handler(8, 'Undefined index...', '/var/www/html/w...', 293, Array)
#2 /var/www/html/web/core/lib/Drupal/Core/Menu/MenuTreeStorage.php(833): Drupal\Core\Menu\MenuTreeStorage->doSave(Array)
#3 /var/www/html/web/modules/contrib/menu_token/src/Service/MenuTokenTreeStorage.php(42): Drupal\Core\Menu\MenuTreeStorage->saveRecursive('menu_link_conte...', Array, Array)
#4 /var/www/html/web/modules/contrib/menu_token/src/Service/MenuTokenMenuLinkManager.php(25): Drupal\menu_token\Service\MenuTokenTreeStorage->rebuildNonDestructive(Array)
#5 /var/www/html/web/modules/contrib/menu_token/src/Service/MenuTokenContextManager.php(114): Drupal\menu_token\Service\MenuTokenMenuLinkManager->rebuildMenuToken(Array)
#6 /var/www/html/web/modules/contrib/menu_token/src/EventSubscriber/MenuTokenSubscriber.php(41): Drupal\menu_token\Service\MenuTokenContextManager->replaceContextualLinks()
Steven Buteneers’s picture

It looks like this is going wrong in \Drupal\menu_token\Service\MenuTokenTreeStorage::rebuildNonDestructive

Here an array of definitions is passed in keyed by the menu link ID, each definition however contains the following keys:
- Link (the actual link).
- Config (menu token config).

The code in \Drupal\menu_token\Service\MenuTokenTreeStorage::rebuildNonDestructive doesn't take this into account, because it is looping over the definitions as if they contain only the link.

This can be seen in this snippet:

foreach ($definitions as $id => $link) {
        // Flag this link as discovered, i.e. saved via rebuild().
        $link['discovered'] = 1;

      ....
}

In the snippet the $link variable does not contain the link itself but is a keyed array with the config and link variable. This is where everything goes wrong.

Steven Buteneers’s picture

Status: Active » Needs review
FileSize
1.18 KB

I found out that in \menu_token_prepare_context_replacement() an exception occurred while replacing tokens when on a node/add form. It fails because there is no actual node to use for replacement.

Because the exception occurs the following never happened:
$links[$key] = $links[$key]["link"];

Which explains the behaviour in #5.

The patch applied makes sure the links are prepared correctly.

vebrovski’s picture

Hi, can you write the steps to reproduce this issue so I can test it. Thanks.

Steven Buteneers’s picture

The steps I took were:

- Install the module
- Create a menu
- Add a link in the menu that requires node context (I uses [node:url:relative])
- Add a node of any type (node/add/xxx), you will get the error.

DevElCuy’s picture

Please somebody test and set the status of this issue to: "Reviewed & tested by community"

vebrovski’s picture

Status: Needs review » Reviewed & tested by the community

Hi, I tested the patch and went through debugging and it works ok. Thanks.

DevElCuy’s picture

Status: Reviewed & tested by the community » Fixed
geek-merlin’s picture

Status: Fixed » Closed (fixed)

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