function _entityconnect_field_attach_form in Entity connect 7
Same name and namespace in other branches
- 7.2 includes/entityconnect.form.inc \_entityconnect_field_attach_form()
Implements hook_field_attach_form().
Here we attach a "Add" et "Edit" submit button for each allowed entityreference field.
1 call to _entityconnect_field_attach_form()
- entityconnect_field_attach_form in ./
entityconnect.module - Implements hook_field_attach_form().
File
- includes/
entityconnect.form.inc, line 13 - Handles all form alters and submit functions for entityconnect.
Code
function _entityconnect_field_attach_form($entity_type, $entity, &$form, &$form_state, $field_name, $field, $langcode = LANGUAGE_NONE) {
// Set value with root level information.
$language = $form[$field_name]['#language'];
$field_container =& $form[$field_name];
$widget_container =& $field_container[$language];
if (isset($widget_container['#field_parents'])) {
foreach ($widget_container['#field_parents'] as $key1 => $parent) {
if (!isset($parents)) {
$parents = $parent;
}
else {
$parents .= "-" . $parent;
}
}
}
if (!empty($entity_type) && !empty($entity)) {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$instance = field_info_instance($form['#entity_type'], $field_name, $bundle);
}
if (!isset($instance['entityconnect_unload_add'])) {
$instance['entityconnect_unload_add'] = variable_get('entityconnect_unload_add_default', 1);
}
if (!isset($instance['entityconnect_unload_edit'])) {
$instance['entityconnect_unload_edit'] = variable_get('entityconnect_unload_edit_default', 1);
}
$addbuttonallowed = $instance['entityconnect_unload_add'];
$editbuttonallowed = $instance['entityconnect_unload_edit'];
if (!isset($instance['entityconnect_show_add_icon'])) {
$instance['entityconnect_show_add_icon'] = variable_get('entityconnect_show_add_icon_default', 0);
}
if (!isset($instance['entityconnect_show_edit_icon'])) {
$instance['entityconnect_show_edit_icon'] = variable_get('entityconnect_show_edit_icon_default', 0);
}
$add_icon = $instance['entityconnect_show_add_icon'];
$edit_icon = $instance['entityconnect_show_edit_icon'];
$field = field_info_field($field_name);
$acceptable_types = NULL;
// We get bundles referenced.
if ($field['module'] == 'entityreference') {
$entity_reference_info = entityreference_get_selection_handler($field, $instance);
$entity_type = $entity_reference_info->field['settings']['target_type'];
if (isset($entity_reference_info->field['settings']['handler_settings']['target_bundles'])) {
$acceptable_types = $entity_reference_info->field['settings']['handler_settings']['target_bundles'];
}
elseif (isset($entity_reference_info->field['settings']['handler_settings']['view'])) {
$name = $entity_reference_info->field['settings']['handler_settings']['view']['view_name'];
$display = $entity_reference_info->field['settings']['handler_settings']['view']['display_name'];
$views = views_get_view($name);
$views_display = $views->display;
switch ($entity_type) {
case 'taxonomy_term':
if (isset($views_display[$display]->display_options['filters']['machine_name']['table']) && $views_display[$display]->display_options['filters']['machine_name']['table'] == 'taxonomy_vocabulary') {
$acceptable_types = $views_display[$display]->display_options['filters']['machine_name']['value'];
}
elseif (isset($views_display['default']->display_options['filters']['machine_name']['value'])) {
$acceptable_types = $views_display['default']->display_options['filters']['machine_name']['value'];
}
break;
default:
if (isset($views_display[$display]->display_options['filters']['type'])) {
$acceptable_types = $views_display[$display]->display_options['filters']['type']['value'];
}
elseif (isset($views_display['default']->display_options['filters']['type']['value'])) {
$acceptable_types = $views_display['default']->display_options['filters']['type']['value'];
}
// $acceptable_types was already set to NULL before
break;
}
}
}
elseif ($field['module'] == 'node_reference') {
$entity_type = 'node';
$acceptable_types = $field['settings']['referenceable_types'];
}
elseif ($field['module'] == 'user_reference') {
$entity_type = 'user';
$acceptable_types = $field['settings']['referenceable_roles'];
}
// We currently should separate Autocomplete widget from others default widget
// because "Edit" button will not react well on multiple selected items.
// Autocomplete widget has not #type value, so we are testing it
// via $widget_container['#type'].
// We also test if $widget_container['view'] is set to support
// the widget provided by entityreference_view_widget module.
if (isset($widget_container['#type']) || isset($widget_container['view'])) {
isset($parents) ? $parents : ($parents = '');
$extra_class = isset($widget_container['#type']) ? $widget_container['#type'] : 'autocomplete';
$extra_class .= !isset($widget_container['#cardinality']) || $widget_container['#cardinality'] == 1 ? ' single-value' : ' multiple-values';
$extra_class .= isset($widget_container['#multiple']) && $widget_container['#multiple'] == TRUE ? ' multiple-selection' : ' single-selection';
if (($addbuttonallowed == 0 || $editbuttonallowed == 0) && (isset($widget_container['#multiple']) && $widget_container['#multiple'] == TRUE) || $widget_container['#type'] == 'radios' || $widget_container['#type'] == 'checkboxes') {
$field_container['#attributes']['class'][] = 'inline-label';
}
if (user_access('entityconnect add button') && $addbuttonallowed == 0) {
if (isset($add_icon)) {
if ($add_icon == '0') {
$classes = $extra_class . ' add-icon';
}
elseif ($add_icon == '1') {
$classes = $extra_class . ' add-icon add-text';
}
else {
$classes = $extra_class;
}
}
entityconnect_add_form_element_new($field_container, $language, $field_name, $parents, 'all', $classes, $instance, $entity_type, $acceptable_types);
}
if (user_access('entityconnect edit button') && $editbuttonallowed == 0) {
if (isset($edit_icon)) {
if ($edit_icon == '0') {
$classes = $extra_class . ' edit-icon';
}
elseif ($edit_icon == '1') {
$classes = $extra_class . ' edit-icon edit-text';
}
else {
$classes = $extra_class;
}
}
entityconnect_add_form_element_edit($field_container, $language, $field_name, $parents, 'all', $classes, $entity_type);
}
}
else {
// Type is not set, so multiple widgets.
// We have to add some extra js for single value fields so we add
// a class to let the js know.
$extra_class = isset($widget_container['#type']) ? $widget_container['#type'] : 'autocomplete';
$extra_class .= !isset($widget_container['#cardinality']) || $widget_container['#cardinality'] == 1 ? ' single-value' : ' multiple-values';
$extra_class .= isset($widget_container['#multiple']) && $widget_container['#multiple'] == TRUE ? ' multiple-selection' : ' single-selection';
if (($addbuttonallowed == 0 || $editbuttonallowed == 0) && isset($widget_container['#type'])) {
if (isset($widget_container['#multiple']) && $widget_container['#multiple'] == TRUE || $widget_container['#type'] == 'radios' || $widget_container['#type'] == 'checkboxes') {
$field_container['#attributes']['class'][] = 'inline-label';
}
}
foreach ($widget_container as $key => &$widget_element) {
if (is_numeric($key)) {
if (user_access('entityconnect add button') && $addbuttonallowed == 0) {
isset($parents) ? $parents : ($parents = '');
if (isset($add_icon)) {
if ($add_icon == '0') {
$classes = $extra_class . ' add-icon';
}
elseif ($add_icon == '1') {
$classes = $extra_class . ' add-icon add-text';
}
else {
$classes = $extra_class;
}
}
entityconnect_add_form_element_new($widget_element, $language, $field_name, $parents, $key, $classes, $instance, $entity_type, $acceptable_types);
}
if (user_access('entityconnect edit button') && $editbuttonallowed == 0) {
isset($parents) ? $parents : ($parents = '');
if (isset($edit_icon)) {
if ($edit_icon == '0') {
$classes = $extra_class . ' edit-icon';
}
elseif ($edit_icon == '1') {
$classes = $extra_class . ' edit-icon edit-text';
}
else {
$classes = $extra_class;
}
}
entityconnect_add_form_element_edit($widget_element, $language, $field_name, $parents, $key, $classes, $entity_type);
}
}
}
}
}