You are here

function party_log_field_attach_create_bundle in Party 7

Implements hook_field_attach_create_bundle().

Add a party reference field to every bundle that is made part of the party log message_type_category.

File

modules/party_log/party_log.module, line 140
Provide a framework for logging things that happen to and with parties.

Code

function party_log_field_attach_create_bundle($entity_type, $bundle) {
  if ($entity_type != 'message') {
    return;
  }
  $message_type = entity_load_single('message_type', $bundle);
  if ($message_type->category != 'party_log') {
    return;
  }

  // Add a party field.
  if (!field_info_field('party')) {
    $field = array(
      'field_name' => 'party',
      'type' => 'entityreference',
      'cardinality' => 1,
      'settings' => array(
        'target_type' => 'party',
        'handler' => 'base',
        'handler_settings' => array(
          'sort' => array(
            'type' => 'none',
          ),
        ),
      ),
    );
    field_create_field($field);
  }
  if (!field_info_instance('message', 'party', $bundle)) {
    $instance = array(
      'field_name' => 'party',
      'entity_type' => 'message',
      'bundle' => $bundle,
      'label' => t('Party'),
      'required' => TRUE,
      'settings' => array(
        'default_value' => '',
      ),
      'widget' => array(
        'type' => 'entityreference_autocomplete',
        'settings' => array(
          'size' => '60',
          'match_operator' => 'CONTAINS',
        ),
      ),
    );
    field_create_instance($instance);
  }

  // If these are related to attaching or detaching entities add a reference
  // field to the appropriate entity type.
  if (in_array(substr($bundle, 0, 21), array(
    'party_data_set_detach',
    'party_data_set_attach',
  ))) {
    $data_set_name = substr($bundle, 22);
    $info = party_get_data_set_info($data_set_name);
    $entity_type = $info['entity type'];
    $field_name = 'party_log_' . $entity_type;

    // Attached entity reference field.
    if (!field_info_field($field_name)) {
      $field = array(
        'field_name' => $field_name,
        'type' => 'entityreference',
        'cardinality' => 1,
        'settings' => array(
          'target_type' => $entity_type,
          'handler' => 'base',
          'handler_settings' => array(
            'sort' => array(
              'type' => 'none',
            ),
          ),
        ),
      );
      field_create_field($field);
    }
    if (!field_info_instance('message', $field_name, $bundle)) {
      $instance = array(
        'field_name' => $field_name,
        'entity_type' => 'message',
        'bundle' => $bundle,
        'label' => $info['label'],
        'required' => TRUE,
        'settings' => array(
          'default_value' => '',
        ),
        'widget' => array(
          'type' => 'entityreference_autocomplete',
          'settings' => array(
            'size' => '60',
            'match_operator' => 'CONTAINS',
          ),
        ),
      );
      field_create_instance($instance);
    }
  }
}