Преглед на файлове

first draft of custom materio showroom location, recording values DOESN'T WORK

Bachir Soussi Chiadmi преди 7 години
родител
ревизия
597e6c9159
променени са 2 файла, в които са добавени 355 реда и са изтрити 42 реда
  1. 36 0
      sites/all/modules/gui/materiobasemod/materio_showroom.install
  2. 319 42
      sites/all/modules/gui/materiobasemod/materio_showroom.module

+ 36 - 0
sites/all/modules/gui/materiobasemod/materio_showroom.install

@@ -0,0 +1,36 @@
+<?php
+/**
+ * @file
+ * Install, update, and uninstall functions for the field_example module.
+ */
+
+/**
+ * Implements hook_field_schema().
+ *
+ * Defines the database schema of the field, using the format used by the
+ * Schema API.
+ *
+ * The data we will store here is just one 7-character element, even
+ * though the widget presents the three portions separately.
+ *
+ * All implementations of hook_field_schema() must be in the module's
+ * .install file.
+ *
+ * @see http://drupal.org/node/146939
+ * @see schemaapi
+ * @see hook_field_schema()
+ * @ingroup field_example
+ */
+function materio_showroom_field_schema($field) {
+  $columns = array(
+    'showroom_tid' => array('type' => 'int', 'not null' => FALSE),
+    'location' => array('type' => 'varchar', 'length' => 10, 'not null' => TRUE),
+  );
+  $indexes = array(
+    'location' => array('location'),
+  );
+  return array(
+    'columns' => $columns,
+    'indexes' => $indexes,
+  );
+}

+ 319 - 42
sites/all/modules/gui/materiobasemod/materio_showroom.module

@@ -1,28 +1,5 @@
 <?php
 
-
-/**
- * Implements hook_init().
- */
-// function materio_showroom_init() {
-//   drupal_add_js(drupal_get_path('module', 'materio_showroom').'/js/materio_showroom.js');
-// }
-
-
-/**
- * Implements hook_permission().
- */
-// function materio_showroom_permission() {
-//   $perms =  array(
-//     'add showroom localisation' =>  array(
-//       'title' => t('add showroom localisation'),
-//       'description' => t('add showroom localisation'),
-//     ),
-//   );
-//
-//   return $perms;
-// }
-
 /**
  * Acts on a field collection item being inserted or updated.
  *
@@ -46,13 +23,14 @@ function materio_showroom_entity_presave($entity, $type) {
       $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)){
-          $entity->field_showroom_localisation_show[LANGUAGE_NONE] = [];
-        }
-        if(empty($entity->field_showroom_localisation_show[LANGUAGE_NONE])){
-          $entity->field_showroom_localisation_show[LANGUAGE_NONE][] = [];
-        }
+        // 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'])){
@@ -68,19 +46,318 @@ function materio_showroom_entity_presave($entity, $type) {
   }
 }
 
+// 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
+
+//     __                     __  _                _______      __    __
+//    / /   ____  _________ _/ /_(_)___  ____     / ____(_)__  / /___/ /
+//   / /   / __ \/ ___/ __ `/ __/ / __ \/ __ \   / /_  / / _ \/ / __  /
+//  / /___/ /_/ / /__/ /_/ / /_/ / /_/ / / / /  / __/ / /  __/ / /_/ /
+// /_____/\____/\___/\__,_/\__/_/\____/_/ /_/  /_/   /_/\___/_/\__,_/
 
+// TODO: create own location field
 /**
- * Implements hook_form_alter().
+ * Implements hook_field_info().
+ *
+ * Provides the description of the field.
  */
