@@ -343,6 +343,11 @@ private function cleanDisplayPreferences($itemtype, $so_id)
343343 // phpcs:ignore PSR1.Methods.CamelCapsMethodName
344344 public function pre_deleteItem ()
345345 {
346+ /**
347+ * @var \DBmysql $DB
348+ */
349+ global $ DB ;
350+
346351 //retrieve search option ID to clean DiplayPreferences
347352 $ container_obj = new PluginFieldsContainer ();
348353 $ container_obj ->getFromDB ($ this ->fields ['plugin_fields_containers_id ' ]);
@@ -389,7 +394,25 @@ public function pre_deleteItem()
389394 ]);
390395
391396 if ($ this ->fields ['type ' ] === 'dropdown ' ) {
392- return PluginFieldsDropdown::destroy ($ this ->fields ['name ' ]);
397+ //load all container (except current one) and check if another use this fields
398+ $ container_obj = new PluginFieldsContainer ();
399+ $ all_container = $ container_obj ->find (['id ' => ['!= ' , $ this ->fields ['plugin_fields_containers_id ' ]]]);
400+
401+ $ use_by_another = false ;
402+ foreach ($ all_container as $ container_fields ) {
403+ foreach (json_decode ($ container_fields ['itemtypes ' ]) as $ itemtype ) {
404+ $ dropdown_classname = PluginFieldsDropdown::getClassname ($ this ->fields ['name ' ]);
405+ $ classname = PluginFieldsContainer::getClassname ($ itemtype , $ container_fields ['name ' ]);
406+ $ dropdown_fk = getForeignKeyFieldForItemType ($ dropdown_classname );
407+ if ($ DB ->fieldExists (getTableForItemType ($ classname ), $ dropdown_fk )) {
408+ $ use_by_another = true ;
409+ }
410+ }
411+ }
412+
413+ if (!$ use_by_another ) {
414+ return PluginFieldsDropdown::destroy ($ this ->fields ['name ' ]);
415+ }
393416 }
394417
395418 return true ;
0 commit comments