tode.module 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. print render(entity_view('node', $vars['nodes'], $vars['viewmode']));
  699. }
  700. /**
  701. * HELPERS
  702. */
  703. function _tode_clean_form($form, $level = 0){
  704. foreach ($form as $key => $value) {
  705. if(strpos($key,'#') !== false || $key == 'form_build_id' || $key == 'form_id' || $key == 'form_token'){
  706. if( $level == 0 || $key == "#element_validate")
  707. unset($form[$key]);
  708. }elseif(is_array($value)){
  709. $form[$key] = _tode_clean_form($value, $level+1);
  710. }
  711. }
  712. return $form;
  713. }
  714. function _tode_prefix_form($form, $prefix = '', $add = TRUE){
  715. foreach ($form as $key => $value) {
  716. if(strpos($key,'#') === false){
  717. if((isset($value['#type']) && $value['#type'] == 'fieldset') || (!$add && is_array($value)))
  718. $value = _tode_prefix_form($value, $prefix, $add);
  719. if($add){
  720. $form[$prefix.$key] = $value;
  721. unset($form[$key]);
  722. }elseif(strpos($key, $prefix) !== false ){
  723. $form[str_replace($prefix, '', $key)] = $value;
  724. unset($form[$key]);
  725. }
  726. }
  727. }
  728. return $form;
  729. }
  730. /**
  731. * _tode_term_form($tid)
  732. *
  733. */
  734. function _tode_term_form($term){
  735. // dsm('_tode_term_form');
  736. if ($term) {
  737. $form_state = array(
  738. 'build_info'=>array(
  739. 'args'=>array(0=>$term)
  740. ),
  741. 'method'=>'post',
  742. );
  743. // function taxonomy_form_term($form, &$form_state, $edit = array(), $vocabulary = NULL) {
  744. $term_form = drupal_retrieve_form('taxonomy_form_term', $form_state);
  745. drupal_prepare_form('taxonomy_form_term', $term_form, $form_state);
  746. return $term_form;
  747. }else{
  748. return false;
  749. }
  750. }
  751. /**
  752. * _tode_trim_options(&$form, $element)
  753. *
  754. */
  755. function _tode_trim_options(&$form, $item){
  756. foreach ($form[$item] as $field_name => $field) {
  757. if(((is_array($field)) && $field['#type'] == 'select') && $field['#multiple']){
  758. $options = $field['#options'];
  759. for ($i=0; $i < count($options); $i++) {
  760. if(!isset($options[$i]->option))
  761. continue;
  762. $op = array();
  763. foreach ($options[$i]->option as $key => $value)
  764. $op[$key] = strlen($value) > 25 ? substr_replace ($value, ' [...] ', 15, -10) : $value; // affiche 'abc...xyz'
  765. $options[$i]->option = $op;
  766. }
  767. $field['#options'] = $options;
  768. $form[$item][$field_name] = $field;
  769. }
  770. }
  771. }
  772. /**
  773. * _tode_get_node_tode_fields_def
  774. */
  775. function _tode_get_node_tode_fields_def($node){
  776. // dsm($node, '_tode_get_fields_def');
  777. #get the fields defenition of node type
  778. $type_fields = field_info_instances('node');
  779. // dsm($type_fields, 'type_fields definition');
  780. #get the tode node fields
  781. $tode_fields = array();
  782. foreach ($type_fields[$node->type] as $field_name => $field) {
  783. if($field['widget']['type'] == 'tode'){
  784. $fieldinfos = field_info_field($field_name);
  785. $field['field_info_field'] = $fieldinfos;
  786. $tode_fields[$field_name] = $field;
  787. }
  788. }
  789. return $tode_fields;
  790. }
  791. function _tode_get_voc_tode_field_def($voc_name){
  792. $type_fields = field_info_instances('node');
  793. // dsm($type_fields, '$type_fields');
  794. foreach ($type_fields as $nodetype => $fields)
  795. foreach ($fields as $field_name => $field)
  796. if($field['widget']['type'] == 'tode'){
  797. // dsm($field, '$field');
  798. $fieldinfos = field_info_field($field_name);
  799. // dsm($fieldinfos, '$fieldinfos');
  800. $field['field_info_field'] = $fieldinfos;
  801. if($voc_name == $fieldinfos['settings']['allowed_values'][0]['vocabulary'])
  802. return $field;
  803. }
  804. return false;
  805. }
  806. function tode_get_nids_from_term($term, $language = false){
  807. if(isset($term->vocabulary_machine_name) && $todefield = _tode_get_voc_tode_field_def($term->vocabulary_machine_name)){
  808. // $fieldinfos = field_info_field($todefield->field_name);
  809. $query = new EntityFieldQuery();
  810. $query->entityCondition('entity_type', 'node')
  811. ->fieldCondition($todefield['field_name'], 'tid', $term->tid);
  812. if($language)
  813. $query->propertyCondition('language', array('und', $language), 'IN');
  814. $result = $query->execute();
  815. if(count($result))
  816. return $result;
  817. }
  818. return false;
  819. }