sbo__relationship_widget.inc 23 KB

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