-// function materio_showroom_form_alter(&$form, &$form_state, $form_id) {
-//   // dsm($form_id);
-//   if( $form_id == "materiau_node_form" ){
-//     dsm($form);
-//     // $form['account']['pass']['#type'] = 'password';
-//     // $form['account']['pass']['#title'] = t('Password');
-//     //
-//     // $form['actions']['#type'] = "container";
-//     // $form['actions']['submit']['#value'] = t('Join');
-//   }
-//
-// }
+function materio_showroom_field_info() {
+  return array(
+    // We name our field as the associative name of the array.
+    'field_materio_showroom_location' => array(
+      'label' => t('Showroom Location'),
+      'description' => t('Define material location by showroom'),
+      'default_widget' => 'materio_showroom_location_text',
+      'default_formatter' => 'materio_showroom_location_simple_text',
+    ),
+  );
+
+  // TODO: define in settings wich vocabulary is for showrooms
+
+}
+
+/**
+ * Implements hook_field_validate().
+ *
+ * This hook gives us a chance to validate content that's in our
+ * field. We're really only interested in the $items parameter, since
+ * it holds arrays representing content in the field we've defined.
+ * We want to verify that the items only contain RGB hex values like
+ * this: #RRGGBB. If the item validates, we do nothing. If it doesn't
+ * validate, we add our own error notification to the $errors parameter.
+ *
+ * @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.'),
+    //     );
+    //   }
+    // }
+  }
+}
+
+/**
+ * Implements hook_field_is_empty().
+ *
+ * hook_field_is_empty() is where Drupal asks us if this field is empty.
+ * Return TRUE if it does not contain data, FALSE if it does. This lets
+ * the form API flag an error when required fields are empty.
+ */
+function materio_showroom_field_is_empty($item, $field) {
+  return empty($item['location']);
+}
+
+//     ______                           __
+//    / ____/___  _________ ___  ____ _/ /____  __________
+//   / /_  / __ \/ ___/ __ `__ \/ __ `/ __/ _ \/ ___/ ___/
+//  / __/ / /_/ / /  / / / / / / /_/ / /_/  __/ /  (__  )
+// /_/    \____/_/  /_/ /_/ /_/\__,_/\__/\___/_/  /____/
+
+/**
+ * Implements hook_field_formatter_info().
+ *
+ * We need to tell Drupal that we have two different types of formatters
+ * for this field. One will change the text color, and the other will
+ * change the background color.
+ *
+ * @see field_example_field_formatter_view()
+ */
+function materio_showroom_field_formatter_info() {
+  return array(
+    // This formatter just displays the hex value in the color indicated.
+    'materio_showroom_location_simple_text' => array(
+      'label' => t('Simple text-based formatter'),
+      'field types' => array('field_example_rgb'),
+    ),
+    // This formatter changes the background color of the content region.
+    // 'field_example_color_background' => array(
+    //   'label' => t('Change the background of the output text'),
+    //   'field types' => array('field_example_rgb'),
+    // ),
+  );
+}
+
+
+/**
+ * Implements hook_field_formatter_view().
+ *
+ * Two formatters are implemented.
+ * - field_example_simple_text just outputs markup indicating the color that
+ *   was entered and uses an inline style to set the text color to that value.
+ * - field_example_color_background does the same but also changes the
+ *   background color of div.region-content.
+ *
+ * @see field_example_field_formatter_info()
+ */
+function materio_showroom_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
+  $element = array();
+
+  switch ($display['type']) {
+    // This formatter simply outputs the field as text and with a color.
+    case 'materio_showroom_location_simple_text':
+      foreach ($items as $delta => $item) {
+        $element[$delta] = array(
+          // We create a render array to produce the desired markup,
+          // "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
+          // 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;
+}
+
+//  _       ___     __           __
+// | |     / (_)___/ /___ ____  / /______
+// | | /| / / / __  / __ `/ _ \/ __/ ___/
+// | |/ |/ / / /_/ / /_/ /  __/ /_(__  )
+// |__/|__/_/\__,_/\__, /\___/\__/____/
+//                /____/
+
+
+/**
+ * Implements hook_field_widget_info().
+ *
+ * Three widgets are provided.
+ * - A simple text-only widget where the user enters the '#ffffff'.
+ * - A 3-textfield widget that gathers the red, green, and blue values
+ *   separately.
+ * - A farbtastic colorpicker widget that chooses the value graphically.
+ *
+ * These widget types will eventually show up in hook_field_widget_form,
+ * where we will have to flesh them out.
+ *
+ * @see field_example_field_widget_form()
+ */
+function materio_showroom_field_widget_info() {
+  return array(
+    'materio_showroom_location_text' => array(
+      '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'),
+    // ),
+  );
+}
+
+
+/**
+ * Implements hook_field_widget_form().
+ *
+ * hook_widget_form() is where Drupal tells us to create form elements for
+ * our field's widget.
+ *
+ * We provide one of three different forms, depending on the widget type of
+ * the Form API item provided.
+ *
+ * The 'field_example_colorpicker' and 'field_example_text' are essentially
+ * the same, but field_example_colorpicker adds a javascript colorpicker
+ * helper.
+ *
+ * field_example_3text displays three text fields, one each for red, green,
+ * and blue. However, the field type defines a single text column,
+ * rgb, which needs an HTML color spec. Define an element validate
+ * handler that converts our r, g, and b fields into a simulated single
+ * 'rgb' form element.
+ */
+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);
+
+  $widget = $element;
+  $widget['#delta'] = $delta;
+
+
+  switch ($instance['widget']['type']) {
+ // TODO: loop through showrooms and don't allow more than one field by show room
+
+    case 'materio_showroom_location_text':
+
+      $widget['showroom_tid'] = array(
+        '#type' => 'hidden',
+        '#default_value' => $showroom_tid,
+        // Allow a slightly larger size that the field length to allow for some
+        // configurations where all characters won't fit in input field.
+        '#size' => 10,
+        '#maxlength' => 10,
+      );
+
+      $widget['location'] = array(
+        '#type' => 'textfield',
+        '#title' => $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.
+        '#size' => 10,
+        '#maxlength' => 10,
+      );
+
+      break;
+
+  }
+
+  $element['location'] = $widget;
+  return $element;
+}
+
+
+/**
+ * Validate the individual fields and then convert to RGB string.
+ */
+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."));
+  //     }
+  //   }
+  //
+  //   $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()
+ */
+function materio_showroom_field_widget_error($element, $error, $form, &$form_state) {
+  switch ($error['error']) {
+    case 'materio_showroom_invalid':
+      form_error($element, $error['message']);
+      break;
+  }
+}