tode.module 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. <?php
  2. /**
  3. * developed by www.g-u-i.net
  4. * 2011
  5. * First time this field will be used, it will create a new term with the value inserted in the textfield,
  6. * after that, the term (tid) will always be the same, just update term name by updating the value of the field.
  7. * You can keep synchronized a fixe term with a node,
  8. * you can set an auto title from this field, The required,
  9. * number of value at 1 and term_node synch of global field are required
  10. *
  11. * <?php
  12. * // automatic node titles php snippet for translation
  13. * $items = field_get_items('node', $node, 'field_tode_program');
  14. * return t($items[0]['name']);
  15. * ?>
  16. *
  17. *
  18. */
  19. /**
  20. * Implements hook_views_api().
  21. */
  22. // TODO hook_views_api().
  23. // function tode_views_api() {
  24. // return array(
  25. // 'api' => 3,
  26. // 'path' => drupal_get_path('module', 'tode') . '/views',
  27. // );
  28. // }
  29. /**
  30. * Implementation of hook_theme().
  31. */
  32. function tode_theme() {
  33. return array(
  34. 'tode' => array(
  35. 'arguments' => array('element' => NULL),
  36. ),
  37. 'tode_node_formatter' => array(
  38. 'variables' => array('item' => NULL, 'viewmode' => 'full', 'nodes'=>NULL),
  39. ),
  40. );
  41. }
  42. /**
  43. * Implementation of hook_init().
  44. */
  45. function tode_init() {
  46. // File hooks and callbacks may be used by any module.
  47. drupal_add_css(drupal_get_path('module', 'tode') .'/tode.css');
  48. }
  49. /**
  50. * Implementation of hook_field_widget_info().
  51. */
  52. function tode_field_widget_info() {
  53. return array(
  54. 'tode' => array(
  55. 'label' => t('Tode (create then update one unique term)'),
  56. 'field types' => array('taxonomy_term_reference'),
  57. 'behaviors' => array(
  58. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  59. 'default value' => FIELD_BEHAVIOR_NONE,
  60. ),
  61. 'settings' => array(
  62. 'size' => 60,
  63. 'show_term_form'=> -1,
  64. 'choose_term_parent'=> -1,
  65. 'maxlength'=> 255,
  66. 'redirect_term_to_node' => -1,
  67. 'show_create_tode' => -1,
  68. ),
  69. ),
  70. );
  71. }
  72. /**
  73. * Implementation of hook_widget_settings
  74. */
  75. function tode_field_widget_settings_form($field, $instance){
  76. // dsm($instance, 'tode_field_widget_settings_form : $instance');
  77. // dsm($field, 'field');
  78. $widget = $instance['widget'];
  79. $settings = $widget['settings'];
  80. // '#description' => t('First time this field will be used, it will create a new term with the value inserted in the textfield, after that, the term (tid) will always be the same, just update term name by updating the value of the field. You can keep synchronized a fixe term with a node, you can set an auto title from this field, The required, number of value at 1 and term_node synch of global field are required')
  81. $form['maxlength'] = array(
  82. '#type' => 'textfield',
  83. '#title' => t('Maximum length of term'),
  84. '#default_value' => $settings['maxlength'],
  85. '#element_validate' => array('_tode_widget_settings_maxlength_validate'),
  86. '#required' => TRUE,
  87. '#description' => t('Defines how many characters can be typed into the text field. For values higher than 255, remember that one term name can not be longer than 255 (would be cutted).'),
  88. );
  89. // TODO complete the all flow of this
  90. $form['show_term_form'] = array(
  91. '#type' => 'checkbox',
  92. '#title' => t('Show taxonomy term edit form ?'),
  93. '#default_value' => $settings['show_term_form'],
  94. );
  95. $form['choose_term_parent'] = array(
  96. '#type' => 'checkbox',
  97. '#title' => t('Enable taxonomy term parent selection ?'),
  98. '#default_value' => $settings['choose_term_parent'],
  99. );
  100. $form['redirect_term_to_node'] = array(
  101. '#type' => 'checkbox',
  102. '#title' => t('Rewrite all term link to theire tode node ?'),
  103. '#default_value' => $settings['redirect_term_to_node'],
  104. );
  105. $form['redirect_node_to_term'] = array(
  106. '#type' => 'checkbox',
  107. '#title' => t('Rewrite all node link to theire term node ?'),
  108. '#default_value' => $settings['redirect_node_to_term'],
  109. );
  110. $form['show_create_tode'] = array(
  111. '#type' => 'checkbox',
  112. '#title' => t('Show create "tode" on term fields about this tode\'s vocabulary ?'),
  113. '#default_value' => $settings['show_create_tode'],
  114. );
  115. return $form;
  116. }
  117. function _tode_widget_settings_maxlength_validate($element, &$form_state) {
  118. // dsm('_tode_widget_settings_maxlength_validate');
  119. // dsm($element, '$element');
  120. // dsm($form_state, '$form_state');
  121. $widget = $form_state['values']['instance']['widget'];
  122. $value = $widget['settings']['maxlength'];
  123. if (!is_numeric($value) || intval($value) != $value || $value <= 0) {
  124. form_error($element, t('"Maximum length" must be a positive integer.'));
  125. }
  126. # TODO checke that node to term and term to node can't be chexked together
  127. }
  128. /**
  129. * Implementation of FAPI hook_elements().
  130. *
  131. * Any FAPI callbacks needed for individual widgets can be declared here,
  132. * and the element will be passed to those callbacks for processing.
  133. *
  134. * Drupal will automatically theme the element using a theme with
  135. * the same name as the hook_elements key.
  136. *
  137. * Autocomplete_path is not used by text_widget but other widgets can use it
  138. * (see nodereference and userreference).
  139. *
  140. * http://drupal.org/node/224333#hook_element_info
  141. *
  142. */
  143. #hook_element_info() WHY ??
  144. /*
  145. function tode_element_info(){
  146. return array(
  147. 'tode' => array(
  148. '#input' => TRUE,
  149. '#columns' => array('value'),
  150. '#delta' => 0,
  151. '#process' => array('tode_element_process'),
  152. ),
  153. );
  154. }
  155. */
  156. /**
  157. * Implementation of hook_field_widget_form().
  158. */
  159. function tode_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  160. // dsm('- - - - tode_field_widget_form');
  161. // dsm($form, '&$form');
  162. // dsm($form_state, '&$form_state');
  163. // dsm($items, 'items');
  164. // dsm($element, '$element');
  165. // dsm($field, '$field');
  166. // dsm($instance, '$instance');
  167. if(isset($form_state['node'])){
  168. $language = $form_state['node']->language;
  169. }else{
  170. $language = null;
  171. }
  172. // dsm($node, '$node');
  173. if(isset($items[$delta])){
  174. $term = taxonomy_term_load($items[$delta]['tid']);
  175. // $term = i18n_taxonomy_term_get_translation($term, $node->language); // marche pas avec localized term
  176. // dsm($term, '$term');
  177. $term_parents = taxonomy_get_parents($term->tid);
  178. // dsm($term_parents, '$term_parents');
  179. $term_parent = array_pop($term_parents);
  180. }
  181. # no need of $node->translation_source because with node translation (not entity fields translation) tid remains
  182. # just have to translate term name on submit
  183. // if( !isset($node->id) && isset($node->translation_source) ){
  184. // }
  185. $form['tode_tid'] = array('#type' => 'hidden', '#value' => isset($term) ? $term->tid : 0,'#delta' => $element['#delta'],);
  186. # parent
  187. if( $instance['widget']['settings']['choose_term_parent'] ){
  188. // add parent form autocomplete if activated
  189. $vocabularies = array();
  190. foreach ($field['settings']['allowed_values'] as $tree)
  191. if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary']))
  192. $vocabularies[$vocabulary->vid] = $vocabulary;
  193. $vocabulary = reset($vocabularies);
  194. // dsm($vocabulary, '$vocabulary');
  195. $form['tode_vid'] = array('#type' => 'hidden', '#value' => $vocabulary->vid,'#delta' => $element['#delta'],);
  196. $form['tode_parent_term'] = array(
  197. '#title' => 'Parent ' . $vocabulary->name,
  198. '#type' => 'textfield',
  199. '#default_value' => isset($term_parent) ? $term_parent->name : '',
  200. '#autocomplete_path' => 'taxonomy/autocomplete' . '/' . $field['field_name'],
  201. '#size' => 60,
  202. '#maxlength' => 1024,
  203. '#element_validate' => array('tode_parent_autocomplete_validate'),
  204. );
  205. }else{
  206. // if parent selection not enabled set the current parent as hidden input
  207. $form['tode_parent_term'] = array(
  208. '#type' => 'hidden',
  209. '#value' => isset($term_parent) ? $term_parent->name : '',
  210. '#delta' => $element['#delta'],
  211. );
  212. }
  213. // set element form item
  214. $element += array(
  215. '#type' => 'textfield',
  216. '#default_value' => isset($term) ? ( module_exists('i18n_taxonomy') ? i18n_taxonomy_term_name($term, $language) : $term->name ) : '',
  217. '#size' => $instance['widget']['settings']['size'],
  218. '#maxlength' => $instance['widget']['settings']['maxlength'],
  219. '#element_validate' => array('tode_widget_validate'),
  220. // '#element_submit' => array('tode_widget_submit'),
  221. );
  222. # add the term edit form
  223. if(isset($term))
  224. _tode_add_term_form($form, $term, $instance, $element['#delta']);
  225. // dsm($form, 'end tode_field_widget_form :: $form');
  226. return $element;
  227. }
  228. /**
  229. * _tode_add_term_form($term)
  230. *
  231. * show the complete taxonomy term form if feature is enabled
  232. *
  233. */
  234. function _tode_add_term_form(&$form, $term, $instance, $delta){
  235. // dsm('- - - - _tode_add_term_form');
  236. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  237. $term_form = _tode_term_form($term);
  238. // dsm($term_form, 'term_form');
  239. if($term_form){
  240. unset($term_form['actions']);
  241. unset($term_form['#action']);
  242. unset($term_form['#method']);
  243. #unset vid info 'cause is in conflict with the node's vid field
  244. unset($term_form['vid']);
  245. $term_form = _tode_clean_form($term_form);
  246. $prefix = 'tode_termform_'.$instance['field_name'];
  247. $term_form = _tode_prefix_form($term_form, $prefix.'_');
  248. $visible = $instance['widget']['settings']['show_term_form'];
  249. $form[$prefix] = array(
  250. '#type' => 'fieldset',
  251. '#title' => t('Term edit'),
  252. '#tree' => TRUE,
  253. '#collapsible' => TRUE,
  254. '#collapsed' => FALSE,
  255. '#weight'=>1,
  256. // '#group'=>'additional_settings',
  257. '#prefix' => $visible ? null : '<div style="display:none;">',
  258. '#suffix' => $visible ? null : '</div>',
  259. );
  260. $form[$prefix] += $term_form;
  261. // $form['tode_termform'] = array('#type' => 'hidden', '#value' => 'true','#delta' => $delta,);
  262. }
  263. }
  264. /**
  265. * Form element validate handler for tode parent term autocomplete element.
  266. */
  267. function tode_parent_autocomplete_validate($element, &$form_state) {
  268. // dsm('- - - - tode_autocomplete_validate');
  269. // dsm($element, '$element');
  270. // dsm($form_state, '$form_state');
  271. $value = array();
  272. if ($typed_term = $element['#value']) {
  273. // Translate term names into actual terms.
  274. // See if the term exists in the chosen vocabulary and return the tid;
  275. // otherwise, set error.
  276. if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => $form_state['values']['tode_vid']))) {
  277. $term = array_pop($possibilities);
  278. $value = (array)$term;
  279. }
  280. else {
  281. form_error($element, t('Parent term can only be an existing term.'));
  282. }
  283. }
  284. //
  285. // dsm('form_set_value');
  286. form_set_value($element, $value, $form_state);
  287. }
  288. /**
  289. * Validation function for the tode element
  290. *
  291. * parses input and sets the values as needed (tid) for storing the data
  292. */
  293. function tode_widget_validate($element, &$form_state){
  294. // dsm('- - - - tode_widget_validate');
  295. // dsm($form_state, 'form_state');
  296. // dsm($element, 'element');
  297. // $node = $form_state['node'];
  298. $value = array();
  299. if ($typed_term = $element['#value']) {
  300. $field = field_widget_field($element, $form_state);
  301. // dsm($field, 'field');
  302. $vocabularies = array();
  303. foreach ($field['settings']['allowed_values'] as $tree) {
  304. if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
  305. $vocabularies[$vocabulary->vid] = $vocabulary;
  306. }
  307. }
  308. // get the parent term tid or 0 (no parents)
  309. $parent_tid = isset($form_state['values']['tode_parent_term']['tid']) ? $form_state['values']['tode_parent_term']['tid'] : 0;
  310. // See if the term already exists and load the term
  311. // otherwise, create a new 'autocreate' term for insert.
  312. if($tid = $form_state['values']['tode_tid']){
  313. $term = taxonomy_term_load($tid);
  314. $term->name = $typed_term;
  315. $term->parent = $parent_tid;
  316. }else{
  317. $vocabulary = reset($vocabularies);
  318. $term = (object)array(
  319. 'tid' => 'autocreate', // autocreate not needed with direct taxonomy_term_save [EDIT] autocreate needed because of the hook_submit insted of hook_validate
  320. 'vid' => $vocabulary->vid,
  321. 'name' => $typed_term,
  322. 'vocabulary_machine_name' => $vocabulary->machine_name,
  323. 'parent' => $parent_tid,
  324. );
  325. // taxonomy_term_save($term); // save here the new term to directly get the tid on hook_node_validate
  326. // [EDIT] do not save the term here because it will be save even if node form is not validate
  327. // results term without node or mutiple orfan term
  328. // moving all process on node submit
  329. }
  330. // dsm($term, '$term');
  331. $value = (array)$term;
  332. }
  333. // dsm($value, '$value');
  334. form_set_value($element, $value, $form_state);
  335. }
  336. /**
  337. * Implements hook_field_widget_error().
  338. */
  339. function tode_field_widget_error($element, $error, $form, &$form_state) {
  340. # use this to set errors
  341. form_error($element['value'], $error['message']);
  342. }
  343. /**
  344. * Implements hook_node_submit().
  345. */
  346. function tode_node_submit($node, $form, &$form_state) {
  347. // dsm('- - - tode_node_submit');
  348. // dsm($node, '$node');
  349. // dsm($form, '$form');
  350. // dsm($form_state, '$form_state');
  351. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  352. // module_load_include('inc', 'i18n_string', 'i18n_string.pages');
  353. //
  354. $tode_fields = _tode_get_node_tode_fields_def($node);
  355. // dsm($tode_fields, '$tode_fields');
  356. if(!count($tode_fields))
  357. return;
  358. $language = $node->language;
  359. $default_language = language_default('language');
  360. // dsm($default_language, '$default_language');
  361. foreach ($tode_fields as $field_name => $field) {
  362. // retreive the prefixed termfom values (hidden or visible)
  363. $prefix = 'tode_termform_'.$field_name;
  364. // if term_form is not available
  365. // it meens that we are on the creation of a node
  366. // hook_node_presave will handle this case
  367. if( !isset($form_state['values'][$prefix]) )
  368. continue;
  369. // else
  370. // we are on the update or on the translation (from initial node)
  371. // so we can go forward
  372. // retreive the initial tode_field language
  373. $init_language = $form[$field_name]['#language'];
  374. // retreive the value of term field, to get the typed term name
  375. if(isset($form_state['values'][$field_name][$init_language][0])){
  376. $tode_field_term_value = $form_state['values'][$field_name][$init_language][0];
  377. }else if(isset($form_state['values'][$field_name][$language][0])){
  378. $tode_field_term_value = $form_state['values'][$field_name][$language][0];
  379. }else if(isset($form_state['values'][$field_name]['und'][0])){
  380. $tode_field_term_value = $form_state['values'][$field_name]['und'][0];
  381. }else{
  382. drupal_set_message('$form_state[values][$field_name][$language][0] is undefined', 'warning');
  383. $tode_field_term_value = false;
  384. }
  385. // dsm($tode_field_term_value, '$tode_field_term_value');
  386. if($tode_field_term_value){
  387. // dsm($form_state['values'][$prefix], '$form_state["values"][$prefix]');
  388. $values = _tode_prefix_form($form_state['values'][$prefix], $prefix.'_', FALSE);
  389. // dsm($values, 'values');
  390. if($field['field_info_field']['translatable'] && module_exists('i18n_taxonomy') && $language != 'und' && $language != $default_language){
  391. // $field['field_info_field']['translatable'] is about entity translation module
  392. $context= array('term',$values['tid'],'name');
  393. i18n_string_textgroup('taxonomy')->update_translation($context, $language, $tode_field_term_value['name']);
  394. $tode_field_term_value['name'] = $values['name'];
  395. }
  396. $parent_tid = $tode_field_term_value['parent'];
  397. $values['parent'] = array($parent_tid => $parent_tid);
  398. // define the form_state for term_form submit
  399. $new_term_form_state = array(
  400. 'build_info'=>array(
  401. 'args'=>array(0=>(object)$tode_field_term_value),
  402. ),
  403. "values"=>array(
  404. 'name'=> $tode_field_term_value['name'], // replace the original (hidden) term name value by the typed in the the entity field
  405. 'op'=> t('Save'),
  406. )
  407. );
  408. // add new values to form_state
  409. $new_term_form_state['values'] += $values;
  410. // dsm($new_term_form_state, 'form_state');
  411. drupal_form_submit('taxonomy_form_term', $new_term_form_state);
  412. }
  413. }
  414. }
  415. /**
  416. * Implements hook_node_presave().
  417. */
  418. function tode_node_presave($node) {
  419. // dsm('- - - tode_node_presave');
  420. // dsm($node, 'node');
  421. if(isset($node->nid))
  422. return;
  423. $tode_fields = _tode_get_node_tode_fields_def($node);
  424. if(!count($tode_fields))
  425. return;
  426. $default_language = language_default('language');
  427. // purpose of that is to directly atribute the right language to the term
  428. foreach ($tode_fields as $field_name => $field){
  429. $tode_field = $node->$field_name;
  430. if(!isset($tode_field[$node->language][0]))
  431. continue;
  432. $tode_field_term = $tode_field[$node->language][0]; // on node creation field language is always to und (sure ?)
  433. // // test the language, if not default language create the term name translation
  434. if( module_exists('i18n_taxonomy') && ( $node->language != 'und' || $node->language != $default_language ) ){
  435. $context= array('term',$tode_field_term['tid'],'name');
  436. i18n_string_textgroup('taxonomy')->update_translation($context, $node->language, $tode_field_term['name']);
  437. }
  438. }
  439. }
  440. // TODO merge two terms when translation is made by selecting a node whiche already exists
  441. // TODO create a node when a term is created else where than the node which got the tode
  442. /**
  443. * Implementation of hook_form_alter().
  444. *
  445. * node deletion to delete also the tode term
  446. *
  447. */
  448. function tode_form_alter(&$form, $form_state, $form_id){
  449. // dsm($form_id, 'tode form_alter form_id');
  450. // TODO block the deletion if tode term has children !! because this will delete them to …
  451. if($form_id == 'test_node_form'){
  452. // dsm($form_state);
  453. }
  454. if (stripos($form_id, 'node_delete_confirm') !== false){
  455. // dsm($form_id, 'tode form_alter form_id');
  456. _tode_node_delete_form_alter($form, $form_state);
  457. // dsm($form);
  458. }else if(stripos($form_id, 'node_admin_content') !== false){
  459. if(isset($form['operation']) && $form['operation']['#value'] == 'delete'){
  460. // dsm($form, 'node_admin_content form');
  461. // dsm($form_state, 'form_state');
  462. _tode_nodes_delete_form_alter($form, $form_state);
  463. }
  464. }
  465. }
  466. function _tode_node_delete_form_alter(&$form, $form_state){
  467. // dsm($form, '_tode_node_delete_form_alter : form');
  468. // get the node
  469. $node = $form['#node'];
  470. // dsm($node, '$node');
  471. #get the fields defenition of node type
  472. $tode_fields = _tode_get_node_tode_fields_def($node);
  473. // dsm($tode_fields);
  474. if(count($tode_fields) == 0)
  475. return;
  476. #get the terms value
  477. $terms = array('names'=>array(), 'tids'=>array());
  478. foreach ($tode_fields as $field_name => $field)
  479. _tode_populate_terms_node_delete($terms, $node->$field_name);
  480. _tode_node_delete_prepare_form($form, $terms);
  481. }
  482. function _tode_nodes_delete_form_alter(&$form, $form_state){
  483. // dsm($form, '_tode_nodes_delete_form_alter : form');
  484. $nodes = array();
  485. foreach ($form_state['values']['nodes'] as $nid => $actif)
  486. if($actif)
  487. $nodes[] = node_load($nid);
  488. #get the terms value
  489. $terms = array('names'=>array(), 'tids'=>array());
  490. foreach ($nodes as $node) {
  491. #get the fields definition of node type
  492. $tode_fields = _tode_get_node_tode_fields_def($node);
  493. //dsm($tode_fields);
  494. if(count($tode_fields) == 0)
  495. continue;
  496. foreach ($tode_fields as $field_name => $field)
  497. _tode_populate_terms_node_delete($terms, $node->$field_name);
  498. }
  499. _tode_node_delete_prepare_form($form, $terms);
  500. }
  501. function _tode_populate_terms_node_delete(&$terms, $tode_field){
  502. foreach ($tode_field as $language) {
  503. foreach ($language as $term) {
  504. $term = taxonomy_term_load($term['tid']);
  505. if(isset($term->tid) && !in_array($term->tid, $terms['tids'])){
  506. $terms['names'][] = $term->name;
  507. $terms['tids'][] = $term->tid;
  508. }
  509. }
  510. }
  511. }
  512. function _tode_node_delete_prepare_form(&$form, $terms){
  513. if(count($terms)){
  514. // TODO add here a checkbox to select terms to delete
  515. #add some warning in form description
  516. $form['description']['#markup'] .= '<br />'.t('this will also delete taxonomy terms : %terms', array('%terms'=>implode(', ', $terms['names'])));
  517. $form['tode_delete'] = array( '#type' => 'hidden', '#value' => serialize($terms['tids']),);
  518. $form['tode_terms'] = array('#type' => 'hidden', '#value' => serialize($terms['names']),);
  519. $form['#submit'][] = 'tode_delete_submit';
  520. }
  521. }
  522. function tode_delete_submit($form, &$form_state){
  523. $tids = unserialize($form['tode_delete']['#value']);
  524. foreach ($tids as $tid)
  525. taxonomy_term_delete($tid);
  526. $terms = unserialize($form['tode_terms']['#value']);
  527. drupal_set_message(t('Following Taxonomy terms have been deleted : %terms', array('%terms' => implode(', ', $terms) )), 'status');
  528. }
  529. /**
  530. * Implements hook_url_outbound_alter().
  531. */
  532. function tode_url_outbound_alter(&$path, &$options, $original_path) {
  533. // dsm('tode_url_outbound_alter');
  534. # terms url
  535. $term = false;
  536. $node = false;
  537. // if(isset($options['entity_type']) && $options['entity_type'] == 'taxonomy_term'){
  538. // // dsm('- - - - tode_url_outbound_alter');
  539. // // dsm($path, '$path');
  540. // // dsm($options, '$options');
  541. // // dsm($original_path, '$original_path');
  542. //
  543. // $term = $options['entity'];
  544. //
  545. // }else{
  546. // $args = explode('/', $original_path);
  547. // // dsm($args, 'args');
  548. // if($args[0] == 'taxonomy' && $args[1] == 'term' && is_numeric($args[2]) ){
  549. // $term = taxonomy_term_load($args[2]);
  550. // }
  551. //
  552. // }
  553. if (preg_match('/^node\/([0-9]*)$/', $path, $matches)) {
  554. $node = node_load($matches[1]);
  555. }
  556. # WARNING works only because nodeandtermrelink_url_outbound_alter() is trigered before this (KADIST)
  557. if (preg_match('/^taxonomy\/term\/([0-9]*)$/', $path, $matches)) {
  558. $term = taxonomy_term_load($matches[1]);
  559. }
  560. if($node){
  561. // dsm($node, 'node');
  562. if($fields = _tode_get_node_tode_fields_def($node)){
  563. foreach ($fields as $field_name => $field) {
  564. if (isset($field['widget']['settings']['redirect_node_to_term']) && $field['widget']['settings']['redirect_node_to_term']) {
  565. dsm($field, '$field');
  566. $items = field_get_items('node', $node, $field_name);
  567. dsm($items, 'items');
  568. $new_path = 'taxonomy/term/'.$items[0]['tid'];
  569. if( $new_alias = drupal_get_path_alias($new_path) ){//, $options['language']->language
  570. $path = $new_alias;
  571. $original_path = $new_path;
  572. $options['alias'] = TRUE;
  573. }else{
  574. $path = $new_path;
  575. }
  576. break;
  577. }
  578. }
  579. }
  580. }
  581. if($term){
  582. // dsm($term, '$term : '.$term->name);
  583. if($field = _tode_get_voc_tode_field_def($term->vocabulary_machine_name)){
  584. // dsm($field, '$field');
  585. // $field_settings = field_info_instance_settings($field->type);//field_info_widget_settings($field->field_name); //
  586. // dsm($field_settings, '$field_settings');
  587. # TODO add if widget setting; how to get the settings
  588. if (isset($field['widget']['settings']['redirect_term_to_node']) && $field['widget']['settings']['redirect_term_to_node']) {
  589. // dsm($field, '$field');
  590. // dsm($options, '$options');
  591. $sr = $field['field_info_field']['storage']['details']['sql']['FIELD_LOAD_CURRENT'];
  592. foreach ($sr as $table => $column) {
  593. $query = db_select($table, 'ft');
  594. $query->join('node', 'n', 'ft.entity_id = n.nid AND n.language = :language OR n.language = :language', array(':language' => $options['language']->language, ':language' => 'und'));
  595. $query
  596. ->fields('ft')
  597. ->condition('ft.'.$column['tid'], $term->tid);
  598. $result = $query->execute();
  599. break;
  600. }
  601. foreach ($result as $node) {
  602. // dsm($node, '$node');
  603. $new_path = 'node/'.$node->entity_id;
  604. if( $new_alias = drupal_get_path_alias($new_path, $options['language']->language) ){
  605. $path = $new_alias;
  606. $original_path = $new_path;
  607. $options['alias'] = TRUE;
  608. }else{
  609. $path = $new_path;
  610. }
  611. break;
  612. }
  613. }
  614. }
  615. }
  616. }
  617. /**
  618. * Implements hook_field_formatter_info().
  619. */
  620. function tode_field_formatter_info() {
  621. return array(
  622. 'tode' => array(
  623. 'label' => t('tode (node of the term)'),
  624. 'field types' => array('taxonomy_term_reference'),
  625. 'settings'=>array('viewmode' => "full"),
  626. ),
  627. );
  628. }
  629. /**
  630. * Implements hook_field_formatter_settings_form().
  631. */
  632. function tode_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  633. $element = array();
  634. $display = $instance['display'][$view_mode];
  635. $settings = $display['settings'];
  636. $formatter = $display['type'];
  637. if($formatter == 'tode'){
  638. if($todefield = _tode_get_voc_tode_field_def($field['settings']['allowed_values'][0]['vocabulary'])){
  639. $entity_infos = entity_get_info();
  640. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  641. $viewmode_options[$viewmode] = $value['label'];
  642. }
  643. $element['viewmode'] = array(
  644. '#type' => 'select',
  645. '#title' => t('View mode'),
  646. '#default_value' => $settings['viewmode'],
  647. '#description' => t('select the view mode for the node'),
  648. '#options' => $viewmode_options,
  649. );
  650. }else{
  651. // TODO: tell that vocabulary has any tode field
  652. }
  653. }
  654. return $element;
  655. }
  656. /**
  657. * Implements hook_field_formatter_settings_summary().
  658. */
  659. function tode_field_formatter_settings_summary($field, $instance, $view_mode) {
  660. $display = $instance['display'][$view_mode];
  661. if($display['type'] == 'tode'){
  662. $settings = $display['settings'];
  663. $summary = t('viewmode : %vm', array('%vm'=> isset($settings['viewmode']) ? $settings['viewmode'] : 'full') );
  664. return $summary;
  665. }
  666. }
  667. /**
  668. * Implements hook_field_formatter_view().
  669. */
  670. function tode_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  671. $element = array();
  672. $settings = $display['settings'];
  673. switch ($display['type']) {
  674. case 'tode':
  675. foreach ($items as $delta => $item) {
  676. $term = taxonomy_term_load($item['tid']);
  677. $entitys = tode_get_nids_from_term($term);
  678. $nodes = array();
  679. if(isset($entitys['node'])){
  680. foreach ($entitys['node'] as $nid => $n) {
  681. $node = node_load($nid);
  682. $nodes[$nid] = $node;
  683. }
  684. $element[$delta] = array(
  685. '#theme' => 'tode_node_formatter',
  686. '#item' => $item,
  687. '#viewmode' => $settings['viewmode'],
  688. '#nodes'=> $nodes,
  689. );
  690. }
  691. }
  692. break;
  693. }
  694. return $element;
  695. }
  696. function theme_tode_node_formatter($vars){
  697. // dsm($vars, 'theme_tode_node_formatter | vars');
  698. // $entity = 'node';
  699. // $nodes = $vars['nodes'];
  700. // $vm = $vars['viewmode'];
  701. // $entity_view = entity_view($entity, $nodes, $vm);
  702. $entity_view = entity_view('node', $vars['nodes'], $vars['viewmode']);
  703. print render($entity_view);
  704. }
  705. /**
  706. * HELPERS
  707. */
  708. function _tode_clean_form($form, $level = 0){
  709. foreach ($form as $key => $value) {
  710. if(strpos($key,'#') !== false || $key == 'form_build_id' || $key == 'form_id' || $key == 'form_token'){
  711. if( $level == 0 || $key == "#element_validate")
  712. unset($form[$key]);
  713. }elseif(is_array($value)){
  714. $form[$key] = _tode_clean_form($value, $level+1);
  715. }
  716. }
  717. return $form;
  718. }
  719. function _tode_prefix_form($form, $prefix = '', $add = TRUE){
  720. foreach ($form as $key => $value) {
  721. if(strpos($key,'#') === false){
  722. if((isset($value['#type']) && $value['#type'] == 'fieldset') || (!$add && is_array($value)))
  723. $value = _tode_prefix_form($value, $prefix, $add);
  724. if($add){
  725. $form[$prefix.$key] = $value;
  726. unset($form[$key]);
  727. }elseif(strpos($key, $prefix) !== false ){
  728. $form[str_replace($prefix, '', $key)] = $value;
  729. unset($form[$key]);
  730. }
  731. }
  732. }
  733. return $form;
  734. }
  735. /**
  736. * _tode_term_form($tid)
  737. *
  738. */
  739. function _tode_term_form($term){
  740. // dsm('_tode_term_form');
  741. if ($term) {
  742. $form_state = array(
  743. 'build_info'=>array(
  744. 'args'=>array(0=>$term)
  745. ),
  746. 'method'=>'post',
  747. );
  748. // function taxonomy_form_term($form, &$form_state, $edit = array(), $vocabulary = NULL) {
  749. $term_form = drupal_retrieve_form('taxonomy_form_term', $form_state);
  750. drupal_prepare_form('taxonomy_form_term', $term_form, $form_state);
  751. return $term_form;
  752. }else{
  753. return false;
  754. }
  755. }
  756. /**
  757. * _tode_trim_options(&$form, $element)
  758. *
  759. */
  760. function _tode_trim_options(&$form, $item){
  761. foreach ($form[$item] as $field_name => $field) {
  762. if(((is_array($field)) && $field['#type'] == 'select') && $field['#multiple']){
  763. $options = $field['#options'];
  764. for ($i=0; $i < count($options); $i++) {
  765. if(!isset($options[$i]->option))
  766. continue;
  767. $op = array();
  768. foreach ($options[$i]->option as $key => $value)
  769. $op[$key] = strlen($value) > 25 ? substr_replace ($value, ' [...] ', 15, -10) : $value; // affiche 'abc...xyz'
  770. $options[$i]->option = $op;
  771. }
  772. $field['#options'] = $options;
  773. $form[$item][$field_name] = $field;
  774. }
  775. }
  776. }
  777. /**
  778. * _tode_get_node_tode_fields_def
  779. */
  780. function _tode_get_node_tode_fields_def($node){
  781. // dsm($node, '_tode_get_fields_def');
  782. #get the fields defenition of node type
  783. $type_fields = field_info_instances('node');
  784. // dsm($type_fields, 'type_fields definition');
  785. #get the tode node fields
  786. $tode_fields = array();
  787. foreach ($type_fields[$node->type] as $field_name => $field) {
  788. if($field['widget']['type'] == 'tode'){
  789. $fieldinfos = field_info_field($field_name);
  790. $field['field_info_field'] = $fieldinfos;
  791. $tode_fields[$field_name] = $field;
  792. }
  793. }
  794. return $tode_fields;
  795. }
  796. function _tode_get_voc_tode_field_def($voc_name){
  797. $type_fields = field_info_instances('node');
  798. // dsm($type_fields, '$type_fields');
  799. foreach ($type_fields as $nodetype => $fields)
  800. foreach ($fields as $field_name => $field)
  801. if($field['widget']['type'] == 'tode'){
  802. // dsm($field, '$field');
  803. $fieldinfos = field_info_field($field_name);
  804. // dsm($fieldinfos, '$fieldinfos');
  805. $field['field_info_field'] = $fieldinfos;
  806. if($voc_name == $fieldinfos['settings']['allowed_values'][0]['vocabulary'])
  807. return $field;
  808. }
  809. return false;
  810. }
  811. function tode_get_nids_from_term($term, $language = false){
  812. if(isset($term->vocabulary_machine_name) && $todefield = _tode_get_voc_tode_field_def($term->vocabulary_machine_name)){
  813. // $fieldinfos = field_info_field($todefield->field_name);
  814. $query = new EntityFieldQuery();
  815. $query->entityCondition('entity_type', 'node')
  816. ->fieldCondition($todefield['field_name'], 'tid', $term->tid);
  817. if($language)
  818. $query->propertyCondition('language', array('und', $language), 'IN');
  819. $result = $query->execute();
  820. if(count($result))
  821. return $result;
  822. }
  823. return false;
  824. }