You are here

function _entityreference_migration_references_field_to_entityreference_field_create_instance in Reference to EntityReference Field Migration 7.2

Same name and namespace in other branches
  1. 7 entityreference_migration.module \_entityreference_migration_references_field_to_entityreference_field_create_instance()

Create the bundle instances of entityreference fields.

1 call to _entityreference_migration_references_field_to_entityreference_field_create_instance()
_entityreference_migration_references_field_to_entityreference_field in ./entityreference_migration.module
Migrate a field from references to entityreference.

File

./entityreference_migration.module, line 305

Code

function _entityreference_migration_references_field_to_entityreference_field_create_instance($entityreference_field, $instance) {
  $entityreference_instance = array(
    'label' => $instance['label'],
    'required' => $instance['required'],
    'description' => $instance['description'],
    'default_value' => $instance['default_value'],
    'field_name' => $instance['field_name'],
    'entity_type' => $instance['entity_type'],
    'bundle' => $instance['bundle'],
    'deleted' => $instance['deleted'],
  );

  // Add in the widget and appropriate settings (TODO for more flexibility)
  if (isset($instance['widget'])) {
    $widget_type = 'entityreference_autocomplete';
    $widget_module = 'entityreference';
    if ($instance['widget']['module'] == 'options') {
      $widget_type = $instance['widget']['type'];
      $widget_module = $instance['widget']['module'];
    }
    $entityreference_instance['widget'] = array(
      'weight' => $instance['widget']['weight'],
      'type' => $widget_type,
      'module' => $widget_module,
      'active' => 1,
      'settings' => array(
        'match_operator' => 'CONTAINS',
        'size' => 60,
        'path' => '',
      ),
    );
  }

  // Add in the display and appropriate settings
  if (isset($instance['display'])) {
    foreach ($instance['display'] as $display_name => $instance_display) {
      $entityreference_instance['display'][$display_name] = array(
        'label' => $instance_display['label'],
        'weight' => $instance_display['weight'],
        'module' => 'entityreference',
      );
      if ($instance_display['type'] == 'hidden') {
        $entityreference_instance['display'][$display_name]['type'] = 'hidden';
      }
      else {
        if ($instance_display['type'] == 'node_reference_node' || $instance_display['type'] == 'user_reference_user') {
          $entityreference_instance['display'][$display_name]['type'] = 'entityreference_entity_view';
          $entityreference_instance['display'][$display_name]['settings'] = array(
            'view_mode' => $instance_display['settings'][$instance_display['module'] . '_view_mode'],
          );
        }
        else {
          if ($instance_display['type'] == 'node_reference_default' || $instance_display['type'] == 'user_reference_default') {
            $entityreference_instance['display'][$display_name]['type'] = 'entityreference_label';
            $entityreference_instance['display'][$display_name]['settings'] = array(
              'link' => TRUE,
            );
          }
          else {
            $entityreference_instance['display'][$display_name]['type'] = 'entityreference_label';
            $entityreference_instance['display'][$display_name]['settings'] = array(
              'link' => TRUE,
            );
          }
        }
      }
    }
  }
  $entityreference_instance = field_create_instance($entityreference_instance);
  return $entityreference_instance;
}