sbo__relationship_widget.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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
  95. array_key_exists($field_name, $form_state['values'])) {
  96. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
  97. $subject_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key];
  98. $object_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $object_id_key];
  99. $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
  100. if (array_key_exists('value', $schema['fields'])) {
  101. $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
  102. }
  103. if (array_key_exists('rank', $schema['fields'])) {
  104. $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
  105. }
  106. $object_uniquename = $form_state['values'][$field_name]['und'][$delta]['object_name'];
  107. $subject_uniquename = $form_state['values'][$field_name]['und'][$delta]['subject_name'];
  108. $type = $form_state['values'][$field_name]['und'][$delta]['type_name'];
  109. }
  110. $widget['#table_name'] = $field_table;
  111. $widget['#fkeys'] = $schema['foreign keys'];
  112. $widget['#base_table'] = $base_table;
  113. $widget['#chado_record_id'] = isset($form['#entity']) ? $form['#entity']->chado_record_id : '';
  114. //$widget['#element_validate'] = array('sbo__relationship_validate');
  115. $widget['#prefix'] = "<span id='$field_table-$delta'>";
  116. $widget['#suffix'] = "</span>";
  117. $widget['value'] = array(
  118. '#type' => 'value',
  119. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  120. );
  121. $widget['chado-' . $field_table . '__' . $pkey] = array(
  122. '#type' => 'value',
  123. '#default_value' => $record_id,
  124. );
  125. $widget['chado-' . $field_table . '__' . $subject_id_key] = array(
  126. '#type' => 'value',
  127. '#default_value' => $subject_id,
  128. );
  129. $widget['chado-' . $field_table . '__type_id'] = array(
  130. '#type' => 'value',
  131. '#default_value' => $type_id,
  132. );
  133. $widget['chado-' . $field_table . '__' . $object_id_key] = array(
  134. '#type' => 'value',
  135. '#default_value' => $object_id,
  136. );
  137. if (array_key_exists('value', $schema['fields'])) {
  138. $widget['chado-' . $field_table . '__value'] = array(
  139. '#type' => 'value',
  140. '#default_value' => $value,
  141. );
  142. }
  143. if (array_key_exists('rank', $schema['fields'])) {
  144. $widget['chado-' . $field_table . '__rank'] = array(
  145. '#type' => 'value',
  146. '#default_value' => $rank,
  147. );
  148. }
  149. $widget['subject_name'] = array(
  150. '#type' => 'textfield',
  151. '#title' => t('Subject'),
  152. '#default_value' => $subject_uniquename,
  153. '#required' => $element['#required'],
  154. '#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,
  155. '#size' => 35,
  156. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  157. );
  158. // Getting default values for the relationship type element.
  159. $default_voc = '';
  160. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
  161. $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
  162. }
  163. $default_term = '';
  164. if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
  165. $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
  166. }
  167. $default_type_id = $type_id;
  168. if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
  169. $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
  170. }
  171. // Option 3: Custom list of Relationship Types
  172. $rtype_options = array();
  173. $rtype_options[] = 'Select a Type';
  174. if ($option3_rtypes) {
  175. $rtypes = explode(PHP_EOL, $option3_rtypes);
  176. foreach($rtypes AS $rtype) {
  177. // Ignore empty lines
  178. if (trim($rtype) == '') {
  179. continue;
  180. }
  181. $term = chado_get_cvterm(array('name' => trim($rtype)));
  182. // Try to get term with vocabulary specified
  183. if (!$term) {
  184. $tmp = explode('|', trim($rtype), 2);
  185. $cv = chado_get_cv(array('name' => trim($tmp[0])));
  186. $rtype = trim($tmp[1]);
  187. $term = chado_get_cvterm(array('name' => $rtype, 'cv_id' => $cv->cv_id));
  188. }
  189. $rtype_options[$term->cvterm_id] = $term->name;
  190. }
  191. $widget['type_id'] = array(
  192. '#type' => 'select',
  193. '#title' => t('Relationship Type'),
  194. '#options' => $rtype_options,
  195. '#default_value' => $default_type_id,
  196. );
  197. if ($type_id && !key_exists($type_id, $rtype_options)) {
  198. 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');
  199. }
  200. }
  201. // Option 2: Child terms of a selected cvterm
  202. else if ($option2_vocab) {
  203. $values = array(
  204. 'cv_id' => $option2_vocab,
  205. 'name' => $option2_parent
  206. );
  207. $parent_term = chado_get_cvterm($values);
  208. // If the term wasn't found then see if it's a synonym.
  209. if(!$parent_term) {
  210. $values = array(
  211. 'synonym' => array(
  212. 'name' => trim($option2_parent),
  213. )
  214. );
  215. $synonym = chado_get_cvterm($values);
  216. if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
  217. $parent_term = $synonym;
  218. }
  219. }
  220. // Get the child terms of the parent term found above.
  221. $sql = "
  222. SELECT subject_id,
  223. (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
  224. FROM {cvtermpath}
  225. WHERE
  226. object_id = :parent_cvterm_id AND
  227. cv_id = :parent_cv_id
  228. ORDER BY name
  229. ";
  230. $args = array(
  231. ':parent_cvterm_id' => $parent_term->cvterm_id,
  232. ':parent_cv_id' => $parent_term->cv_id->cv_id
  233. );
  234. $results = chado_query($sql, $args);
  235. while($child = $results->fetchObject()) {
  236. $rtype_options[$child->subject_id] = $child->name;
  237. }
  238. $widget['type_id'] = array(
  239. '#type' => 'select',
  240. '#title' => t('Relationship Type'),
  241. '#options' => $rtype_options,
  242. '#default_value' => $default_type_id,
  243. );
  244. if ($type_id && !key_exists($type_id, $rtype_options)) {
  245. 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');
  246. }
  247. }
  248. // Option 1: All terms of selected vocabularies
  249. else if ($option1_test && array_pop($option1_test)) {
  250. $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
  251. $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
  252. while ($obj = $results->fetchObject()) {
  253. $rtype_options[$obj->cvterm_id] = $obj->name;
  254. }
  255. $widget['type_id'] = array(
  256. '#type' => 'select',
  257. '#title' => t('Relationship Type'),
  258. '#options' => $rtype_options,
  259. '#default_value' => $default_type_id,
  260. );
  261. if ($type_id && !key_exists($type_id, $rtype_options)) {
  262. 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');
  263. }
  264. }
  265. // Default option:
  266. else {
  267. // Set up available cvterms for selection
  268. $vocs = array(0 => 'Select a vocabulary');
  269. $vocs = chado_get_cv_select_options();
  270. $cv_id = isset($form_state['values'][$field_name]['und'][0]['vocabulary']) ? $form_state['values'][$field_name]['und'][0]['vocabulary'] : 0;
  271. // Try getting the cv_id from cvterm for existing records
  272. if (!$cv_id && $type_id) {
  273. $cvterm = chado_get_cvterm(array('cvterm_id' => $type_id));
  274. if (isset($cvterm->cv_id->cv_id)) {
  275. $cv_id = $cvterm->cv_id->cv_id;
  276. $default_term = $cvterm->name;
  277. }
  278. }
  279. if (!$cv_id) {
  280. $cv_id = $default_voc;
  281. }
  282. $widget['vocabulary'] = array(
  283. '#type' => 'select',
  284. '#title' => t('Vocabulary'),
  285. '#options' => $vocs,
  286. '#required' => $element['#required'],
  287. '#default_value' => $cv_id,
  288. '#ajax' => array(
  289. 'callback' => "sbo__relationship_widget_form_ajax_callback",
  290. 'wrapper' => "$field_table-$delta",
  291. 'effect' => 'fade',
  292. 'method' => 'replace'
  293. ),
  294. );
  295. if ($cv_id) {
  296. $options = array();
  297. $widget['type_name'] = array(
  298. '#type' => 'textfield',
  299. '#title' => t('Relationship Type'),
  300. '#size' => 15,
  301. '#default_value' => $default_term,
  302. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
  303. );
  304. }
  305. }
  306. $widget['object_name'] = array(
  307. '#type' => 'textfield',
  308. '#title' => t('Object'),
  309. '#default_value' => $object_uniquename,
  310. '#required' => $element['#required'],
  311. '#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,
  312. '#size' => 35,
  313. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
  314. );
  315. }
  316. /**
  317. *
  318. * @see TripalFieldWidget::validate()
  319. */
  320. public function validate($element, $form, &$form_state, $langcode, $delta) {
  321. $field_name = $this->field['field_name'];
  322. $field_type = $this->field['type'];
  323. $field_table = $this->instance['settings']['chado_table'];
  324. $field_column = $this->instance['settings']['chado_column'];
  325. $base_table = $this->instance['settings']['base_table'];
  326. $chado_record_id = array_key_exists('#entity', $element)? $element['#entity']->chado_record_id : NULL;
  327. $schema = chado_get_schema($field_table);
  328. $fkeys = $schema['foreign keys'];
  329. // 'nd_reagent_relationship' and 'project_relationship' have different column names from
  330. // subject_id/object_id. Do a pattern matching to get the column names.
  331. $subject_id_key = 'subject_id';
  332. $object_id_key = 'object_id';
  333. foreach ($schema['foreign keys'][$base_table]['columns'] AS $lcolum => $rcolum) {
  334. if (preg_match('/^subject_.*id/', $lcolum)) {
  335. $subject_id_key = $lcolum;
  336. }
  337. else if (preg_match('/^object_.*id/', $lcolum)) {
  338. $object_id_key = $lcolum;
  339. }
  340. }
  341. $voc_id = array_key_exists('vocabulary', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : '';
  342. $type_name = array_key_exists('type_name', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['type_name'] : '';
  343. $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] : '';
  344. $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]: '';
  345. $type_id = $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'];
  346. $subject_name = $form_state['values'][$field_name][$langcode][$delta]['subject_name'];
  347. $object_name = $form_state['values'][$field_name][$langcode][$delta]['object_name'];
  348. // If the row is empty then skip this one, there's nothing to validate.
  349. if (!($type_id or !$type_name) and !$subject_name and !$object_name) {
  350. return;
  351. }
  352. else if ($type_name && $voc_id) {
  353. $val = array(
  354. 'cv_id' => $voc_id,
  355. 'name' => $type_name
  356. );
  357. $cvterm = chado_generate_var('cvterm', $val);
  358. $type_id = $cvterm->cvterm_id;
  359. }
  360. // Do not proceed if subject ID or object ID does not exist
  361. if (!key_exists($subject_id_key, $fkeys[$base_table]['columns']) ||
  362. !key_exists($object_id_key, $fkeys[$base_table]['columns'])) {
  363. return;
  364. }
  365. // Get the subject ID.
  366. $subject_id = '';
  367. $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
  368. $matches = array();
  369. if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  370. $subject_id = $matches[1];
  371. }
  372. else {
  373. $values = array('uniquename' => $subject_name);
  374. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  375. if(count($subject) > 0) {
  376. $subject_id = $subject[0]->$fkey_rcolumn;
  377. }
  378. }
  379. // Get the object ID.
  380. $object_id = '';
  381. $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
  382. $matches = array();
  383. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  384. $object_id = $matches[1];
  385. }
  386. else {
  387. $values = array('uniquename' => $object_name);
  388. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  389. if (count($object) > 0) {
  390. $object_id = $object[0]->$fkey_rcolumn;
  391. }
  392. }
  393. if ($subject_id && $object_id && $type_id) {
  394. // Set the IDs according to the values that were determined above.
  395. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $subject_id;
  396. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
  397. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
  398. if (array_key_exists('rank', $schema['fields'])) {
  399. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
  400. }
  401. }
  402. else {
  403. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = '';
  404. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = '';
  405. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = '';
  406. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__value'] = '';
  407. if (array_key_exists('rank', $schema['fields'])) {
  408. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = '';
  409. }
  410. }
  411. }
  412. /**
  413. * Theme function for the sbo__relationship_widget.
  414. */
  415. public function theme($element) {
  416. $layout = "
  417. <div class=\"chado-linker--relationship-widget\">
  418. <div class=\"chado-linker--relationship-widget-item\">" .
  419. drupal_render($element['subject_name']) . "
  420. </div>
  421. <div class=\"chado-linker--relationship-widget-item\">" .
  422. drupal_render($element['vocabulary']) . "
  423. </div>
  424. <div class=\"chado-linker--relationship-widget-item\">" .
  425. drupal_render($element['type_name']) . "
  426. </div>
  427. <div class=\"chado-linker--relationship-widget-item\">" .
  428. drupal_render($element['type_id']) . "
  429. </div>
  430. <div>" .
  431. drupal_render($element['object_name']) . "
  432. </div>
  433. </div>
  434. ";
  435. return $layout;
  436. }
  437. }
  438. function theme_sbo__relationship_instance_settings ($variables) {
  439. $element = $variables['element'];
  440. $option1 = $element['option1'];
  441. $option1_vocabs = $element['option1_vocabs'];
  442. $option2 = $element['option2'];
  443. $option2_vocab = $element['option2_vocab'];
  444. $option2_parent = $element['option2_parent'];
  445. $option3 = $element['option3'];
  446. $rtype = $element['relationship_types'];
  447. $layout = drupal_render($option1);
  448. $layout .= drupal_render($option1_vocabs);
  449. $layout .=
  450. drupal_render($option2) .
  451. "<div class=\"chado-linker--relationship-instance-settings-option2\">" .
  452. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  453. drupal_render($option2_vocab) .
  454. "</div>" .
  455. "<div class=\"chado-linker--relationship-instance-settings-option2-item\">" .
  456. drupal_render($option2_parent) .
  457. "</div>" .
  458. "</div>";
  459. $layout .= drupal_render($option3);
  460. $layout .= drupal_render($rtype);
  461. return $layout;
  462. }
  463. /**
  464. * An Ajax callback for the relationshp widget.
  465. */
  466. function sbo__relationship_widget_form_ajax_callback(&$form, $form_state) {
  467. // Get the triggering element
  468. $form_element_name = $form_state['triggering_element']['#name'];
  469. preg_match('/(.+?)\[(.+?)\]\[(.+?)\]/', $form_element_name, $matches);
  470. $field = $matches[1];
  471. $lang = $matches[2];
  472. $delta = $matches[3];
  473. // Return the widget that triggered the AJAX call
  474. if (isset($form[$field][$lang][$delta])) {
  475. return $form[$field][$lang][$delta];
  476. }
  477. // Alternatively, return the default value widget for the widget setting form
  478. else {
  479. return $form['instance']['default_value_widget'][$field];
  480. }
  481. }
  482. /**
  483. * An Ajax callback for the relationshp instance setting form.
  484. */
  485. function sbo__relationship_instance_settings_form_ajax_callback(&$form, &$form_state) {
  486. $acpath = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'];
  487. $acpath .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'] . '/';
  488. $urlval = $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'];
  489. $urlval .= $form_state['values']['instance']['settings']['relationships']['option2_vocab'];
  490. // Reset value if a different vocabulary is selected
  491. $form['instance']['settings']['relationships']['option2_parent']['#value'] = NULL;
  492. $form_state['values']['instance']['settings']['relationships']['option2_parent'] = NULL;
  493. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_path'] = $acpath;
  494. $form['instance']['settings']['relationships']['option2_parent']['#autocomplete_input']['#url_value'] = $urlval;
  495. return $form['instance']['settings']['relationships']['option2_parent'];
  496. }