I noticed a bug with the module node_reference 7.x-2.0-beta3.
case:
- Node ID10 refers to node ID20
- Node ID20 is deleted
- When will the node ID10 was the result:
Notice : Undefined property: stdClass::$type dans _node_extract_type() (ligne 370 dans /.../drupal-7/modules/node/node.module).
Notice : Undefined property: stdClass::$nid dans node_build_content() (ligne 1359 dans /.../drupal-7/modules/node/node.module).
EntityMalformedException : Propriété de bundle manquante sur l'entité de type node. dans entity_extract_ids() (ligne 7389 dans/.../drupal-7/includes/common.inc).
I used the delete hook node to solve this problem:
/**
* hook_node_delete
*/
function fonctions_globales_node_delete($node) {
//print_sr($node);
// Correctif bug module Reference 7.x-2.0-beta3
$query = db_select('field_config', 'fc')
->fields('fc',array('field_name'))
->condition('fc.type', 'node_reference');
$result = $query->execute();
foreach ($result as $v) {
//print_sr($v);
db_delete('field_data_'.$v->field_name)
->condition($v->field_name.'_nid', $node->nid)
->execute();
db_delete('field_revision_'.$v->field_name)
->condition($v->field_name.'_nid', $node->nid)
->execute();
}
field_cache_clear(); // /!\ IMPORTANT!
//exit;
}
Greetings.
Comment | File | Size | Author |
---|---|---|---|
#10 | references_clean.zip | 1.54 KB | modstore |
Comments
Comment #1
xjmIf I'm not mistaken, with this code,
field_cache_clear()
will be called N times when multiple nodes are deleted.Comment #2
hefox CreditAttribution: hefox commentedTrying to clearify title
d7 core issue with taxonomy #687180: Deleting a taxonomy vocabulary leaves term reference fields still pointing to it, and a PDO Exception when creating content
D6 cck issue #83929: referential integrity
Issue is with node reference and user reference.
Comment #3
hefox CreditAttribution: hefox commentedAn update function is needed to delete existing incorrect references.
Likely need to do something on cron due to the potential large amount of references being deleted.
Comment #4
Clément CreditAttribution: Clément commentedYou're right xjm, I corrected this way:
I'm not really for this task when cron because during 1H (1 hour by cron) nodes in question are no longer accessible. I find it problematic.
Comment #5
hefox CreditAttribution: hefox commentedComment #6
xjmRegarding deleting on cron, it should at least be a queue in some form, because otherwise this code is guaranteed to break large sites. See: #89181: Use queue API for node and comment, user, node multiple deletes.
Comment #7
Clément CreditAttribution: Clément commentedThank you for your link. I'll watch.
Greetings.
Comment #8
David_Rothstein CreditAttribution: David_Rothstein commentedJust a quick note that the code here should probably use hook_entity_delete() rather than hook_node_delete(), so that nodes and users can be dealt with similarly?
Comment #9
Dave ReidProbably not since the sub-modules for node and user are separate and hard-coded to either node or user.
Comment #10
modstore CreditAttribution: modstore commentedI created a module that runs in the queue. Full module attached.
If you want it to run more often that every hour, I suggest using Elysia cron, and you can specify how often each of your cron modules should run.
Comment #11
sashainparisElysia cron will help run *less_often*: if hosting is the reason why your cron is triggered only once an hour, Elysia won't help.
To be pragmatic, I believe this choice should need a setting to decide the way treatment should be triggered: Cron vs. "On delete".
Thanks all for this work!
Alexandre
Comment #12
antiorario CreditAttribution: antiorario commentedI created a module to address this issue for a client, and was thinking about submitting it to References for inclusion, but since I'm not very clear on References' current state of development, I decided to publish Reference Delete as a new module. The first dev release will be available later, but the module is already on git.
It works on node delete and user delete (not cron). It might be clumsy and/or rough around the edges, so I'm open to suggestions.
Comment #13
domidc CreditAttribution: domidc commentedRaising to critical.
This is a serious shortcoming in the references module. Not deleting the occurrences of nodes in ohter reference fields creates serious inconsistencies.
For example the search api apachesolr module indexes items which the entity api tries to load this causes these kinds of errors: EntityMetadataWrapperException: Unable to get the data property location as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue()
The functionality discussed should be committed in the main references module.
Comment #14
David_Rothstein CreditAttribution: David_Rothstein commentedAnother module that might help here (currently just a sandbox module) is Field reference delete.
Comment #15
peterx CreditAttribution: peterx commentedThe delete could register a shutdown function to run after the page processing is finished. You then get everything processed almost immediately without holding up the current page. You could copy the process from Poormanscron.
Comment #16
Fabianx CreditAttribution: Fabianx commentedThere is also ultimate_cron to run a cron process much more often ...
Comment #17
maxq10 CreditAttribution: maxq10 commentedNot much of a PHP guy, but I am having a similar problem. If I used the code in #10, what would be a good way to modify that to account for nodes that had since been unpublished (but not deleted)?
Comment #17.0
maxq10 CreditAttribution: maxq10 commentedRemoving the full path of the error
Comment #18
hefox CreditAttribution: hefox commentedJust dropping this here, non-cron way that works with entity cache
Comment #19
khurrami CreditAttribution: khurrami commentedThanks a lot #4 helped me