Implements SelectionInterface::settingsForm().
Overrides SelectionInterface::settingsForm
public static function settingsForm(&$field, &$instance) {
$entity_info = entity_get_info($field['settings']['target_type']);
$bundles = entity_get_bundles($field['settings']['target_type']);
// Merge-in default values.
if (!isset($instance['settings']['handler_settings'])) {
$instance['settings']['handler_settings'] = array();
}
$instance['settings']['handler_settings'] += array(
'target_bundles' => array(),
'sort' => array(
'field' => '_none',
),
'auto_create' => FALSE,
);
if (!empty($entity_info['entity_keys']['bundle'])) {
$bundle_options = array();
foreach ($bundles as $bundle_name => $bundle_info) {
$bundle_options[$bundle_name] = $bundle_info['label'];
}
$target_bundles_title = t('Bundles');
// Default core entity types with sensible labels.
if ($field['settings']['target_type'] == 'node') {
$target_bundles_title = t('Content types');
}
elseif ($field['settings']['target_type'] == 'taxonomy_term') {
$target_bundles_title = t('Vocabularies');
}
$form['target_bundles'] = array(
'#type' => 'checkboxes',
'#title' => $target_bundles_title,
'#options' => $bundle_options,
'#default_value' => !empty($instance['settings']['handler_settings']['target_bundles']) ? $instance['settings']['handler_settings']['target_bundles'] : array(),
'#required' => TRUE,
'#size' => 6,
'#multiple' => TRUE,
'#element_validate' => array(
'_entity_reference_element_validate_filter',
),
);
}
else {
$form['target_bundles'] = array(
'#type' => 'value',
'#value' => array(),
);
}
// @todo Use Entity::getPropertyDefinitions() when all entity types are
// converted to the new Field API.
$fields = drupal_map_assoc(drupal_schema_fields_sql($entity_info['base_table']));
foreach (field_info_instances($field['settings']['target_type']) as $bundle_instances) {
foreach ($bundle_instances as $instance_name => $instance_info) {
$field_info = field_info_field($instance_name);
foreach ($field_info['columns'] as $column_name => $column_info) {
$fields[$instance_name . '.' . $column_name] = t('@label (@column)', array(
'@label' => $instance_info['label'],
'@column' => $column_name,
));
}
}
}
$form['sort']['field'] = array(
'#type' => 'select',
'#title' => t('Sort by'),
'#options' => array(
'_none' => t('- None -'),
) + $fields,
'#ajax' => TRUE,
'#limit_validation_errors' => array(),
'#default_value' => $instance['settings']['handler_settings']['sort']['field'],
);
$form['sort']['settings'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'entity_reference-settings',
),
),
'#process' => array(
'_entity_reference_form_process_merge_parent',
),
);
if ($instance['settings']['handler_settings']['sort']['field'] != '_none') {
// Merge-in default values.
$instance['settings']['handler_settings']['sort'] += array(
'direction' => 'ASC',
);
$form['sort']['settings']['direction'] = array(
'#type' => 'select',
'#title' => t('Sort direction'),
'#required' => TRUE,
'#options' => array(
'ASC' => t('Ascending'),
'DESC' => t('Descending'),
),
'#default_value' => $instance['settings']['handler_settings']['sort']['direction'],
);
}
return $form;
}