sbo__relationship_widget.inc 23 KB

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