materio_showroom.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. // TODO: alter entity translation field permission with field_permission
  3. // __ __ _ _______ __ __
  4. // / / ____ _________ _/ /_(_)___ ____ / ____(_)__ / /___/ /
  5. // / / / __ \/ ___/ __ `/ __/ / __ \/ __ \ / /_ / / _ \/ / __ /
  6. // / /___/ /_/ / /__/ /_/ / /_/ / /_/ / / / / / __/ / / __/ / /_/ /
  7. // /_____/\____/\___/\__,_/\__/_/\____/_/ /_/ /_/ /_/\___/_/\__,_/
  8. /**
  9. * Implements hook_field_info().
  10. *
  11. * Provides the description of the field.
  12. */
  13. function materio_showroom_field_info() {
  14. return array(
  15. // We name our field as the associative name of the array.
  16. 'field_materio_showroom_location' => array(
  17. 'label' => t('Showroom Location'),
  18. 'description' => t('Define material location by showroom'),
  19. 'default_widget' => 'materio_showroom_location_text',
  20. 'default_formatter' => 'materio_showroom_location_simple_text',
  21. ),
  22. );
  23. // TODO: define in settings wich vocabulary is for showrooms
  24. }
  25. /**
  26. * Implements hook_field_validate().
  27. *
  28. * This hook gives us a chance to validate content that's in our
  29. * field. We're really only interested in the $items parameter, since
  30. * it holds arrays representing content in the field we've defined.
  31. * We want to verify that the items only contain RGB hex values like
  32. * this: #RRGGBB. If the item validates, we do nothing. If it doesn't
  33. * validate, we add our own error notification to the $errors parameter.
  34. *
  35. * @see field_example_field_widget_error()
  36. */
  37. // function materio_showroom_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  38. // foreach ($items as $delta => $item) {
  39. // // if (!empty($item['rgb'])) {
  40. // // if (!preg_match('@^#[0-9a-f]{6}$@', $item['rgb'])) {
  41. // // $errors[$field['field_name']][$langcode][$delta][] = array(
  42. // // 'error' => 'field_example_invalid',
  43. // // 'message' => t('Color must be in the HTML format #abcdef.'),
  44. // // );
  45. // // }
  46. // // }
  47. // }
  48. // }
  49. /**
  50. * Implements hook_field_is_empty().
  51. *
  52. * hook_field_is_empty() is where Drupal asks us if this field is empty.
  53. * Return TRUE if it does not contain data, FALSE if it does. This lets
  54. * the form API flag an error when required fields are empty.
  55. */
  56. function materio_showroom_field_is_empty($item, $field) {
  57. // dsm($item,'item');
  58. // dsm($field,'field');
  59. return empty($item['location']) && empty($item['showroom_tid']);
  60. }
  61. // ______ __
  62. // / ____/___ _________ ___ ____ _/ /____ __________
  63. // / /_ / __ \/ ___/ __ `__ \/ __ `/ __/ _ \/ ___/ ___/
  64. // / __/ / /_/ / / / / / / / / /_/ / /_/ __/ / (__ )
  65. // /_/ \____/_/ /_/ /_/ /_/\__,_/\__/\___/_/ /____/
  66. /**
  67. * Implements hook_field_formatter_info().
  68. *
  69. * We need to tell Drupal that we have two different types of formatters
  70. * for this field. One will change the text color, and the other will
  71. * change the background color.
  72. *
  73. * @see field_example_field_formatter_view()
  74. */
  75. function materio_showroom_field_formatter_info() {
  76. return array(
  77. // This formatter just displays the hex value in the color indicated.
  78. 'materio_showroom_location_simple_text' => array(
  79. 'label' => t('Simple text-based formatter'),
  80. 'field types' => array('field_example_rgb'),
  81. ),
  82. // This formatter changes the background color of the content region.
  83. // 'field_example_color_background' => array(
  84. // 'label' => t('Change the background of the output text'),
  85. // 'field types' => array('field_example_rgb'),
  86. // ),
  87. );
  88. }
  89. /**
  90. * Implements hook_field_formatter_view().
  91. *
  92. * Two formatters are implemented.
  93. * - field_example_simple_text just outputs markup indicating the color that
  94. * was entered and uses an inline style to set the text color to that value.
  95. * - field_example_color_background does the same but also changes the
  96. * background color of div.region-content.
  97. *
  98. * @see field_example_field_formatter_info()
  99. */
  100. function materio_showroom_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  101. $element = array();
  102. switch ($display['type']) {
  103. // This formatter simply outputs the field as text and with a color.
  104. case 'materio_showroom_location_simple_text':
  105. foreach ($items as $delta => $item) {
  106. $element[$delta] = array(
  107. '#type' => 'html_tag',
  108. '#tag' => 'p',
  109. '#value' => t('@loc', array('@loc' => $item['location'])),
  110. );
  111. }
  112. break;
  113. }
  114. return $element;
  115. }
  116. // _ ___ __ __
  117. // | | / (_)___/ /___ ____ / /______
  118. // | | /| / / / __ / __ `/ _ \/ __/ ___/
  119. // | |/ |/ / / /_/ / /_/ / __/ /_(__ )
  120. // |__/|__/_/\__,_/\__, /\___/\__/____/
  121. // /____/
  122. /**
  123. * Implements hook_field_widget_info().
  124. *
  125. * Three widgets are provided.
  126. * - A simple text-only widget where the user enters the '#ffffff'.
  127. * - A 3-textfield widget that gathers the red, green, and blue values
  128. * separately.
  129. * - A farbtastic colorpicker widget that chooses the value graphically.
  130. *
  131. * These widget types will eventually show up in hook_field_widget_form,
  132. * where we will have to flesh them out.
  133. *
  134. * @see field_example_field_widget_form()
  135. */
  136. function materio_showroom_field_widget_info() {
  137. return array(
  138. 'materio_showroom_location_text' => array(
  139. 'label' => t('Location as text'),
  140. 'field types' => array('field_materio_showroom_location'),
  141. ),
  142. );
  143. }
  144. /**
  145. * Implements hook_field_widget_form().
  146. *
  147. * hook_widget_form() is where Drupal tells us to create form elements for
  148. * our field's widget.
  149. *
  150. * We provide one of three different forms, depending on the widget type of
  151. * the Form API item provided.
  152. *
  153. * The 'field_example_colorpicker' and 'field_example_text' are essentially
  154. * the same, but field_example_colorpicker adds a javascript colorpicker
  155. * helper.
  156. *
  157. * field_example_3text displays three text fields, one each for red, green,
  158. * and blue. However, the field type defines a single text column,
  159. * rgb, which needs an HTML color spec. Define an element validate
  160. * handler that converts our r, g, and b fields into a simulated single
  161. * 'rgb' form element.
  162. */
  163. function materio_showroom_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  164. $locval = isset($items[$delta]['location']) ? $items[$delta]['location'] : '';
  165. $showroom_tid = isset($items[$delta]['showroom_tid']) ? $items[$delta]['showroom_tid'] : 0;
  166. $widget = $element;
  167. $widget['#delta'] = $delta;
  168. switch ($instance['widget']['type']) {
  169. // TODO: loop through showrooms and don't allow more than one field by show room
  170. case 'materio_showroom_location_text':
  171. $widget['showroom_tid'] = array(
  172. '#type' => 'hidden',
  173. '#default_value' => $showroom_tid,
  174. // Allow a slightly larger size that the field length to allow for some
  175. // configurations where all characters won't fit in input field.
  176. '#size' => 10,
  177. '#maxlength' => 10,
  178. );
  179. $widget['location'] = array(
  180. '#type' => 'textfield',
  181. '#title' => "Showroom", //$showroom_term->name,
  182. '#default_value' => $locval,
  183. // Allow a slightly larger size that the field length to allow for some
  184. // configurations where all characters won't fit in input field.
  185. '#size' => 10,
  186. '#maxlength' => 10,
  187. );
  188. break;
  189. }
  190. return $widget;
  191. }
  192. /**
  193. * Implements hook_field_widget_settings_form().
  194. */
  195. function materio_showroom_field_widget_settings_form($field, $instance) {
  196. $widget = $instance['widget'];
  197. $settings = $widget['settings'];
  198. $vocs = taxonomy_get_vocabularies();
  199. $options = array();
  200. foreach ($vocs as $vid => $voc) {
  201. $options[$vid] = $voc->name;
  202. }
  203. $form['vocabulary'] = array(
  204. '#type' => 'select',
  205. '#title' => t('Vocabulary'),
  206. '#default_value' => $settings['vocabulary'],
  207. // '#element_validate' => array('_tode_widget_settings_maxlength_validate'),
  208. '#required' => TRUE,
  209. '#description' => t('Choose which vocabulary will be associated as showroom.'),
  210. '#options' => $options,
  211. );
  212. return $form;
  213. }
  214. /**
  215. * Implements hook_form_alter().
  216. */
  217. function materio_showroom_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  218. if($form['#field']['type'] == "field_materio_showroom_location"){
  219. // dsm($form, 'form');
  220. $form['field']['cardinality']['#disabled'] = 'true';
  221. $form['field']['cardinality']['#default_value'] = -1;
  222. }
  223. //
  224. }
  225. /**
  226. * Implements hook_form_alter().
  227. */
  228. function materio_showroom_form_alter(&$form, &$form_state, $form_id) {
  229. // act only on node edit form
  230. if(isset($form['#node_edit_form'])){
  231. // dsm($form_id, 'form_id');
  232. // dsm($form, 'form');
  233. // dsm($form_state, 'form_state');
  234. // define some constants
  235. $field_type = 'field_materio_showroom_location';
  236. $node = $form['#node'];
  237. $nodetype = $form['type']['#value'];
  238. // get all (probably one :p) showroom field instances from the current node
  239. $fieldsmap = field_info_field_map();
  240. $showroomfieldinstances = array();
  241. foreach ($fieldsmap as $field_name => $field) {
  242. // dsm($field,$field_name);
  243. if ($field['type'] == $field_type
  244. && isset($field['bundles']['node'])
  245. && in_array($nodetype, $field['bundles']['node'])) {
  246. $showroomfieldinstances[] = $field_name;
  247. }
  248. }
  249. // dsm($showroomfieldinstances, 'showroomfieldinstances');
  250. if(!empty($showroomfieldinstances)){
  251. // act on each field instance
  252. foreach ($showroomfieldinstances as $field_name) {
  253. // retrive various field infos
  254. $field_info = field_info_field($field_name);
  255. $field_instance = field_info_instance('node', $field_name, $nodetype);
  256. // dsm($field_info, 'field_info');
  257. // get all terms from chosen vocabulary in field instance widget settings
  258. $vid = $field_instance['widget']['settings']['vocabulary'];
  259. $tree = taxonomy_get_tree($vid);
  260. foreach ($tree as $key => $term) {
  261. $terms[$term->tid] = $term->name;
  262. }
  263. // dsm($terms, 'terms');
  264. // get already recorded values
  265. $old_items = field_get_items('node', $node, $field_name);
  266. foreach ($old_items as $i => $value) {
  267. $values[$value['showroom_tid']] = $value['location'];
  268. }
  269. // dsm($values, 'values');
  270. // build new item list
  271. foreach ($terms as $tid => $name) {
  272. $items[] = array(
  273. 'showroom_tid' => $tid,
  274. 'location' => isset($values[$tid]) ? $values[$tid] : ''
  275. );
  276. }
  277. // dsm($items, 'items');
  278. // retrieve new field form with our custom items
  279. $new_field_form = field_default_form('node', $node, $field_info, $field_instance, LANGUAGE_NONE, $items, $form, $form_state);
  280. //dsm($new_field_form, 'default_form');
  281. // change items location field title
  282. $i = 0;
  283. foreach ($terms as $tid => $name) {
  284. $item = $new_field_form[$field_name][LANGUAGE_NONE][$i];
  285. $new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#title'] = $terms[$item['showroom_tid']['#default_value']];
  286. $i++;
  287. }
  288. // remove the last one more item added by default
  289. unset($new_field_form[$field_name][LANGUAGE_NONE][$i]);
  290. // delete normal field form and replace it with our new custom field form
  291. unset($form[$field_name]);
  292. $form[$field_name] = $new_field_form[$field_name];
  293. // remove "add more" button
  294. unset($form[$field_name]['und']['add_more']);
  295. }
  296. }
  297. }
  298. }