You are here

function domain_entity_create_field_instance in Domain Access Entity 7

Create a field instance.

1 call to domain_entity_create_field_instance()
domain_entity_types_enable_domain_field in ./domain_entity.module
Helper function to active domain field on bundles of entity types.

File

./domain_entity.module, line 708
Defines field (e.g. domain_entity) for entities, and access query alter.

Code

function domain_entity_create_field_instance($field_name, $field_type, $required, $entity_type, $bundle, $label, $widget, $description = NULL, $weight = 0, $display = array()) {

  // Look for or add the specified field to the requested entity bundle.
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle);
  if (empty($field)) {
    $field = array(
      'field_name' => $field_name,
      'type' => $field_type,
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'entity_types' => array(
        $entity_type,
      ),
      'translatable' => FALSE,
      'locked' => FALSE,
    );
    $field = field_create_field($field);
  }
  if (empty($instance)) {
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'label' => $label,
      'required' => $required,
      'settings' => array(),
      'display' => array(),
      'description' => $description,
      'default_value' => array(
        array(
          'value' => 0,
        ),
      ),
      'widget' => array(
        'type' => $widget,
      ),
      'display' => array(),
    );
    $entity_info = entity_get_info($entity_type);

    // Add default view mode and teaser so display is set.
    $entity_info['view modes'] += array(
      'default' => array(),
      'node_teaser' => array(),
    );
    foreach ($entity_info['view modes'] as $view_mode => $data) {
      $instance['display'][$view_mode] = $display + array(
        'label' => 'hidden',
        'type' => 'hidden',
        'settings' => array(),
        'weight' => $weight,
      );
    }
    field_create_instance($instance);
  }
}