Compare commits

..
6 Commits
Author SHA1 Message Date
bachy 4a958da4df parent bug on node update fixed
parent selection used to works only on node creation, now works on node creation and update

add automatic node titles snippet fro term translation

Signed-off-by: bachy <git@g-u-i.net>
2012-04-16 23:07:27 +02:00
bachy 9d97a0b7b7 tode_url_outbound_alter improvement (path_alias)
Signed-off-by: bachy <git@g-u-i.net>
2012-03-18 14:38:35 +01:00
bachy 20ad82694b modale Tode creation
Signed-off-by: bachy <git@g-u-i.net>
2012-03-13 17:24:52 +01:00
bachy 9d10e145b4 micro debug
Signed-off-by: bachy <git@g-u-i.net>
2012-02-10 19:53:00 +01:00
bachy 6ab6fc916b views integration start, not sure to keep that
Signed-off-by: bachy <git@g-u-i.net>
2012-02-07 19:46:12 +01:00
bachy fbaa166db8 bug correction
Signed-off-by: bachy <git@g-u-i.net>
2012-02-04 12:29:28 +01:00
4 changed files with 495 additions and 46 deletions
+11 -1
View File
@@ -1,3 +1,13 @@
.ctu-term-edit-form-box{border: 1px solid #ddd; padding:0.5em;}
#node-form fieldset.tcu-advanced-options,
#node-form fieldset.tcu-term-fields{width:45%; display:inline-block; vertical-align: top;}
#node-form fieldset.tcu-term-fields{width:45%; display:inline-block; vertical-align: top;}
.tode-add-modal-wrapper{
}
.tode-add-modal-wrapper .form-item{
display:inline-block;
}
.tode-add-modal-wrapper .tode-add-modal-links{
display:inline-block;
}
+451 -45
View File
@@ -8,30 +8,31 @@
* 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
*
* <?php
* // automatic node titles php snippet for translation
* $items = field_get_items('node', $node, 'field_tode_program');
* return t($items[0]['name']);
* ?>
*
*
*/
/**
* Implementation of hook_menu().
* Implements hook_views_api().
*/
/*
function tode_menu() {
$items = array();
// $items['node/%node/tode/%term'] = array(
// 'title' => 'Taxonomy Term',
// 'type' => MENU_LOCAL_TASK,
// 'access callback' => 'tode_node_tab_access',
// 'access arguments' => array(2),
// 'page callback' => 'tode_tab_page',
// 'page arguments' => array(2),
// 'file' => 'tode.pages.inc',
// 'weight' => 2,
// );
return $items;
}
TODO hook_views_api().
*/
// function tode_views_api() {
// return array(
// 'api' => 3,
// 'path' => drupal_get_path('module', 'tode') . '/views',
// );
// }
/**
* Implementation of hook_theme().
@@ -112,8 +113,19 @@ function tode_field_widget_settings_form($field, $instance){
'#default_value' => $settings['choose_term_parent'],
);
$form['redirect_term_to_node'] = array(
'#type' => 'checkbox',
'#title' => t('Rewrite all term link to theire tode node ?'),
'#default_value' => $settings['redirect_term_to_node'],
);
$form['show_create_tode'] = array(
'#type' => 'checkbox',
'#title' => t('Show create "tode" on term fields about this tode\'s vocabulary ?'),
'#default_value' => $settings['show_create_tode'],
);
return $form;
}
function _tode_widget_settings_maxlength_validate($element, &$form_state) {
@@ -169,7 +181,11 @@ function tode_field_widget_form(&$form, &$form_state, $field, $instance, $langco
// dsm($instance, '$instance');
$node = $form_state['node'];
if(isset($form_state['node'])){
$language = $form_state['node']->language;
}else{
$language = null;
}
// dsm($node, '$node');
if(isset($items[$delta])){
@@ -223,7 +239,7 @@ function tode_field_widget_form(&$form, &$form_state, $field, $instance, $langco
// set element form item
$element += array(
'#type' => 'textfield',
'#default_value' => isset($term) ? ( if(module_exists('i18n_taxonomy')) ? i18n_taxonomy_term_name($term, $node->language) : $term->name ) : '',
'#default_value' => isset($term) ? ( module_exists('i18n_taxonomy') ? i18n_taxonomy_term_name($term, $language) : $term->name ) : '',
'#size' => $instance['widget']['settings']['size'],
'#maxlength' => $instance['widget']['settings']['maxlength'],
'#element_validate' => array('tode_widget_validate'),
@@ -328,7 +344,7 @@ function tode_widget_validate($element, &$form_state){
// dsm($form_state, 'form_state');
// dsm($element, 'element');
$node = $form_state['node'];
// $node = $form_state['node'];
$value = array();
@@ -401,7 +417,7 @@ function tode_node_submit($node, $form, &$form_state) {
module_load_include('inc', 'taxonomy', 'taxonomy.admin');
// module_load_include('inc', 'i18n_string', 'i18n_string.pages');
//
$tode_fields = _tode_get_tode_fields_def($node);
$tode_fields = _tode_get_node_tode_fields_def($node);
// dsm($tode_fields, '$tode_fields');
@@ -432,14 +448,39 @@ function tode_node_submit($node, $form, &$form_state) {
$init_language = $form[$field_name]['#language'];
// retreive the value of term field, to get the typed term name
$tode_field_term_value = $form_state['values'][$field_name][$init_language][0];
if(isset($form_state['values'][$field_name][$init_language][0])){
$tode_field_term_value = $form_state['values'][$field_name][$init_language][0];
}else if(isset($form_state['values'][$field_name][$language][0])){
$tode_field_term_value = $form_state['values'][$field_name][$language][0];
}else if(isset($form_state['values'][$field_name]['und'][0])){
$tode_field_term_value = $form_state['values'][$field_name]['und'][0];
}else{
drupal_set_message('$form_state[values][$field_name][$language][0] is undefined', 'warning');
$tode_field_term_value = false;
}
// dsm($tode_field_term_value, '$tode_field_term_value');
$values = _tode_prefix_form($form_state['values'][$prefix], $prefix.'_', FALSE);
// dsm($values, 'values');
if(!module_exists('i18n_taxonomy') || $language == 'und' || $language == $default_language){
if($tode_field_term_value){
// dsm($form_state['values'][$prefix], '$form_state["values"][$prefix]');
$values = _tode_prefix_form($form_state['values'][$prefix], $prefix.'_', FALSE);
// dsm($values, 'values');
if(module_exists('i18n_taxonomy') && $language != 'und' && $language != $default_language){
$context= array('term',$values['tid'],'name');
i18n_string_textgroup('taxonomy')->update_translation($context, $language, $tode_field_term_value['name']);
$tode_field_term_value['name'] = $values['name'];
}
$parent_tid = $tode_field_term_value['parent'];
$values['parent'] = array($parent_tid => $parent_tid);
// if(!module_exists('i18n_taxonomy') || $language == 'und' || $language == $default_language){
// define the form_state for term_form submit
$new_term_form_state = array(
'build_info'=>array(
@@ -452,14 +493,12 @@ function tode_node_submit($node, $form, &$form_state) {
);
// add new values to form_state
$new_term_form_state['values'] += $values;
// dsm($new_term_form_state, 'form_state');
drupal_form_submit('taxonomy_form_term', $new_term_form_state);
}else{
$context= array('term',$values['tid'],'name');
i18n_string_textgroup('taxonomy')->update_translation($context, $language, $tode_field_term_value['name']);
// }else{
}
}
@@ -478,7 +517,7 @@ function tode_node_presave($node) {
if($node->nid)
return;
$tode_fields = _tode_get_tode_fields_def($node);
$tode_fields = _tode_get_node_tode_fields_def($node);
if(!count($tode_fields))
return;
@@ -501,10 +540,311 @@ function tode_node_presave($node) {
/*
TODO Sélectionner une traduction pour test tode fr
merge two terms when translation is made by selecting a node whiche already exists
TODO merge two terms when translation is made by selecting a node whiche already exists
*/
/*
TODO create a node when a term is created else where than the node which got the tode
*/
/**
* Implements hook_menu().
*/
function tode_menu() {
// dsm('tode_menu');
$items = array();
$items['tode/%ctools_js/add'] = array(
'title' => 'Tode modal add entity',
'page callback' => 'tode_entity_add',
'page arguments' => array(1,3,4,5),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function tode_entity_add($js = FALSE, $bundle, $entity, $title = 'Title'){
// Fall back if $js is not set.
if (!$js) {
return drupal_get_form('tode_entity_add_form', $bundle, $entity);
}
ctools_include('modal');
ctools_include('ajax');
$form_state = array(
'title' => t('Create '.$entity),
'ajax' => TRUE,
'build_info' => array('args' => array('0' => $bundle, '1' => $entity, '2' => $title)),
);
$output = ctools_modal_form_wrapper('tode_entity_add_form', $form_state);
if (!empty($form_state['executed'])) {
$commands = array();
$commands[] = ctools_modal_command_dismiss();
print ajax_render($commands);
exit;
}
else {
print ajax_render($output);
exit;
}
}
function tode_entity_add_form($form, $form_state, $bundle, $entity, $title) {
$form = array();
$form['title'] = array(
'#type' => 'textfield',
'#title' => t($title),
'#size' => 40,
'#maxlength' => 255,
);
$form['bundle'] = array(
'#type' => 'hidden',
'#value' => $bundle,
);
$form['entity'] = array(
'#type' => 'hidden',
'#value' => $entity,
);
$form['create'] = array(
'#type' => 'submit',
'#value' => t('Create'),
);
return $form;
}
function tode_entity_add_form_validate($form, &$form_state){
if(empty($form_state['values']['title'])){
form_set_error('title', 'Title field can\'t be empty!');
}
}
function tode_entity_add_form_submit($form, &$form_state){
global $user;
// dsm($form_state, '$form_state');
$values = $form_state['values'];
$bundle_fields = field_info_instances($values['bundle']);
// dsm($bundle_fields, '$bundle_fields');
$fields = $bundle_fields[$values['entity']];
// dsm($fields, '$fields');
foreach ($fields as $field_name => $field) {
if($field['widget']['type'] == 'tode'){
$tode_field = $field;
break;
}
}
# get vocabulary
$tode_field_infos = field_info_field($field['field_name']);
// dsm($tode_field_infos, '$tode_field_infos');
$voc_name = $tode_field_infos['settings']['allowed_values'][0]['vocabulary'];
$vocabulary = taxonomy_vocabulary_machine_name_load($voc_name);
// dsm($vocabulary, '$vocabulary');
# create the term
$term = new stdClass();
$term->name = $values['title'];
$term->vid = $vocabulary->vid; // 1 is a vocabulary id you wish this term to assign to
// $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
taxonomy_term_save($term); // Finally, save our term
// dsm($term, '$term');
# create the node
//http://www.group42.ca/creating_and_updating_nodes_programmatically_in_drupal_7
//http://timonweb.com/how-programmatically-create-nodes-comments-and-taxonomies-drupal-7
$node = new stdClass();
$node->type = $values['entity'];
node_object_prepare($node);
$tode_field_name = $tode_field['field_name'];
$node = (array)$node +array(
"title" => $values['title'],
"language" => LANGUAGE_NONE,
"uid" => $user->uid,
$tode_field_name => array( LANGUAGE_NONE => array(0 => array('tid' => $term->tid))),
);
// $node->$tode_field_name[$node->language]['tid'] = $term->tid;
// $node->$tode_field_name = array( $node->language => array('tid' => $term->tid));
// $tode_field_name = $tode_field['field_name'];
// $node = (object) array(
// "type" => $values['entity'],
// // node_object_prepare($node);
// "title" => $values['title'],
// "language" => LANGUAGE_NONE,
// "uid" => $user->uid,
// $tode_field_name => array( LANGUAGE_NONE=>array('tid' => $term->tid)),
// );
$node = (object)$node;
// dsm($node, '$node');
node_save($node);
// dsm($node, '$node');
# tag the node with the term
# set the tode field to the term tid
}
function BAD_ONE_tode_entity_add_form($js = FALSE, $bundle, $entity) {
// dsm('- - - - tode_entity_add_form');
// dsm($bundle, '$bundle');
// dsm($entity, '$entity');
// error_log($js);
// error_log($bundle);
// error_log($entity);
//
global $user;
//module_load_include('inc', 'node', 'node.pages');
ctools_include('node.pages', 'node', '');
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => $entity,
'language' => LANGUAGE_NONE
);
$form_id = $entity.'_node_form';
if (!$js) {
return drupal_get_form($form_id, $node);
}
ctools_include('modal');
ctools_include('ajax');
$form_state = array(
'title' => t('Add '.$entity),
'ajax' => TRUE,
);
$form_state['build_info']['args'] = array($node);
$output = ctools_modal_form_wrapper($entity.'_node_form', $form_state);
if (!empty($form_state['executed'])) {
$output = array();
$output[] = ctools_modal_command_display( t('Node created'), '<div class="modal-message">'.$entity.' creation successful.</div>'); /** Add success message*/
};
print ajax_render($output);
exit;
}
/**
* Implements hook_field_widget_form_alter().
*
* add create entity bundle front of term reference field with tode voc
*/
function DESACTIVATED_tode_field_widget_form_alter(&$element, &$form_state, $context) {
$field = $context['field'];
$instance = $context['instance'];
if($field['type'] == 'taxonomy_term_reference' && ($instance['widget']['type'] == 'taxonomy_autocomplete' || $instance['widget']['type'] == 'autocomplete_deluxe_taxonomy')){
// dsm('- - - - - tode_field_widget_form_alter');
// dsm($element, '$element');
// dsm($form_state, '$form_state');
// dsm($context, '$context');
// dsm($instance['widget']['type'], '$instance[widget][type]');
foreach ($field['settings']['allowed_values'] as $key => $value) {
if($tode = _tode_get_voc_tode_fields_def($value['vocabulary'])){
ctools_include('ajax');
ctools_include('modal');
ctools_modal_add_js();
// ctools_add_css('tode', 'tode');
// drupal_add_css(drupal_get_path('module', 'tode') . "tode.css");
// dsm($tode, '$tode');
$type_fields = field_info_instances('node');
if(!isset($element['#attributes']["class"]))
$element['#attributes']["class"] = array();
$element['#attributes']["class"][] = "tode-add-modal";
$element['#suffix'] = '';
foreach ($tode['bundles'] as $bundle => $entities) {
foreach ($entities as $entity) {
$tode_instance = $type_fields[$entity][$tode['field_name']];
$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
// $btn .= ctools_modal_text_button(t('create new test'), 'tode/nojs/add/node/test', t('alt'));
// dsm($btn);
if($element['#suffix'] == '')
$element['#suffix'] = '<div class="tode-add-modal-links">';
$element['#suffix'] .= $btn;
}
}
}
}
if( isset($element['#suffix']) && $element['#suffix'] != '' ){
$throbber = theme('image', array('path' => ctools_image_path('loading_animation.gif', 'modal_forms'), 'alt' => t('Loading...'), 'title' => t('Loading')));
$js_settings = array(
'tode-add-modal' => array(
'modalSize' => array(
'type' => 'fixed',
'width' => 500,
'height' => 200,
),
'modalOptions' => array(
'opacity' => 0.4,
'background' => '#000',
),
'animation' => 'fadeIn',
// 'modalTheme' => 'ModalFormsPopup',
'throbber' => $throbber,
'closeText' => t('Close'),
),
);
drupal_add_js($js_settings, 'setting');
$element['#prefix'] = '<div class="tode-add-modal-wrapper">';
$element['#suffix'] .= '</div></div>';
}
// dsm($element, '$element');
}
}
/**
* Implements hook_taxonomy_term_insert().
*/
// function tode_taxonomy_term_insert($term) {
// dsm($term, '$term');
// }
@@ -515,11 +855,13 @@ function tode_node_presave($node) {
*
*/
function tode_form_alter(&$form, $form_state, $form_id){
// dsm($form_id, 'tode form_alter form_id');
/*
TODO block the deletion if tode term has children !! because this will delete them to …
*/
if($form_id == 'test_node_form'){
// dsm($form_state);
}
if (stripos($form_id, 'node_delete_confirm') !== false){
// dsm($form_id, 'tode form_alter form_id');
_tode_node_delete_form_alter($form, $form_state);
@@ -542,7 +884,7 @@ function _tode_node_delete_form_alter(&$form, $form_state){
// dsm($node, '$node');
#get the fields defenition of node type
$tode_fields = _tode_get_tode_fields_def($node);
$tode_fields = _tode_get_node_tode_fields_def($node);
// dsm($tode_fields);
if(count($tode_fields) == 0)
@@ -567,7 +909,7 @@ function _tode_nodes_delete_form_alter(&$form, $form_state){
$terms = array('names'=>array(), 'tids'=>array());
foreach ($nodes as $node) {
#get the fields definition of node type
$tode_fields = _tode_get_tode_fields_def($node);
$tode_fields = _tode_get_node_tode_fields_def($node);
//dsm($tode_fields);
if(count($tode_fields) == 0)
@@ -585,7 +927,7 @@ function _tode_populate_terms_node_delete(&$terms, $tode_field){
foreach ($tode_field as $language) {
foreach ($language as $term) {
$term = taxonomy_term_load($term['tid']);
if(!in_array($term->tid, $terms['tids'])){
if(isset($term->tid) && !in_array($term->tid, $terms['tids'])){
$terms['names'][] = $term->name;
$terms['tids'][] = $term->tid;
}
@@ -594,6 +936,7 @@ function _tode_populate_terms_node_delete(&$terms, $tode_field){
}
function _tode_node_delete_prepare_form(&$form, $terms){
if(count($terms)){
/*
TODO add here a checkbox to select terms to delete
@@ -620,6 +963,49 @@ function tode_delete_submit($form, &$form_state){
drupal_set_message(t('Following Taxonomy terms have been deleted : %terms', array('%terms' => implode(', ', $terms) )), 'status');
}
/**
* Implements hook_url_outbound_alter().
*/
function tode_url_outbound_alter(&$path, &$options, $original_path) {
# terms url
if(isset($options['entity_type']) && $options['entity_type'] == 'taxonomy_term'){
// dsm('- - - - tode_url_outbound_alter');
// dsm($path, '$path');
// dsm($options, '$options');
// dsm($original_path, '$original_path');
$term = $options['entity'];
if($field = _tode_get_voc_tode_fields_def($term->vocabulary_machine_name)){
$sr = $field['storage']['details']['sql']['FIELD_LOAD_CURRENT'];
foreach ($sr as $table => $column) {
$query = db_select($table, 'ft');
$query->join('node', 'n', 'ft.entity_id = n.nid AND n.language = :language', array(':language' => $options['language']->language));
$query
->fields('ft')
->condition('ft.'.$column['tid'], $term->tid);
$result = $query->execute();
break;
}
foreach ($result as $node) {
$new_path = 'node/'.$node->entity_id;
if( $new_alias = drupal_get_path_alias($new_path, $options['language']->language) ){
$path = $new_alias;
$original_path = $new_path;
$options['alias'] = TRUE;
}else{
$path = $new_path;
}
break;
}
}
}
}
/**
* HELPERS
*/
@@ -708,9 +1094,9 @@ function _tode_trim_options(&$form, $item){
}
/**
* OK OK OK
* _tode_get_node_tode_fields_def
*/
function _tode_get_tode_fields_def($node){
function _tode_get_node_tode_fields_def($node){
// dsm($node, '_tode_get_fields_def');
#get the fields defenition of node type
@@ -727,3 +1113,23 @@ function _tode_get_tode_fields_def($node){
return $tode_fields;
}
function _tode_get_voc_tode_fields_def($voc_name){
$type_fields = field_info_instances('node');
// dsm($type_fields, '$type_fields');
foreach ($type_fields as $nodetype => $fields)
foreach ($fields as $field_name => $field)
if($field['widget']['type'] == 'tode'
&& isset($field['widget']['settings']['redirect_term_to_node'])
&& $field['widget']['settings']['redirect_term_to_node']
){
$fieldinfos = field_info_field($field_name);
// dsm($fieldinfos, '$fieldinfos');
if($voc_name == $fieldinfos['settings']['allowed_values'][0]['vocabulary'])
return $fieldinfos;
}
return false;
}
+33
View File
@@ -0,0 +1,33 @@
<?php
/**
* Implements hook_views_data().
*/
// function tode_views_data() {
// // Code goes here. See example at http://views.doc.logrus.com/group__views__hooks.html#g227057901681e4a33e33c199c7a8c989
//
// $data['node']['tode_parent'] = array(
// 'group' => t('tode'),
// 'title' => t('Tode'),
// 'help' => t('Filtrer with tode parent term.'), // The help that appears on the UI,
// // 'real field' => 'field_pv_value',
// 'filter' => array(
// 'handler' => 'tode_views_handler_parent_filter',
// ),
// );
//
// }
function tode_views_data_alter(&$data) {
$data['tid']['tode_parent'] = array(
'title' => t('Tode parent'),
'group' => 'Tode',
'help' => t('Parent term of tode field'),
'real field' => 'field_tode_program',
'filter' => array(
'handler' => 'tode_views_handler_parent_filter',
),
);
}