sbo__relationship_widget.inc 23 KB

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