tode.module 33 KB

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