From fd06eb01bbeff3575f859a151092c44e2558f35e Mon Sep 17 00:00:00 2001
From: Kgaut <Kgaut@686336.no-reply.drupal.org>
Date: Fri, 10 Jul 2015 11:15:22 +0200
Subject: [PATCH] Issue #1883400 by Kgaut, paranojik, Primsi: Status of ECK
 integration


diff --git a/entityconnect.module b/entityconnect.module
index 787e82a..b0b14f5 100644
--- a/entityconnect.module
+++ b/entityconnect.module
@@ -160,7 +160,8 @@ function entityconnect_form_alter(&$form, &$form_state, $form_id) {
           || ((arg(1) == 'add') && ($cid = arg(3)) && $child = TRUE)
           || ((arg(1) == 'people') && (arg(2) == 'create') && ($cid = arg(3)) && $child = TRUE)
           || ((arg(2) == 'taxonomy') && (arg(4) == 'add') && ($cid = arg(5)) && $child = TRUE)
-          || ((arg(2) == 'taxonomy') && (arg(3) == 'add') && ($cid = arg(4)) && $child = TRUE)) {
+          || ((arg(2) == 'taxonomy') && (arg(3) == 'add') && ($cid = arg(4)) && $child = TRUE)
+          || ((arg(2) == 'entity-type') && (arg(5) == 'add') && ($cid = arg(6)) && $child = TRUE)) {
     $cache = entityconnect_cache_get($cid);
     entityconnect_include_form();
     if ($child) {
@@ -241,6 +242,14 @@ function _entityconnect_get_ref_fields() {
           break;
 
         default:
+          // Entity construction kit support.
+          if (module_exists('eck')) {
+            $entity_info = entity_get_info($entity_type);
+            if (!empty($entity_info['module']) && $entity_info['module'] == 'eck') {
+              $ref_fields[$id] = $field;
+            }
+          }
+
           break;
       }
     }
diff --git a/includes/entityconnect.form.inc b/includes/entityconnect.form.inc
index 6bdacf3..f4f6017 100644
--- a/includes/entityconnect.form.inc
+++ b/includes/entityconnect.form.inc
@@ -80,7 +80,7 @@ function _entityconnect_field_attach_form($entity_type, $entity, &$form, &$form_
             $acceptable_types = $views_display['default']->display_options['filters']['machine_name']['value'];
           }
           break;
-        
+
         default:
           break;
       }
@@ -334,7 +334,7 @@ function entityconnect_add_edit_button_submit(&$form, &$form_state) {
     }
     $params[$key] = $value;
   }
