/**
  * Generate a settings form for this handler.
  */
 public function settingsForm($field, $instance)
 {
     $field_name = $field['field_name'];
     $form['action'] = array('#type' => 'select', '#title' => t('Action'), '#options' => array('none' => t('Do nothing'), 'hide' => t('Hide field'), 'disable' => t('Disable field')), '#description' => t('Action to take when prepopulating field with values via URL.'));
     $form['action_on_edit'] = array('#type' => 'checkbox', '#title' => t('Apply action on edit'), '#description' => t('Apply action when editing an existing entity.'), '#states' => array('invisible' => array(':input[name="instance[settings][behaviors][prepopulate][action]"]' => array('value' => 'none'))));
     $form['fallback'] = array('#type' => 'select', '#title' => t('Fallback behaviour'), '#description' => t('Determine what should happen if no values are provided via URL.'), '#options' => array('none' => t('Do nothing'), 'hide' => t('Hide field'), 'form_error' => t('Set form error'), 'redirect' => t('Redirect')));
     // Get list of permissions.
     $perms = array();
     $perms[0] = t('- None -');
     foreach (module_list(FALSE, FALSE, TRUE) as $module) {
         // By keeping them keyed by module we can use optgroups with the
         // 'select' type.
         if ($permissions = module_invoke($module, 'permission')) {
             foreach ($permissions as $id => $permission) {
                 $perms[$module][$id] = strip_tags($permission['title']);
             }
         }
     }
     $form['skip_perm'] = array('#type' => 'select', '#title' => t('Skip access permission'), '#description' => t('Set a permission that will not be affected by the fallback behavior.'), '#options' => $perms);
     $description = t('Determine if values that should be prepopulated should "listen" to the OG-context.');
     if ($disabled = !module_exists('og_context') || !og_is_group_audience_field($field_name)) {
         $description .= '<br / >' . t('Organic groups integration: Enable OG-context and set "Entity selection mode" to "Organic groups" to enable this selection.');
     }
     $form['og_context'] = array('#type' => 'checkbox', '#title' => t('OG context'), '#description' => $description, '#disabled' => $disabled);
     return $form;
 }
/**
 * Alter target id to fetch referenced values from.
 *
 * @param int &$target_id
 *   Current target id as determined by
 *   entityreference_autofill_field_attach_form().
 * @param array &$form_state
 *   The current $form_state array.
 * @param array $context
 *   An associative array containing the following key-value pairs:
 *   - field_name: The name of the reference field.
 *   - field: Triggering field (The entity reference field) info array.
 *     - field: Field info.
 *     - instance: Instance info.
 *   - form: Current form array.
 *   - langcode: The current langcode.
 */
function hook_entityreference_autofill_target_id_alter(&$target_id, &$form_state, $context)
{
    // Fetch value from form input array instead of values.
    if (og_is_group_audience_field($context['field_name'])) {
        $reference_field_parents = $form_state['triggering_element']['#parents'];
        $referenced_target_id = drupal_array_get_nested_value($form_state['input'], $reference_field_parents);
    }
}
 /**
  * Implements EntityReference_BehaviorHandler_Abstract::views_data_alter().
  */
 public function views_data_alter(&$data, $field)
 {
     // We need to override the default EntityReference table settings when OG
     // behavior is being used.
     if (og_is_group_audience_field($field['field_name'])) {
         $entity_types = array_keys($field['bundles']);
         // We need to join the base table for the entities
         // that this field is attached to.
         foreach ($entity_types as $entity_type) {
             $entity_info = entity_get_info($entity_type);
             $data['og_membership'] = array('table' => array('join' => array($entity_info['base table'] => array('left_field' => $entity_info['entity keys']['id'], 'field' => 'etid', 'extra' => array(0 => array('field' => 'entity_type', 'value' => $entity_type))))), $field['field_name'] => $data['field_data_' . $field['field_name']][$field['field_name']], $field['field_name'] . '_target_id' => $data['field_data_' . $field['field_name']][$field['field_name'] . '_target_id']);
             // Change config with settings from og_membership table.
             foreach (array('filter', 'argument', 'sort', 'relationship') as $op) {
                 $data['og_membership'][$field['field_name'] . '_target_id'][$op]['field'] = 'gid';
                 $data['og_membership'][$field['field_name'] . '_target_id'][$op]['table'] = 'og_membership';
                 unset($data['og_membership'][$field['field_name'] . '_target_id'][$op]['additional fields']);
             }
             // Add gid as the relationship field.
             $data['og_membership'][$field['field_name'] . '_target_id']['relationship']['field'] = 'gid';
         }
         // Get rid of the original table configs.
         unset($data['field_data_' . $field['field_name']]);
         unset($data['field_revision_' . $field['field_name']]);
     }
 }