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_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 | ||||
|   $field_type = 'field_materio_showroom_location'; | ||||
|   $node = $form['#node']; | ||||
|   $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(); | ||||
|   $showroomfieldinstances = array(); | ||||
|   $user_termref_field_instances = array(); | ||||
|   foreach ($fieldsmap as $field_name => $field) { | ||||
|     // dsm($field,$field_name); | ||||
|     // get all (probably one :p) showroom field instances from the current node | ||||
|     if ($field['type'] == $field_type | ||||
|         && isset($field['bundles']['node']) | ||||
|         && in_array($nodetype, $field['bundles']['node'])) { | ||||
|       $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($user_termref_field_instances, 'user_termref_field_instances'); | ||||
|  | ||||
|   // if there is no showroom field instances do nothing | ||||
|   if(!empty($showroomfieldinstances)){ | ||||
|     // act on each field instance | ||||
|     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 | ||||
|       $vid = $field_instance['widget']['settings']['vocabulary']; | ||||
|       $voc = taxonomy_vocabulary_load($vid); | ||||
|       $tree = taxonomy_get_tree($vid); | ||||
|       foreach ($tree as $key => $term) { | ||||
|         $terms[$term->tid] = $term->name; | ||||
|       } | ||||
|       // 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 | ||||
|       $old_items = field_get_items('node', $node, $field_name); | ||||
|       foreach ($old_items as $i => $value) { | ||||
| @@ -319,10 +348,12 @@ function materio_showroom_form_alter(&$form, &$form_state, $form_id) { | ||||
|       // dsm($new_field_form, 'default_form'); | ||||
|  | ||||
|       // change items location field title | ||||
|       // and check access comparing term id with user taged with | ||||
|       $i = 0; | ||||
|       foreach ($terms as $tid => $name) { | ||||
|         $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']['#disabled'] = !in_array($tid, $user_showrooms); | ||||
|         $i++; | ||||
|       } | ||||
|  | ||||
| @@ -335,8 +366,8 @@ function materio_showroom_form_alter(&$form, &$form_state, $form_id) { | ||||
|  | ||||
|       // remove "add more" button | ||||
|       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