added per user showroom location field access
This commit is contained in:
		| @@ -264,24 +264,43 @@ function materio_showroom_form_alter(&$form, &$form_state, $form_id) { | |||||||
|     // dsm($form, 'form'); |     // dsm($form, 'form'); | ||||||
|     // dsm($form_state, 'form_state'); |     // dsm($form_state, 'form_state'); | ||||||
|  |  | ||||||
|  |     _materio_showroom_alter_location_field_form($form, $form_state, $form_id); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function _materio_showroom_alter_location_field_form(&$form, &$form_state, $form_id){ | ||||||
|   // define some constants |   // define some constants | ||||||
|   $field_type = 'field_materio_showroom_location'; |   $field_type = 'field_materio_showroom_location'; | ||||||
|   $node = $form['#node']; |   $node = $form['#node']; | ||||||
|   $nodetype = $form['type']['#value']; |   $nodetype = $form['type']['#value']; | ||||||
|  |  | ||||||
|     // get all (probably one :p) showroom field instances from the current node |   global $user; | ||||||
|  |   $user = user_load($user->uid); // Make sure the user object is fully loaded | ||||||
|  |   // dsm($user, 'user'); | ||||||
|  |  | ||||||
|   $fieldsmap = field_info_field_map(); |   $fieldsmap = field_info_field_map(); | ||||||
|   $showroomfieldinstances = array(); |   $showroomfieldinstances = array(); | ||||||
|  |   $user_termref_field_instances = array(); | ||||||
|   foreach ($fieldsmap as $field_name => $field) { |   foreach ($fieldsmap as $field_name => $field) { | ||||||
|     // dsm($field,$field_name); |     // dsm($field,$field_name); | ||||||
|  |     // get all (probably one :p) showroom field instances from the current node | ||||||
|     if ($field['type'] == $field_type |     if ($field['type'] == $field_type | ||||||
|         && isset($field['bundles']['node']) |         && isset($field['bundles']['node']) | ||||||
|         && in_array($nodetype, $field['bundles']['node'])) { |         && in_array($nodetype, $field['bundles']['node'])) { | ||||||
|       $showroomfieldinstances[] = $field_name; |       $showroomfieldinstances[] = $field_name; | ||||||
|     } |     } | ||||||
|  |     // get all term reference fields instance from users | ||||||
|  |     if (isset($field['bundles']['user']) | ||||||
|  |         && $field['type'] == 'taxonomy_term_reference') { | ||||||
|  |       $field_info = field_info_field($field_name); | ||||||
|  |       // dsm($field_info, "field_info"); | ||||||
|  |       $user_termref_field_instances[$field_info['settings']['allowed_values'][0]['vocabulary']] = $field_name; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
|   // dsm($showroomfieldinstances, 'showroomfieldinstances'); |   // dsm($showroomfieldinstances, 'showroomfieldinstances'); | ||||||
|  |   // dsm($user_termref_field_instances, 'user_termref_field_instances'); | ||||||
|  |  | ||||||
|  |   // if there is no showroom field instances do nothing | ||||||
|   if(!empty($showroomfieldinstances)){ |   if(!empty($showroomfieldinstances)){ | ||||||
|     // act on each field instance |     // act on each field instance | ||||||
|     foreach ($showroomfieldinstances as $field_name) { |     foreach ($showroomfieldinstances as $field_name) { | ||||||
| @@ -292,12 +311,22 @@ function materio_showroom_form_alter(&$form, &$form_state, $form_id) { | |||||||
|  |  | ||||||
|       // get all terms from chosen vocabulary in field instance widget settings |       // get all terms from chosen vocabulary in field instance widget settings | ||||||
|       $vid = $field_instance['widget']['settings']['vocabulary']; |       $vid = $field_instance['widget']['settings']['vocabulary']; | ||||||
|  |       $voc = taxonomy_vocabulary_load($vid); | ||||||
|       $tree = taxonomy_get_tree($vid); |       $tree = taxonomy_get_tree($vid); | ||||||
|       foreach ($tree as $key => $term) { |       foreach ($tree as $key => $term) { | ||||||
|         $terms[$term->tid] = $term->name; |         $terms[$term->tid] = $term->name; | ||||||
|       } |       } | ||||||
|       // dsm($terms, 'terms'); |       // dsm($terms, 'terms'); | ||||||
|  |  | ||||||
|  |       // get user own term for current vocabulary (if any) | ||||||
|  |       if(isset($user_termref_field_instances[$voc->machine_name])){ | ||||||
|  |         $user_field_name = $user_termref_field_instances[$voc->machine_name]; | ||||||
|  |         foreach (field_get_items('user', $user, $user_field_name) as $key => $value) { | ||||||
|  |           $user_showrooms[] = $value['tid']; | ||||||
|  |         } | ||||||
|  |         // dsm($user_showrooms, "user_showrooms"); | ||||||
|  |       } | ||||||
|  |  | ||||||
|       // get already recorded values |       // get already recorded values | ||||||
|       $old_items = field_get_items('node', $node, $field_name); |       $old_items = field_get_items('node', $node, $field_name); | ||||||
|       foreach ($old_items as $i => $value) { |       foreach ($old_items as $i => $value) { | ||||||
| @@ -316,13 +345,15 @@ function materio_showroom_form_alter(&$form, &$form_state, $form_id) { | |||||||
|  |  | ||||||
|       // retrieve new field form with our custom items |       // retrieve new field form with our custom items | ||||||
|       $new_field_form = field_default_form('node', $node, $field_info, $field_instance, LANGUAGE_NONE, $items, $form, $form_state); |       $new_field_form = field_default_form('node', $node, $field_info, $field_instance, LANGUAGE_NONE, $items, $form, $form_state); | ||||||
|         //dsm($new_field_form, 'default_form'); |       // dsm($new_field_form, 'default_form'); | ||||||
|  |  | ||||||
|       // change items location field title |       // change items location field title | ||||||
|  |       // and check access comparing term id with user taged with | ||||||
|       $i = 0; |       $i = 0; | ||||||
|       foreach ($terms as $tid => $name) { |       foreach ($terms as $tid => $name) { | ||||||
|         $item = $new_field_form[$field_name][LANGUAGE_NONE][$i]; |         $item = $new_field_form[$field_name][LANGUAGE_NONE][$i]; | ||||||
|         $new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#title'] = $terms[$item['showroom_tid']['#default_value']]; |         $new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#title'] = $terms[$item['showroom_tid']['#default_value']]; | ||||||
|  |         $new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#disabled'] = !in_array($tid, $user_showrooms); | ||||||
|         $i++; |         $i++; | ||||||
|       } |       } | ||||||
|  |  | ||||||
| @@ -335,8 +366,8 @@ function materio_showroom_form_alter(&$form, &$form_state, $form_id) { | |||||||
|  |  | ||||||
|       // remove "add more" button |       // remove "add more" button | ||||||
|       unset($form[$field_name]['und']['add_more']); |       unset($form[$field_name]['und']['add_more']); | ||||||
|  |  | ||||||
|       } |  | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // TODO: migrate old location field to new one | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Bachir Soussi Chiadmi
					Bachir Soussi Chiadmi