I encountered a minor error when i am updating a node programmaticly.
Relevant modules:
- path module enabled
- pathauto module installed
- cck + ctools
I have a custom content type where i get the following error when i try to update a node programmaticly:
Notice: Undefined index: alias in path_node_update() (row 204 from \modules\path\path.module).
if i look in this function and i see:
function path_node_update($node) {
if (isset($node->path)) {
$path = $node->path;
$path['alias'] = trim($path['alias']);
// Delete old alias if user erased it.
if (!empty($path['pid']) && empty($path['alias'])) {
path_delete($path['pid']);
}
// Only save a non-empty alias.
if (!empty($path['alias'])) {
// Ensure fields for programmatic executions.
$path['source'] = 'node/' . $node->nid;
$path['language'] = isset($node->language) ? $node->language : LANGUAGE_NONE;
path_save($path);
}
}
}
the $node->path contains only Array ( [pathauto] => 1 ) .
This happens only if i run the below code:
$node = node_load($nodeId);
$node->title = 'test';
node_save($node);
I dont know if this is pathauto or the drupal core path module. the only way for me to solve it is hack into the core path.module which i dont preffer.
function path_node_update($node) {
if (isset($node->path) && !$node->path['pathauto'] ) {
$path = $node->path;
$path['alias'] = trim($path['alias']);
// Delete old alias if user erased it.
if (!empty($path['pid']) && empty($path['alias'])) {
path_delete($path['pid']);
}
// Only save a non-empty alias.
if (!empty($path['alias'])) {
// Ensure fields for programmatic executions.
$path['source'] = 'node/' . $node->nid;
$path['language'] = isset($node->language) ? $node->language : LANGUAGE_NONE;
path_save($path);
}
}
}
Comments
Comment #1
dave reidI think you are using the Pathauto persist module. It causes this bug but has been recently fixed: #1290304: Produces error in path_node_update()
Comment #2
donniewiko commentedI dont have that module installed. And i have read that error before (the search i did on my notice got me there).
So forgive me for opening it again, but i dont think its a duplicate
Comment #3
dave reidHrm, if you are doing just a node_load() and node_save() there is nothing that loads in data into $node->path at all. Neither core path module nor pathauto does this (neither module implements hook_node_load() nor hook_entity_load()). So I'm not sure why this would be happening.
Comment #4
dave reidComment #5
donniewiko commentedYea that was what i was thinking.
I have tried to debug the error but completed all functions that pathauto uses when saving (updating) a node.
Ill try and find out if this error is caused by a combination of my modules.
Comment #6
czigor commentedThe same error here when saving nodes in a form submit function.
Attached is my patch.
Comment #7
czigor commentedComment #8
geekyMoa commentedPatch in #6 worked for me.
Comment #9
dave reidTo also avoid this, you can set
$node->path['alias'] = '';and it will work without having to patch or hack any core/contrib code.Comment #10
dave reidLet's try this patch out to see if it can help prevent it from happening from others.
Comment #11
dave reidI have filed an issue for core to help prevent this from happening in the future: #1576552: Prevent PHP notices in path_node_insert/update if $node->path is defined but $node->path['alias'] is not..
I'm also going to commit #10 to 7.x-1.x to help prevent this from happening any more.
http://drupalcode.org/project/pathauto.git/commit/a92e087
Comment #13
lhangea commentedThis fix might also be an issue in certain situations. For example I want to regenerate the path alias automatically when adding a node to a menu (using node_save) but when I save the menu item the path alias doesn't update automatically , moreover the checkbox with 'Generate automatic URL alias ' is disabled automatically after saving the menu item. However if I do this from back-office it works and updates the path alias based on node having/not having a menu item.
Comment #14
lhangea commentedNever mind about the above post. I checked the module and realized that it does what it has to do. The path alias wasn't updating because I was trying to do everything in hook_menu_link_alter where the menu item is not yet saved and that's why the path stayed the save.
The solution was to add extra submit function which executes after the default submit functions where I could do the node_save() for path alias update.