materio_showroom.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /**
  3. * Acts on a field collection item being inserted or updated.
  4. *
  5. * This hook is invoked before the field collection item is saved to the database.
  6. *
  7. * @param FieldCollectionItemEntity $field_collection_item
  8. * The field collection item that is being inserted or updated.
  9. *
  10. * @see hook_entity_presave()
  11. */
  12. function materio_showroom_entity_presave($entity, $type) {
  13. // dsm($type);
  14. if ($type == 'field_collection_item' && $entity->field_name == 'field_showroom_localisation') {
  15. // dsm($entity);
  16. global $user;
  17. $user = user_load($user->uid); // Make sure the user object is fully loaded
  18. // dsm($user);
  19. if(isset($entity->field_showroom_localisation_loca[LANGUAGE_NONE])){
  20. $user_showroom = $user->field_showroom[LANGUAGE_NONE][0]['tid'];
  21. // dsm($user_showroom);
  22. if(empty($entity->field_showroom_localisation_show)){
  23. $entity->field_showroom_localisation_show[LANGUAGE_NONE] = [];
  24. }
  25. foreach ($entity->field_showroom_localisation_loca[LANGUAGE_NONE] as $i => $loca) {
  26. // if(empty($entity->field_showroom_localisation_show[LANGUAGE_NONE])){
  27. $entity->field_showroom_localisation_show[LANGUAGE_NONE][] = array('tid'=>'');
  28. // }
  29. dsm($entity->field_showroom_localisation_show);
  30. if(!isset($entity->original->field_showroom_localisation_show[LANGUAGE_NONE][$i]['tid'])){
  31. $tid = $user_showroom;
  32. }else{
  33. $tid = $entity->original->field_showroom_localisation_show[LANGUAGE_NONE][$i]['tid'];
  34. }
  35. dsm($tid);
  36. $entity->field_showroom_localisation_show[LANGUAGE_NONE][$i]['tid'] = $tid;
  37. dsm($entity->field_showroom_localisation_show);
  38. }
  39. }
  40. }
  41. }
  42. // function materio_showroom_entity_load($entities, $type) {
  43. // dsm($type);
  44. // if($type == 'node'){
  45. // dsm($entities);
  46. // }
  47. // foreach ($entities as $entity) {
  48. // $entity->foo = mymodule_add_something($entity, $type);
  49. // }
  50. // }
  51. // TODO: alter entity translation field permission with field_permission
  52. // __ __ _ _______ __ __
  53. // / / ____ _________ _/ /_(_)___ ____ / ____(_)__ / /___/ /
  54. // / / / __ \/ ___/ __ `/ __/ / __ \/ __ \ / /_ / / _ \/ / __ /
  55. // / /___/ /_/ / /__/ /_/ / /_/ / /_/ / / / / / __/ / / __/ / /_/ /
  56. // /_____/\____/\___/\__,_/\__/_/\____/_/ /_/ /_/ /_/\___/_/\__,_/
  57. // TODO: create own location field
  58. /**
  59. * Implements hook_field_info().
  60. *
  61. * Provides the description of the field.
  62. */
  63. function materio_showroom_field_info() {
  64. return array(
  65. // We name our field as the associative name of the array.
  66. 'field_materio_showroom_location' => array(
  67. 'label' => t('Showroom Location'),
  68. 'description' => t('Define material location by showroom'),
  69. 'default_widget' => 'materio_showroom_location_text',
  70. 'default_formatter' => 'materio_showroom_location_simple_text',
  71. ),
  72. );
  73. // TODO: define in settings wich vocabulary is for showrooms
  74. }
  75. /**
  76. * Implements hook_field_validate().
  77. *
  78. * This hook gives us a chance to validate content that's in our
  79. * field. We're really only interested in the $items parameter, since
  80. * it holds arrays representing content in the field we've defined.
  81. * We want to verify that the items only contain RGB hex values like
  82. * this: #RRGGBB. If the item validates, we do nothing. If it doesn't
  83. * validate, we add our own error notification to the $errors parameter.
  84. *
  85. * @see field_example_field_widget_error()
  86. */
  87. function materio_showroom_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  88. foreach ($items as $delta => $item) {
  89. // if (!empty($item['rgb'])) {
  90. // if (!preg_match('@^#[0-9a-f]{6}$@', $item['rgb'])) {
  91. // $errors[$field['field_name']][$langcode][$delta][] = array(
  92. // 'error' => 'field_example_invalid',
  93. // 'message' => t('Color must be in the HTML format #abcdef.'),
  94. // );
  95. // }
  96. // }
  97. }
  98. }
  99. /**
  100. * Implements hook_field_is_empty().
  101. *
  102. * hook_field_is_empty() is where Drupal asks us if this field is empty.
  103. * Return TRUE if it does not contain data, FALSE if it does. This lets
  104. * the form API flag an error when required fields are empty.
  105. */
  106. function materio_showroom_field_is_empty($item, $field) {
  107. return empty($item['location']);
  108. }
  109. // ______ __
  110. // / ____/___ _________ ___ ____ _/ /____ __________
  111. // / /_ / __ \/ ___/ __ `__ \/ __ `/ __/ _ \/ ___/ ___/
  112. // / __/ / /_/ / / / / / / / / /_/ / /_/ __/ / (__ )
  113. // /_/ \____/_/ /_/ /_/ /_/\__,_/\__/\___/_/ /____/
  114. /**
  115. * Implements hook_field_formatter_info().
  116. *
  117. * We need to tell Drupal that we have two different types of formatters
  118. * for this field. One will change the text color, and the other will
  119. * change the background color.
  120. *
  121. * @see field_example_field_formatter_view()
  122. */
  123. function materio_showroom_field_formatter_info() {
  124. return array(
  125. // This formatter just displays the hex value in the color indicated.
  126. 'materio_showroom_location_simple_text' => array(
  127. 'label' => t('Simple text-based formatter'),
  128. 'field types' => array('field_example_rgb'),
  129. ),
  130. // This formatter changes the background color of the content region.
  131. // 'field_example_color_background' => array(
  132. // 'label' => t('Change the background of the output text'),
  133. // 'field types' => array('field_example_rgb'),
  134. // ),
  135. );
  136. }
  137. /**
  138. * Implements hook_field_formatter_view().
  139. *
  140. * Two formatters are implemented.
  141. * - field_example_simple_text just outputs markup indicating the color that
  142. * was entered and uses an inline style to set the text color to that value.
  143. * - field_example_color_background does the same but also changes the
  144. * background color of div.region-content.
  145. *
  146. * @see field_example_field_formatter_info()
  147. */
  148. function materio_showroom_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  149. $element = array();
  150. switch ($display['type']) {
  151. // This formatter simply outputs the field as text and with a color.
  152. case 'materio_showroom_location_simple_text':
  153. foreach ($items as $delta => $item) {
  154. $element[$delta] = array(
  155. // We create a render array to produce the desired markup,
  156. // "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
  157. // See theme_html_tag().
  158. '#type' => 'html_tag',
  159. '#tag' => 'p',
  160. // '#attributes' => array(
  161. // 'style' => 'color: ' . $item['rgb'],
  162. // ),
  163. '#value' => t('@loc', array('@loc' => $item['location'])),
  164. );
  165. }
  166. break;
  167. // This formatter adds css to the page changing the '.region-content' area's
  168. // background color. If there are many fields, the last one will win.
  169. // case 'field_example_color_background':
  170. // foreach ($items as $delta => $item) {
  171. // $element[$delta] = array(
  172. // '#type' => 'html_tag',
  173. // '#tag' => 'p',
  174. // '#value' => t('The content area color has been changed to @code', array('@code' => $item['rgb'])),
  175. // '#attached' => array(
  176. // 'css' => array(
  177. // array(
  178. // 'data' => 'div.region-content { background-color:' . $item['rgb'] . ';}',
  179. // 'type' => 'inline',
  180. // ),
  181. // ),
  182. // ),
  183. // );
  184. // }
  185. // break;
  186. }
  187. return $element;
  188. }
  189. // _ ___ __ __
  190. // | | / (_)___/ /___ ____ / /______
  191. // | | /| / / / __ / __ `/ _ \/ __/ ___/
  192. // | |/ |/ / / /_/ / /_/ / __/ /_(__ )
  193. // |__/|__/_/\__,_/\__, /\___/\__/____/
  194. // /____/
  195. /**
  196. * Implements hook_field_widget_info().
  197. *
  198. * Three widgets are provided.
  199. * - A simple text-only widget where the user enters the '#ffffff'.
  200. * - A 3-textfield widget that gathers the red, green, and blue values
  201. * separately.
  202. * - A farbtastic colorpicker widget that chooses the value graphically.
  203. *
  204. * These widget types will eventually show up in hook_field_widget_form,
  205. * where we will have to flesh them out.
  206. *
  207. * @see field_example_field_widget_form()
  208. */
  209. function materio_showroom_field_widget_info() {
  210. return array(
  211. 'materio_showroom_location_text' => array(
  212. 'label' => t('Location as text'),
  213. 'field types' => array('field_materio_showroom_location'),
  214. ),
  215. // 'field_example_3text' => array(
  216. // 'label' => t('RGB text field'),
  217. // 'field types' => array('field_example_rgb'),
  218. // ),
  219. // 'field_example_colorpicker' => array(
  220. // 'label' => t('Color Picker'),
  221. // 'field types' => array('field_example_rgb'),
  222. // ),
  223. );
  224. }
  225. /**
  226. * Implements hook_field_widget_form().
  227. *
  228. * hook_widget_form() is where Drupal tells us to create form elements for
  229. * our field's widget.
  230. *
  231. * We provide one of three different forms, depending on the widget type of
  232. * the Form API item provided.
  233. *
  234. * The 'field_example_colorpicker' and 'field_example_text' are essentially
  235. * the same, but field_example_colorpicker adds a javascript colorpicker
  236. * helper.
  237. *
  238. * field_example_3text displays three text fields, one each for red, green,
  239. * and blue. However, the field type defines a single text column,
  240. * rgb, which needs an HTML color spec. Define an element validate
  241. * handler that converts our r, g, and b fields into a simulated single
  242. * 'rgb' form element.
  243. */
  244. function materio_showroom_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  245. $locval = isset($items[$delta]['location']) ? $items[$delta]['location'] : '';
  246. global $user;
  247. $user = user_load($user->uid); // Make sure the user object is fully loaded
  248. // dsm($user, 'user');
  249. $showroom_tid = $user->field_showroom[LANGUAGE_NONE][0]['tid'];
  250. // dsm($user_showroom);
  251. $showroom_term = taxonomy_term_load($showroom_tid);
  252. // dsm($showroom_term);
  253. $widget = $element;
  254. $widget['#delta'] = $delta;
  255. switch ($instance['widget']['type']) {
  256. // TODO: loop through showrooms and don't allow more than one field by show room
  257. case 'materio_showroom_location_text':
  258. $widget['showroom_tid'] = array(
  259. '#type' => 'hidden',
  260. '#default_value' => $showroom_tid,
  261. // Allow a slightly larger size that the field length to allow for some
  262. // configurations where all characters won't fit in input field.
  263. '#size' => 10,
  264. '#maxlength' => 10,
  265. );
  266. $widget['location'] = array(
  267. '#type' => 'textfield',
  268. '#title' => $showroom_term->name,
  269. '#default_value' => $locval,
  270. // Allow a slightly larger size that the field length to allow for some
  271. // configurations where all characters won't fit in input field.
  272. '#size' => 10,
  273. '#maxlength' => 10,
  274. );
  275. break;
  276. }
  277. $element['location'] = $widget;
  278. return $element;
  279. }
  280. /**
  281. * Validate the individual fields and then convert to RGB string.
  282. */
  283. function materio_showroom_location_text_validate($element, &$form_state) {
  284. // @todo: Isn't there a better way to find out which element?
  285. // $delta = $element['#delta'];
  286. // $field = $form_state['field'][$element['#field_name']][$element['#language']]['field'];
  287. // $field_name = $field['field_name'];
  288. // if (isset($form_state['values'][$field_name][$element['#language']][$delta]['rgb'])) {
  289. // $values = $form_state['values'][$field_name][$element['#language']][$delta]['rgb'];
  290. // foreach (array('r', 'g', 'b') as $colorfield) {
  291. // $colorfield_value = hexdec($values[$colorfield]);
  292. // // If they left any empty, we'll set the value empty and quit.
  293. // if (strlen($values[$colorfield]) == 0) {
  294. // form_set_value($element, '', $form_state);
  295. // return;
  296. // }
  297. // // If they gave us anything that's not hex, reject it.
  298. // if ((strlen($values[$colorfield]) != 2) || $colorfield_value < 0 || $colorfield_value > 255) {
  299. // form_error($element[$colorfield], t("Saturation value must be a 2-digit hexadecimal value between 00 and ff."));
  300. // }
  301. // }
  302. //
  303. // $value = sprintf('#%02s%02s%02s', $values['r'], $values['g'], $values['b']);
  304. // form_set_value($element, $value, $form_state);
  305. // }
  306. }
  307. /**
  308. * Implements hook_field_widget_error().
  309. *
  310. * hook_field_widget_error() lets us figure out what to do with errors
  311. * we might have generated in hook_field_validate(). Generally, we'll just
  312. * call form_error().
  313. *
  314. * @see field_example_field_validate()
  315. * @see form_error()
  316. */
  317. function materio_showroom_field_widget_error($element, $error, $form, &$form_state) {
  318. switch ($error['error']) {
  319. case 'materio_showroom_invalid':
  320. form_error($element, $error['message']);
  321. break;
  322. }
  323. }