-  
+
   $data = array(
     'form'       => $form,
     'form_state' => $form_state,
@@ -394,6 +394,13 @@ function entityconnect_child_form_submit(&$form, &$form_state) {
         break;
 
       default:
+        // Enitity construction kit support.
+        if (module_exists('eck')) {
+          $entity_info = entity_get_info($entity_type);
+          if ($entity_info['module'] == 'eck') {
+            $data['target_id'] = $form_state['values']['entity']->id;
+          }
+        }
         break;
     }
 
@@ -636,6 +643,53 @@ function entityconnect_return_form_alter(&$form, &$form_state, $form_id, $cache_
           break;
 
         default:
+          // Entity construction kit support.
+          if (module_exists('eck')) {
+            $entiy_info = entity_get_info($data['target_entity_type']);
+            if ($entiy_info['module'] = 'eck') {
+              if (($data['target_id'] != NULL || $data['target_id'] != 0) && $eck_entity = entity_load_single($data['target_entity_type'], array($data['target_id']))) {
+                switch ($widget_container_type) {
+                  // Autocomplete tags style.
+                  case 'textfield':
+                    $element['#default_value'] = !empty($eck_entity->title) ? $eck_entity->title : t('Entity has no title');
+                    $element['#default_value'] .= ' (' . $eck_entityerm->id . ')';
+                    if (!empty ($widget_container['#value'])) {
+                      $element['#default_value'] .= ', ' . $widget_container['#value'];
+                    }
+                    break;
+
+                  // Select list.
+                  case 'select':
+                    if ($widget_container['#multiple'] == FALSE) {
+                      $element['#default_value'] = $eck_entity->id;
+                    }
+                    else {
+                      $element['#default_value'] = $eck_entity->id;
+                      $element['#default_value'] = $widget_container['#value'] + array($element['#default_value'] => $element['#default_value']);
+                    }
+                    break;
+
+                  // Radios widget.
+                  case 'radios':
+                    $element['#default_value'] = $eck_entity->id;
+                    break;
+
+                  // Checkboxes widget.
+                  case 'checkboxes':
+                    $element['#default_value'] = $eck_entity->id;
+                    $element['#default_value'] = $form[$data['field']][$language]['#value'] + array($element['#default_value'] => $element['#default_value']);
+                    break;
+
+                  default:
+                    if ($field_info['module'] == 'entityreference') {
+                      $element['#default_value'] = !empty($eck_entity->title) ? $eck_entity->title : t('Entity has no title');
+                      $element['#default_value'] .= ' (' . $eck_entity->id . ')';
+                    }
+                    break;
+                }
+              }
+            }
+          }
           break;
       }
       if (isset($element)) {
@@ -685,6 +739,15 @@ function entityconnect_child_form_alter(&$form, &$form_state, $form_id, $cache_i
       $form['actions']['submit']['#submit'][] = 'taxonomy_form_vocabulary_submit';
       break;
 
+    case 'eck__entity__form_add_' . $form['#entity_type'] . '_' . $form['#bundle']:
+    case 'eck__entity__form_edit_' . $form['#entity_type'] . '_' . $form['#bundle']:
+      $form['actions']['cancel']['#weight'] = 10001;
+      $form['actions']['submit']['#submit'][] = 'eck__entity__form_submit';
+
+      // @todo I think we don't need #submit any more..?
+      $form['#submit'] = array();
+      break;
+
     default:
       break;
   }
diff --git a/includes/entityconnect.menu.inc b/includes/entityconnect.menu.inc
index f832da9..a0f645e 100644
--- a/includes/entityconnect.menu.inc
+++ b/includes/entityconnect.menu.inc
@@ -119,6 +119,44 @@ function entityconnect_add($cache_id) {
       break;
 
     default:
+      // Entity construction kit support.
+      if (module_exists('eck')) {
+        $entity_info = entity_get_info($entity_type);
+
+        if (!empty($entity_info['module']) && $entity_info['module'] == 'eck') {
+          // Only one allowed, no need for the intermediate step.
+          if (count($acceptable_types) === 1) {
+            $bundle_name = array_pop($acceptable_types);
+            $bundle = $entity_info['bundles'][$bundle_name];
+            drupal_goto($bundle['crud']['add']['path'] . "/$cache_id");
+          }
+          // All bundles allowed.
+          elseif (count($acceptable_types) === 0) {
+            foreach ($entity_info['bundles'] as $key => $bundle) {
+              $content[$key]['href'] = $bundle['crud']['add']['path'] . "/$cache_id";
+              $content[$key]['label'] = $bundle['label'];
+            }
+            $output = theme('entityconnect_entity_add_list', array(
+              'cache id' => $cache_id,
+              'items' => $content,
+            ));
+            $output .= l(t('Cancel'), "admin/entityconnect/return/$cache_id");
+          }
+          // Only certain bundles allowed.
+          else {
+            foreach ($acceptable_types as $bundle_name) {
+              $bundle = $entity_info['bundles'][$bundle_name];
+              $content[$bundle_name]['href'] = $bundle['crud']['add']['path'] . "/$cache_id";
+              $content[$bundle_name]['label'] = $bundle['label'];
+            }
+            $output = theme('entityconnect_entity_add_list', array(
+              'cache id' => $cache_id,
+              'items' => $content,
+            ));
+            $output .= l(t('Cancel'), "admin/entityconnect/return/$cache_id");
+          }
+        }
+      }
       break;
   }
   return $output;
@@ -222,6 +260,36 @@ function entityconnect_edit($cache_id) {
       break;
 
     default:
+      // Support for entity construction kit.
+      if (module_exists('eck')) {
+        $entity_info = entity_get_info($entity_type);
+        if ($entity_info['module'] == 'eck') {
+
+          // @todo Do we need this part? Because of entityconnect.form.inc:241
+          // we always get only one target_id. Or am I missing something?
+          if (is_array($target_id)) {
+            $eck_entities = entity_load($entity_type, $target_id);
+            foreach ($eck_entities as $key => $eck_entity) {
+              $crud_edit_path = preg_replace('/\%/', $key, $entity_info['bundles'][$eck_entity->type]['crud']['edit']['path']);
+              $content[$key] = array(
+                'label' => $eck_entity->title,
+                'href' => $crud_edit_path,
+              );
+            }
+            $output = theme('entityconnect_entity_add_list', array(
+                'items' => $content,
+                'cache id' => $cache_id)
+            );
+            $output .= l(t('Cancel'), "admin/entityconnect/return/$cache_id");
+          }
+          else {
+            $eck_entity = entity_load_single($entity_type, $target_id);
+            $crud_edit_path = preg_replace('/\%/', $target_id, $entity_info['bundles'][$eck_entity->type]['crud']['edit']['path']);
+            drupal_goto($crud_edit_path, array(
+              'query' => array("build_cache_id" => $cache_id, "child" => TRUE)));
+          }
+        }
+      }
       break;
   }
   return $output;
-- 
2.1.4

