commit 3858cae6a848dba30f6e100fe600a604b1d8e718
Author: lslinnet <larslinnet@gmail.com>
Date:   Tue Feb 10 08:18:57 2015 +0100

    Initial commit

diff --git a/inline_entity_form_view_mode.info b/inline_entity_form_view_mode.info
new file mode 100644
index 0000000..7998d36
--- /dev/null
+++ b/inline_entity_form_view_mode.info
@@ -0,0 +1,4 @@
+name = Inline Entity Form View mode selector
+description = Enables you to add a view mode selector per inline entity form field widget
+package = Fields
+core = 7.x
diff --git a/inline_entity_form_view_mode.module b/inline_entity_form_view_mode.module
new file mode 100644
index 0000000..4640f2f
--- /dev/null
+++ b/inline_entity_form_view_mode.module
@@ -0,0 +1,203 @@
+<?php
+
+/**
+ * @file Module file for the inline entity view mode selector module.
+ */
+
+/**
+ * Implements hook_ctools_plugin_directory().
+ */
+function inline_entity_form_view_mode_ctools_plugin_directory($module, $plugin) {
+  if ($module == 'entityreference') {
+    return 'plugins/' . $plugin;
+  }
+}
+
+/**
+ * Getter function that just returns an object if present on the entity
+ * else returns the default value.
+ *
+ * @param $entity
+ * @param $options
+ * @param $name
+ * @param $entity_type
+ * @param $info
+ * @return string
+ */
+function inline_entity_form_view_mode_get_entity_view_mode($entity, $options, $name, $entity_type, $info) {
+  if (isset($entity->{$name})) {
+    return $entity->{$name};
+  }
+  return 'full';
+}
+
+/**
+ * Implements hook_field_widget_WIDGET_TYPE_form_alter().
+ *
+ * To alter the form of the inline_entity_form widget type.
+ */
+function inline_entity_form_view_mode_field_widget_inline_entity_form_form_alter(&$element, &$form_state, $context) {
+  $field = $context['field'];
+  if (_inline_entity_form_view_mode_supported_field_widget($field)) {
+    $item = isset($context['items'][$context['delta']]) ? $context['items'][$context['delta']] : NULL;
+    $target_type = $field['settings']['target_type'];
+    $view_modes = _inline_entity_form_view_mode_get_view_modes($target_type);
+
+    $view_mode_per_id = array();
+    foreach ($context['items'] as $item) {
+      $items_per_id[$item['target_id']] = $item['view_mode'];
+    }
+
+
+    if ($element['actions']['ief_add'] && $element['actions']['ief_add_existing']) {
+//      $element['entities']['#table_fields']['view_mode'] = array(
+//        'type' => 'property',
+//        'label' => 'View mode',
+//        'weight' => 101,
+//      );
+
+      $children = element_children($element['entities']);
+
+      foreach ($children as $delta) {
+        $entity = $element['entities'][$delta];
+        $ids = entity_extract_ids($element['entities']['#entity_type'], $entity['#entity']);
+
+        // populate the entity with a fake property to allow for ief's handling of fields.
+        $element['entities'][$delta]['#entity']->view_mode = isset($view_mode_per_id[$ids[0]]) ? $view_mode_per_id[$ids[0]] : 'full';
+      }
+    }
+
+    else if (false) {
+      $element['form']['view_mode'] = array(
+        '#type' => 'select',
+        '#title' => 'View Mode',
+        '#options' => $view_modes,
+        '#default_value' => isset($view_mode_per_id[$ids[0]]) ? $view_mode_per_id[$ids[0]] : 'full',
+        '#weight' => 10,
+        '#id' => 'view-mode-' . $delta,
+      );
+    }
+  }
+}
+
+/**
+ * Implements hook_field_widget_form_alter().
+ */
+function inline_entity_form_view_mode_field_widget_form_alter(&$element, &$form_state, $context) {
+  $field = $context['field'];
+  if (_inline_entity_form_view_mode_supported_field_widget($field)) {
+    $item = isset($context['items'][$context['delta']]) ? $context['items'][$context['delta']] : NULL;
+    $target_type = $field['settings']['target_type'];
+    $view_modes = _inline_entity_form_view_mode_get_view_modes($target_type, $context);
+
+    $instance = $context['instance'];
+    if (in_array($instance['widget']['type'], array('entityreference_autocomplete', 'options_select', 'options_buttons'))) {
+      $element['view_mode'] = array(
+        '#type' => 'select',
+        '#title' => 'View Mode',
+        '#options' => $view_modes,
+        '#default_value' => isset($item) ? $item['view_mode'] : 'full',
+        '#weight' => 10,
+        '#id' => 'view-mode-' . $context['delta'],
+      );
+    }
+  }
+}
+
+/**
+ * Fetches view_modes based of the target entity type.
+ *
+ * @param $target_type string - name of an entity type
+ * @return array - list of view modes
+ */
+function _inline_entity_form_view_mode_get_view_modes($target_type) {
+  $entity_info = entity_get_info($target_type);
+  $view_modes = array('full' => 'Default');
+  foreach ($entity_info['view modes'] as $view_key => $view_mode) {
+    $view_modes[$view_key] = $view_mode['label'];
+  }
+  drupal_alter('inline_entity_form_view_mode_get_view_modes', $target_type, $view_modes);
+  return $view_modes;
+}
+
+/**
+ * Check if a field is supported by inline entity form view mode
+ *
+ * @param $field array - definition of a field
+ * @return bool - true if the given field is a supported field.
+ */
+function _inline_entity_form_view_mode_supported_field_widget($field) {
+  return $field['type'] == 'entityreference'
+  && isset($field['settings']['handler_settings']['behaviors']['ief_viewmode_behavior'])
+  && $field['settings']['handler_settings']['behaviors']['ief_viewmode_behavior']['status'] == 1;
+}
+
+
+/**
+ * Perform alterations before an entity form is included in the IEF widget.
+ *
+ * @param $entity_form
+ *   Nested array of form elements that comprise the entity form.
+ * @param $form_state
+ *   The form state of the parent form.
+ */
+function inline_entity_form_view_mode_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
+  $field_name = $entity_form['#parents'][0];
+  $langcode = $entity_form['#entity']->language;
+  $field = $form_state['field'][$field_name][$langcode]['field'];
+  if (_inline_entity_form_view_mode_supported_field_widget($field)) {
+    $view_modes = _inline_entity_form_view_mode_get_view_modes($entity_form['#entity_type']);
+    $default_value = 'full';
+    if ($form_state['triggering_element']['#ief_row_form'] == 'edit') {
+      $delta = $form_state['triggering_element']['#ief_row_delta'];
+      $default_value = $form_state['inline_entity_form'][$entity_form['#ief_id']]['entities'][$delta]['item']['view_mode'];
+    }
+    $entity_form['view_mode'] = array(
+      '#type' => 'select',
+      '#title' => 'View Mode',
+      '#description' => t('Used when rendering the referenced entity, do not belong to the created entity'),
+      '#options' => $view_modes,
+      '#default_value' => $default_value,
+      '#weight' => 99,
+    );
+  }
+}
+
+/**
+ * Perform alterations before the reference form is included in the IEF widget.
+ *
+ * The reference form is used to add existing entities through an autocomplete
+ * field
+ *
+ * @param $reference_form
+ *   Nested array of form elements that comprise the reference form.
+ * @param $form_state
+ *   The form state of the parent form.
+ */
+function inline_entity_form_view_mode_inline_entity_form_reference_form_alter(&$reference_form, &$form_state) {
+  $field_name = $reference_form['#parents'][0];
+  $field = $form_state['field'][$field_name]['und']['field'];
+  if (_inline_entity_form_view_mode_supported_field_widget($field)) {
+    $view_modes = _inline_entity_form_view_mode_get_view_modes($reference_form['#entity_type']);
+    $reference_form['view_mode'] = array(
+      '#type' => 'select',
+      '#title' => 'View Mode',
+      '#options' => $view_modes,
+      '#default_value' => 'full',
+      '#weight' => 10,
+//      '#id' => 'view-mode-' . $context['delta'],
+    );
+  }
+}
+
+/**
+ * Implements hook_inline_entity_form_table_fields_alter().
+ */
+function inline_entity_form_view_mode_inline_entity_form_table_fields_alter(&$fields, $context) {
+  $fields['view_mode'] = array(
+    'type' => 'column',
+    'label' => t('View mode'),
+    'name' => 'view_mode',
+    'weight' => 100,
+  );
+}
diff --git a/plugins/behavior/IEFViewModeBehavior.class.php b/plugins/behavior/IEFViewModeBehavior.class.php
new file mode 100644
index 0000000..0161e7e
--- /dev/null
+++ b/plugins/behavior/IEFViewModeBehavior.class.php
@@ -0,0 +1,48 @@
+<?php
+
+class IEFViewModeBehavior extends EntityReference_BehaviorHandler_Abstract {
+
+  public function schema_alter(&$schema, $field) {
+    $schema['columns']['view_mode'] = array(
+      'description' => 'Target view mode machine name.',
+      'type' => 'varchar',
+      'length' => 32,
+      'default' => 'full',
+      'not null' => FALSE,
+    );
+  }
+
+  /**
+   * We fake the view_mode property, used for when rendering the table of entities
+   * due to the way that IEF either needs a field or property to render something
+   * in the table.
+   */
+  public function property_info_alter(&$info, $entity_type, $field, $instance, $field_type) {
+    $properties = &$info['node']['properties'];
+
+    $properties['view_mode'] = array(
+      'label' => t("Content ID"),
+      'type' => 'text',
+      'description' => t("A flux based view_mode identifier"),
+      'queryable' => FALSE,
+      'computed' => TRUE,
+      'getter callback' => 'inline_entity_form_view_mode_get_entity_view_mode',
+    );
+  }
+
+  /**
+   * Generate a settings form for this handler.
+   */
+  public function settingsForm($field, $instance) {
+    $form['update_schema'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Change schema'),
+      '#default_value' => 1,
+    );
+
+    if ($i == 1) {
+
+    }
+    return $form;
+  }
+}
diff --git a/plugins/behavior/ief_viewmode_behavior.inc b/plugins/behavior/ief_viewmode_behavior.inc
new file mode 100644
index 0000000..58032da
--- /dev/null
+++ b/plugins/behavior/ief_viewmode_behavior.inc
@@ -0,0 +1,12 @@
+<?php
+
+/**
+ * Define a behavior for the viewmode selector class.
+ */
+
+$plugin = array(
+  'title' => t('Inline Entity Form View mode selector'),
+  'class' => 'IEFViewModeBehavior',
+  'weight' => 10,
+  'behavior type' => 'field',
+);
