From 1ccc5d07d6818afe8292c2a739c9f93f89622246 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Sat, 26 Nov 2016 21:18:06 +0100 Subject: [PATCH] coded the showroom field still have to manage access --- .../materiobasemod/materio_showroom.module | 287 ++++++++---------- 1 file changed, 133 insertions(+), 154 deletions(-) diff --git a/sites/all/modules/gui/materiobasemod/materio_showroom.module b/sites/all/modules/gui/materiobasemod/materio_showroom.module index ef51324a..81295968 100644 --- a/sites/all/modules/gui/materiobasemod/materio_showroom.module +++ b/sites/all/modules/gui/materiobasemod/materio_showroom.module @@ -1,61 +1,5 @@ field_name == 'field_showroom_localisation') { - // dsm($entity); - - global $user; - $user = user_load($user->uid); // Make sure the user object is fully loaded - // dsm($user); - if(isset($entity->field_showroom_localisation_loca[LANGUAGE_NONE])){ - - $user_showroom = $user->field_showroom[LANGUAGE_NONE][0]['tid']; - // dsm($user_showroom); - - if(empty($entity->field_showroom_localisation_show)){ - $entity->field_showroom_localisation_show[LANGUAGE_NONE] = []; - } - - foreach ($entity->field_showroom_localisation_loca[LANGUAGE_NONE] as $i => $loca) { - // if(empty($entity->field_showroom_localisation_show[LANGUAGE_NONE])){ - $entity->field_showroom_localisation_show[LANGUAGE_NONE][] = array('tid'=>''); - // } - dsm($entity->field_showroom_localisation_show); - - if(!isset($entity->original->field_showroom_localisation_show[LANGUAGE_NONE][$i]['tid'])){ - $tid = $user_showroom; - }else{ - $tid = $entity->original->field_showroom_localisation_show[LANGUAGE_NONE][$i]['tid']; - } - dsm($tid); - $entity->field_showroom_localisation_show[LANGUAGE_NONE][$i]['tid'] = $tid; - dsm($entity->field_showroom_localisation_show); - } - } - } -} - -// function materio_showroom_entity_load($entities, $type) { - // dsm($type); - // if($type == 'node'){ - // dsm($entities); - // } - // foreach ($entities as $entity) { - // $entity->foo = mymodule_add_something($entity, $type); - // } -// } - // TODO: alter entity translation field permission with field_permission // __ __ _ _______ __ __ @@ -64,7 +8,6 @@ function materio_showroom_entity_presave($entity, $type) { // / /___/ /_/ / /__/ /_/ / /_/ / /_/ / / / / / __/ / / __/ / /_/ / // /_____/\____/\___/\__,_/\__/_/\____/_/ /_/ /_/ /_/\___/_/\__,_/ -// TODO: create own location field /** * Implements hook_field_info(). * @@ -97,18 +40,18 @@ function materio_showroom_field_info() { * * @see field_example_field_widget_error() */ -function materio_showroom_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) { - foreach ($items as $delta => $item) { - // if (!empty($item['rgb'])) { - // if (!preg_match('@^#[0-9a-f]{6}$@', $item['rgb'])) { - // $errors[$field['field_name']][$langcode][$delta][] = array( - // 'error' => 'field_example_invalid', - // 'message' => t('Color must be in the HTML format #abcdef.'), - // ); - // } - // } - } -} +// function materio_showroom_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) { +// foreach ($items as $delta => $item) { +// // if (!empty($item['rgb'])) { +// // if (!preg_match('@^#[0-9a-f]{6}$@', $item['rgb'])) { +// // $errors[$field['field_name']][$langcode][$delta][] = array( +// // 'error' => 'field_example_invalid', +// // 'message' => t('Color must be in the HTML format #abcdef.'), +// // ); +// // } +// // } +// } +// } /** * Implements hook_field_is_empty(). @@ -118,7 +61,9 @@ function materio_showroom_field_validate($entity_type, $entity, $field, $instanc * the form API flag an error when required fields are empty. */ function materio_showroom_field_is_empty($item, $field) { - return empty($item['location']); + // dsm($item,'item'); + // dsm($field,'field'); + return empty($item['location']) && empty($item['showroom_tid']); } // ______ __ @@ -171,38 +116,12 @@ function materio_showroom_field_formatter_view($entity_type, $entity, $field, $i case 'materio_showroom_location_simple_text': foreach ($items as $delta => $item) { $element[$delta] = array( - // We create a render array to produce the desired markup, - // "

The color code ... #hexcolor

". - // See theme_html_tag(). '#type' => 'html_tag', '#tag' => 'p', - // '#attributes' => array( - // 'style' => 'color: ' . $item['rgb'], - // ), '#value' => t('@loc', array('@loc' => $item['location'])), ); } break; - - // This formatter adds css to the page changing the '.region-content' area's - // background color. If there are many fields, the last one will win. - // case 'field_example_color_background': - // foreach ($items as $delta => $item) { - // $element[$delta] = array( - // '#type' => 'html_tag', - // '#tag' => 'p', - // '#value' => t('The content area color has been changed to @code', array('@code' => $item['rgb'])), - // '#attached' => array( - // 'css' => array( - // array( - // 'data' => 'div.region-content { background-color:' . $item['rgb'] . ';}', - // 'type' => 'inline', - // ), - // ), - // ), - // ); - // } - // break; } return $element; @@ -236,14 +155,6 @@ function materio_showroom_field_widget_info() { 'label' => t('Location as text'), 'field types' => array('field_materio_showroom_location'), ), - // 'field_example_3text' => array( - // 'label' => t('RGB text field'), - // 'field types' => array('field_example_rgb'), - // ), - // 'field_example_colorpicker' => array( - // 'label' => t('Color Picker'), - // 'field types' => array('field_example_rgb'), - // ), ); } @@ -269,20 +180,11 @@ function materio_showroom_field_widget_info() { */ function materio_showroom_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { $locval = isset($items[$delta]['location']) ? $items[$delta]['location'] : ''; - - global $user; - $user = user_load($user->uid); // Make sure the user object is fully loaded - // dsm($user, 'user'); - - $showroom_tid = $user->field_showroom[LANGUAGE_NONE][0]['tid']; - // dsm($user_showroom); - $showroom_term = taxonomy_term_load($showroom_tid); - // dsm($showroom_term); + $showroom_tid = isset($items[$delta]['showroom_tid']) ? $items[$delta]['showroom_tid'] : 0; $widget = $element; $widget['#delta'] = $delta; - switch ($instance['widget']['type']) { // TODO: loop through showrooms and don't allow more than one field by show room @@ -299,7 +201,7 @@ function materio_showroom_field_widget_form(&$form, &$form_state, $field, $insta $widget['location'] = array( '#type' => 'textfield', - '#title' => $showroom_term->name, + '#title' => "Showroom", //$showroom_term->name, '#default_value' => $locval, // Allow a slightly larger size that the field length to allow for some // configurations where all characters won't fit in input field. @@ -308,56 +210,133 @@ function materio_showroom_field_widget_form(&$form, &$form_state, $field, $insta ); break; - } - $element['location'] = $widget; - return $element; + return $widget; } /** - * Validate the individual fields and then convert to RGB string. + * Implements hook_field_widget_settings_form(). */ -function materio_showroom_location_text_validate($element, &$form_state) { - // @todo: Isn't there a better way to find out which element? - // $delta = $element['#delta']; - // $field = $form_state['field'][$element['#field_name']][$element['#language']]['field']; - // $field_name = $field['field_name']; - // if (isset($form_state['values'][$field_name][$element['#language']][$delta]['rgb'])) { - // $values = $form_state['values'][$field_name][$element['#language']][$delta]['rgb']; - // foreach (array('r', 'g', 'b') as $colorfield) { - // $colorfield_value = hexdec($values[$colorfield]); - // // If they left any empty, we'll set the value empty and quit. - // if (strlen($values[$colorfield]) == 0) { - // form_set_value($element, '', $form_state); - // return; - // } - // // If they gave us anything that's not hex, reject it. - // if ((strlen($values[$colorfield]) != 2) || $colorfield_value < 0 || $colorfield_value > 255) { - // form_error($element[$colorfield], t("Saturation value must be a 2-digit hexadecimal value between 00 and ff.")); - // } - // } +function materio_showroom_field_widget_settings_form($field, $instance) { + $widget = $instance['widget']; + $settings = $widget['settings']; + + $vocs = taxonomy_get_vocabularies(); + $options = array(); + foreach ($vocs as $vid => $voc) { + $options[$vid] = $voc->name; + } + + $form['vocabulary'] = array( + '#type' => 'select', + '#title' => t('Vocabulary'), + '#default_value' => $settings['vocabulary'], + // '#element_validate' => array('_tode_widget_settings_maxlength_validate'), + '#required' => TRUE, + '#description' => t('Choose which vocabulary will be associated as showroom.'), + '#options' => $options, + ); + + return $form; +} + +/** + * Implements hook_form_alter(). + */ +function materio_showroom_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) { + if($form['#field']['type'] == "field_materio_showroom_location"){ + // dsm($form, 'form'); + $form['field']['cardinality']['#disabled'] = 'true'; + $form['field']['cardinality']['#default_value'] = -1; + } // - // $value = sprintf('#%02s%02s%02s', $values['r'], $values['g'], $values['b']); - // form_set_value($element, $value, $form_state); - // } } /** - * Implements hook_field_widget_error(). - * - * hook_field_widget_error() lets us figure out what to do with errors - * we might have generated in hook_field_validate(). Generally, we'll just - * call form_error(). - * - * @see field_example_field_validate() - * @see form_error() + * Implements hook_form_alter(). */ -function materio_showroom_field_widget_error($element, $error, $form, &$form_state) { - switch ($error['error']) { - case 'materio_showroom_invalid': - form_error($element, $error['message']); - break; +function materio_showroom_form_alter(&$form, &$form_state, $form_id) { + // act only on node edit form + if(isset($form['#node_edit_form'])){ + // dsm($form_id, 'form_id'); + // dsm($form, 'form'); + // dsm($form_state, 'form_state'); + + // 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 + $fieldsmap = field_info_field_map(); + $showroomfieldinstances = array(); + foreach ($fieldsmap as $field_name => $field) { + // dsm($field,$field_name); + if ($field['type'] == $field_type + && isset($field['bundles']['node']) + && in_array($nodetype, $field['bundles']['node'])) { + $showroomfieldinstances[] = $field_name; + } + } + // dsm($showroomfieldinstances, 'showroomfieldinstances'); + + if(!empty($showroomfieldinstances)){ + // act on each field instance + foreach ($showroomfieldinstances as $field_name) { + // retrive various field infos + $field_info = field_info_field($field_name); + $field_instance = field_info_instance('node', $field_name, $nodetype); + // dsm($field_info, 'field_info'); + + // get all terms from chosen vocabulary in field instance widget settings + $vid = $field_instance['widget']['settings']['vocabulary']; + $tree = taxonomy_get_tree($vid); + foreach ($tree as $key => $term) { + $terms[$term->tid] = $term->name; + } + // dsm($terms, 'terms'); + + // get already recorded values + $old_items = field_get_items('node', $node, $field_name); + foreach ($old_items as $i => $value) { + $values[$value['showroom_tid']] = $value['location']; + } + // dsm($values, 'values'); + + // build new item list + foreach ($terms as $tid => $name) { + $items[] = array( + 'showroom_tid' => $tid, + 'location' => isset($values[$tid]) ? $values[$tid] : '' + ); + } + // dsm($items, '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); + //dsm($new_field_form, 'default_form'); + + // change items location field title + $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']]; + $i++; + } + + // remove the last one more item added by default + unset($new_field_form[$field_name][LANGUAGE_NONE][$i]); + + // delete normal field form and replace it with our new custom field form + unset($form[$field_name]); + $form[$field_name] = $new_field_form[$field_name]; + + // remove "add more" button + unset($form[$field_name]['und']['add_more']); + + } + } } }