sbo__relationship_widget.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. class sbo__relationship_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Relationship';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('sbo__relationship');
  7. /**
  8. *
  9. * @see TripalFieldWidget::form()
  10. */
  11. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  12. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  13. // TODO: make this widget deal better with the various relationship
  14. // tables. See the load function as it does a better job of this.
  15. return;
  16. // Get the field settings.
  17. $field_name = $this->field['field_name'];
  18. $field_type = $this->field['type'];
  19. $field_table = $this->instance['settings']['chado_table'];
  20. $field_column = $this->instance['settings']['chado_column'];
  21. $base_table = $this->instance['settings']['base_table'];
  22. // Get the FK column that links to the base table.
  23. $base_table = $this->instance['settings']['base_table'];
  24. $schema = chado_get_schema($field_table);
  25. $pkey = $schema['primary key'][0];
  26. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  27. $fkey = $fkeys[0];
  28. // Get the instance settings. There are three options for how this widget
  29. // will be displayed. Those are controlled in the instance settings
  30. // of the field.
  31. // Option 1: relationship types are limited to a specific vocabulary.
  32. // Option 2: relationship types are limited to a subset of one vocabulary.
  33. // Option 3: relationship types are limited to a predefined set.
  34. $instance = $this->instance;
  35. $settings = '';
  36. $option1_vocabs = '';
  37. $option2_parent = '';
  38. $option2_vocab = '';
  39. $option3_rtypes = '';
  40. if (array_key_exists('relationships', $instance)) {
  41. $settings = $instance['settings']['relationships'];
  42. $option1_vocabs = $settings['option1_vocabs'];
  43. $option2_vocab = $settings['option2_vocab'];
  44. $option2_parent = $settings['option2_parent'];
  45. $option3_rtypes = $settings['relationship_types'];
  46. }
  47. // For testing if there are selected vocabs for option1 we'll copy the
  48. // contents in a special variable for later.
  49. $option1_test = $option1_vocabs;
  50. // Get the field defaults.
  51. $record_id = '';
  52. $subject_id = '';
  53. $object_id = '';
  54. $type_id = '';
  55. $value = '';
  56. $rank = '';
  57. $subject_uniquename = '';
  58. $object_uniquename = '';
  59. $type = '';
  60. // 'nd_reagent_relationship' and 'project_relationship' have different column names from
  61. // subject_id/object_id. Do a pattern matching to get the column names.
  62. $subject_id_key = 'subject_id';
  63. $object_id_key = 'object_id';
  64. foreach ($schema['foreign keys'][$base_table]['columns'] AS $lcolum => $rcolum) {
  65. if (preg_match('/^subject_.*id/', $lcolum)) {
  66. $subject_id_key = $lcolum;
  67. }
  68. else if (preg_match('/^object_.*id/', $lcolum)) {
  69. $object_id_key = $lcolum;
  70. }
  71. }
  72. // If the field already has a value then it will come through the $items
  73. // array. This happens when editing an existing record.
  74. if (count($items) > 0 and array_key_exists($delta, $items)) {
  75. // Check for element values that correspond to fields in the Chado table.
  76. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  77. $subject_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $subject_id_key, $subject_id);
  78. $object_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $object_id_key, $object_id);
  79. $type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
  80. // Not all Chado tables have a value and rank. So we'll only get
  81. // those if applicable.
  82. if (array_key_exists('value', $schema['fields'])) {
  83. $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__value', $value);
  84. }
  85. if (array_key_exists('rank', $schema['fields'])) {
  86. $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
  87. }
  88. // Get element values added to help support insert/updates.
  89. $object_uniquename = tripal_get_field_item_keyval($items, $delta, 'object_name', $object_uniquename);
  90. $subject_uniquename = tripal_get_field_item_keyval($items, $delta, 'subject_name', $subject_uniquename);
  91. $type = tripal_get_field_item_keyval($items, $delta, 'type_name', $type);
  92. }
  93. // Check $form_state['values'] to see if an AJAX call set the values.
  94. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  95. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  96. $subject_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key];
  97. $object_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $object_id_key];
  98. $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
  99. if (array_key_exists('value', $schema['fields'])) {
  100. $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
  101. }
  102. if (array_key_exists('rank', $schema['fields'])) {
  103. $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
  104. }
  105. $object_uniquename = $form_state['values'][$field_name]['und'][$delta]['object_name'];
  106. $subject_uniquename = $form_state['values'][$field_name]['und'][$delta]['subject_name'];
  107. $type = $form_state['values'][$field_name]['und'][$delta]['type_name'];
  108. }
  109. $widget['#table_name'] = $field_table;
  110. $widget['#fkeys'] = $schema['foreign keys'];
  111. $widget['#base_table'] = $base_table;
  112. $widget['#chado_record_id'] = isset($form['#entity']) ? $form['#entity']->chado_record_id : '';
  113. //$widget['#element_validate'] = array('sbo__relationship_validate');
  114. $widget['#prefix'] = "<span id='$field_table-$delta'>";
  115. $widget['#suffix'] = "</span>";
  116. $widget['value'] = array(
  117. '#type' => 'value',
  118. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  119. );
  120. $widget['chado-' . $field_table . '__' . $pkey] = array(
  121. '#type' => 'value',
  122. '#default_value' => $record_id,
  123. );
  124. $widget['chado-' . $field_table . '__' . $subject_id_key] = array(
  125. '#type' => 'value',
  126. '#default_value' => $subject_id,
  127. );
  128. $widget['chado-' . $field_table . '__type_id'] = array(
  129. '#type' => 'value',
  130. '#default_value' => $type_id,
  131. );
  132. $widget['chado-' . $field_table . '__' . $object_id_key] = array(
  133. '#type' => 'value',
  134. '#default_value' => $object_id,
  135. );
  136. if (array_key_exists('value', $schema['fields'])) {
  137. $widget['chado-' . $field_table . '__value'] = array(
  138. '#type' => 'value',
  139. '#default_value' => $value,
  140. );
  141. }
  142. if (array_key_exists('rank', $schema['fields'])) {
  143. $widget['chado-' . $field_table . '__rank'] = array(
  144. '#type' => 'value',
  145. '#default_value' => $rank,
  146. );
  147. }
  148. $widget['subject_name'] = array(
  149. '#type' => 'textfield',
  150. '#title' => t('Subject'),
  151. '#default_value' => $subject_uniquename,
  152. '#required' => $element['#required'],
  153. '#maxlength' => array_key_exists($subject_id_key, $schema['fields']) && array_key_exists('length', $schema['fields'][$subject_id_key]) ? $schema['fields'][$subject_id_key]['length'] : 255,
  154. '#size' => 35,
  155. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  156. );
  157. // Getting default values for the relationship type element.
  158. $default_voc = '';
  159. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
  160. $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
  161. }
  162. $default_term = '';
  163. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
  164. $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
  165. }
  166. $default_type_id = $type_id;
  167. if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
  168. $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
  169. }
  170. // Option 3: Custom list of Relationship Types
  171. $rtype_options = array();
  172. $rtype_options[] = 'Select a Type';
  173. if ($option3_rtypes) {
  174. $rtypes = explode(PHP_EOL, $option3_rtypes);
  175. foreach($rtypes AS $rtype) {
  176. // Ignore empty lines
  177. if (trim($rtype) == '') {
  178. continue;
  179. }
  180. $term = tripal_get_cvterm(array('name' => trim($rtype)));
  181. // Try to get term with vocabulary specified
  182. if (!$term) {
  183. $tmp = explode('|', trim($rtype), 2);
  184. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  185. $rtype = trim($tmp[1]);
  186. $term = tripal_get_cvterm(array('name' => $rtype, 'cv_id' => $cv->cv_id));
  187. }
  188. $rtype_options[$term->cvterm_id] = $term->name;
  189. }
  190. $widget['type_id'] = array(
  191. '#type' => 'select',
  192. '#title' => t('Relationship Type'),
  193. '#options' => $rtype_options,
  194. '#default_value' => $default_type_id,
  195. );
  196. if ($type_id && !key_exists($type_id, $rtype_options)) {
  197. form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
  198. }
  199. }
  200. // Option 2: Child terms of a selected cvterm
  201. else if ($option2_vocab) {
  202. $values = array(
  203. 'cv_id' => $option2_vocab,
  204. 'name' => $option2_parent
  205. );
  206. $parent_term = tripal_get_cvterm($values);
  207. // If the term wasn't found then see if it's a synonym.
  208. if(!$parent_term) {
  209. $values = array(
  210. 'synonym' => array(
  211. 'name' => trim($option2_parent),
  212. )
  213. );
  214. $synonym = tripal_get_cvterm($values);
  215. if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
  216. $parent_term = $synonym;
  217. }
  218. }
  219. // Get the child terms of the parent term found above.
  220. $sql = "
  221. SELECT subject_id,
  222. (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
  223. FROM {cvtermpath}
  224. WHERE
  225. object_id = :parent_cvterm_id AND
  226. cv_id = :parent_cv_id
  227. ORDER BY name
  228. ";
  229. $args = array(
  230. ':parent_cvterm_id' => $parent_term->cvterm_id,
  231. ':parent_cv_id' => $parent_term->cv_id->cv_id
  232. );
  233. $results = chado_query($sql, $args);
  234. while($child = $results->fetchObject()) {
  235. $rtype_options[$child->subject_id] = $child->name;
  236. }
  237. $widget['type_id'] = array(
  238. '#type' => 'select',
  239. '#title' => t('Relationship Type'),
  240. '#options' => $rtype_options,
  241. '#default_value' => $default_type_id,
  242. );
  243. if ($type_id && !key_exists($type_id, $rtype_options)) {
  244. form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
  245. }
  246. }
  247. // Option 1: All terms of selected vocabularies
  248. else if ($option1_test && array_pop($option1_test)) {
  249. $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
  250. $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
  251. while ($obj = $results->fetchObject()) {
  252. $rtype_options[$obj->cvterm_id] = $obj->name;
  253. }
  254. $widget['type_id'] = array(
  255. '#type' => 'select',
  256. '#title' => t('Relationship Type'),
  257. '#options' => $rtype_options,
  258. '#default_value' => $default_type_id,
  259. );
  260. if ($type_id && !key_exists($type_id, $rtype_options)) {
  261. form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
  262. }
  263. }
  264. // Default option:
  265. else {
  266. // Set up available cvterms for selection
  267. $vocs = array(0 => 'Select a vocabulary');
  268. $vocs = tripal_get_cv_select_options();
  269. $cv_id = isset($form_state['values'][$field_name]['und'][0]['vocabulary']) ? $form_state['values'][$field_name]['und'][0]['vocabulary'] : 0;
  270. // Try getting the cv_id from cvterm for existing records
  271. if (!$cv_id && $type_id) {
  272. $cvterm = tripal_get_cvterm(array('cvterm_id' => $type_id));
  273. if (isset($cvterm->cv_id->cv_id)) {
  274. $cv_id = $cvterm->cv_id->cv_id;
  275. $default_term = $cvterm->name;
  276. }
  277. }
  278. if (!$cv_id) {
  279. $cv_id = $default_voc;
  280. }
  281. $widget['vocabulary'] = array(
  282. '#type' => 'select',
  283. '#title' => t('Vocabulary'),
  284. '#options' => $vocs,
  285. '#required' => $element['#required'],
  286. '#default_value' => $cv_id,
  287. '#ajax' => array(
  288. 'callback' => "sbo__relationship_widget_form_ajax_callback",
  289. 'wrapper' => "$field_table-$delta",
  290. 'effect' => 'fade',
  291. 'method' => 'replace'
  292. ),
  293. );
  294. if ($cv_id) {
  295. $options = array();
  296. $widget['type_name'] = array(
  297. '#type' => 'textfield',
  298. '#title' => t('Relationship Type'),
  299. '#size' => 15,
  300. '#default_value' => $default_term,
  301. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
  302. );
  303. }
  304. }
  305. $widget['object_name'] = array(
  306. '#type' => 'textfield',
  307. '#title' => t('Object'),
  308. '#default_value' => $object_uniquename,
  309. '#required' => $element['#required'],
  310. '#maxlength' => array_key_exists($object_id_key, $schema['fields']) && array_key_exists('length', $schema['fields'][$object_id_key]) ? $schema['fields'][$object_id_key]['length'] : 255,
  311. '#size' => 35,
  312. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  313. );
  314. }
  315. /**
  316. *
  317. * @see TripalFieldWidget::validate()
  318. */
  319. public function validate($element, $form, &$form_state, $langcode, $delta) {
  320. $field_name = $this->field['field_name'];
  321. $field_type = $this->field['type'];
  322. $field_table = $this->instance['settings']['chado_table'];
  323. $field_column = $this->instance['settings']['chado_column'];
  324. $base_table = $this->instance['settings']['base_table'];
  325. $chado_record_id = array_key_exists('#entity', $element)? $element['#entity']->chado_record_id : NULL;
  326. $schema = chado_get_schema($field_table);
  327. $fkeys = $schema['foreign keys'];
  328. // 'nd_reagent_relationship' and 'project_relationship' have different column names from
  329. // subject_id/object_id. Do a pattern matching to get the column names.
  330. $subject_id_key = 'subject_id';
  331. $object_id_key = 'object_id';
  332. foreach ($schema['foreign keys'][$base_table]['columns'] AS $lcolum => $rcolum) {
  333. if (preg_match('/^subject_.*id/', $lcolum)) {
  334. $subject_id_key = $lcolum;
  335. }
  336. else if (preg_match('/^object_.*id/', $lcolum)) {
  337. $object_id_key = $lcolum;
  338. }
  339. }
  340. $voc_id = array_key_exists('vocabulary', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : '';
  341. $type_name = array_key_exists('type_name', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['type_name'] : '';
  342. $subject_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] : '';
  343. $object_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key]: '';
  344. $type_id = $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'];
  345. $subject_name = $form_state['values'][$field_name][$langcode][$delta]['subject_name'];
  346. $object_name = $form_state['values'][$field_name][$langcode][$delta]['object_name'];
  347. // If the row is empty then skip this one, there's nothing to validate.
  348. if (!($type_id or !$type_name) and !$subject_name and !$object_name) {
  349. return;
  350. }
  351. else if ($type_name && $voc_id) {
  352. $val = array(
  353. 'cv_id' => $voc_id,
  354. 'name' => $type_name
  355. );
  356. $cvterm = chado_generate_var('cvterm', $val);
  357. $type_id = $cvterm->cvterm_id;
  358. }
  359. // Do not proceed if subject ID or object ID does not exist
  360. if (!key_exists($subject_id_key, $fkeys[$base_table]['columns']) ||
  361. !key_exists($object_id_key, $fkeys[$base_table]['columns'])) {
  362. return;
  363. }
  364. // Get the subject ID.
  365. $subject_id = '';
  366. $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
  367. $matches = array();
  368. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  369. $subject_id = $matches[1];
  370. }
  371. else {
  372. $values = array('uniquename' => $subject_name);
  373. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  374. if(count($subject) > 0) {
  375. $subject_id = $subject[0]->$fkey_rcolumn;
  376. }
  377. }
  378. // Get the object ID.
  379. $object_id = '';
  380. $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
  381. $matches = array();
  382. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  383. $object_id = $matches[1];
  384. }
  385. else {
  386. $values = array('uniquename' => $object_name);
  387. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  388. if (count($object) > 0) {
  389. $object_id = $object[0]->$fkey_rcolumn;
  390. }
  391. }
  392. if ($subject_id && $object_id && $type_id) {
  393. // Set the IDs according to the values that were determined above.
  394. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $subject_id;
  395. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
  396. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
  397. if (array_key_exists('rank', $schema['fields'])) {
  398. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
  399. }
  400. }
  401. else {
  402. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = '';
  403. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = '';
  404. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = '';
  405. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__value'] = '';
  406. if (array_key_exists('rank', $schema['fields'])) {
  407. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = '';
  408. }
  409. }
  410. }
  411. /**
  412. * Theme function for the sbo__relationship_widget.
  413. */
  414. public function theme($element) {
  415. $layout = "
  416. <div class=\"chado-linker--relationship-widget\">
  417. <div class=\"chado-linker--relationship-widget-item\">" .
  418. drupal_render($element['subject_name']) . "
  419. </div>
  420. <div class=\"chado-linker--relationship-widget-item\">" .
  421. drupal_render($element['vocabulary']) . "
  422. </div>
  423. <div class=\"chado-linker--relationship-widget-item\">" .
  424. drupal_render($element['type_name']) . "
  425. </div>
  426. <div class=\"chado-linker--relationship-widget-item\">" .
  427. drupal_render($element['type_id']) . "
  428. </div>
  429. <div>" .
  430. drupal_render($element['object_name']) . "
  431. </div>
  432. </div>
  433. ";
  434. return $layout;
  435. }
  436. }
  437. function theme_sbo__relationship_instance_settings ($variables) {
  438. $element = $variables['element'];
  439. $option1 = $element['option1'];
  440. $option1_vocabs = $element['option1_vocabs'];
  441. $option2 = $element['option2'];
  442. $option2_vocab = $element['option2_vocab'];
  443. $option2_parent = $element['option2_parent'];
  444. $option3 = $element['option3'];
  445. $rtype = $element['relationship_types'];
  446. $layout = drupal_render($option1);
  447. $layout .= drupal_render($option1_vocabs);
  448. $layout .=
  449. drupal_render($option2) .
  450. "<div class=\"chado-linker--relationship-instance-settings-option2\">" .
  451. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  452. drupal_render($option2_vocab) .
  453. "</div>" .
  454. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  455. drupal_render($option2_parent) .
  456. "</div>" .
  457. "</div>";
  458. $layout .= drupal_render($option3);
  459. $layout .= drupal_render($rtype);
  460. return $layout;
  461. }
  462. /**
  463. * An Ajax callback for the relationshp widget.
  464. */
  465. function sbo__relationship_widget_form_ajax_callback(&$form, $form_state) {
  466. // Get the triggering element
  467. $form_element_name = $form_state['triggering_element']['#name'];
  468. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  469. $field = $matches[1];
  470. $lang = $matches[2];
  471. $delta = $matches[3];
  472. // Return the widget that triggered the AJAX call
  473. if (isset($form[$field][$lang][$delta])) {
  474. return $form[$field][$lang][$delta];
  475. }
  476. // Alternatively, return the default value widget for the widget setting form
  477. else {
  478. return $form['instance']['default_value_widget'][$field];
  479. }
  480. }
  481. /**
  482. * An Ajax callback for the relationshp instance setting form.
  483. */
  484. function sbo__relationship_instance_settings_form_ajax_callback(&$form, &$form_state) {
  485. $acpath = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'];
  486. $acpath .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'] . '/';
  487. $urlval = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'];
  488. $urlval .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'];
  489. // Reset value if a different vocabulary is selected
  490. $form['instance']['settings']['relationships']['option2_parent']['#value'] = NULL;
  491. $form_state['values']['instance']['settings']['relationships']['option2_parent'] = NULL;
  492. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'] = $acpath;
  493. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'] = $urlval;
  494. return $form['instance']['settings']['relationships']['option2_parent'];
  495. }