You are here

function references_dialog_process_widget in References dialog 7

Add our references dialog fields to the existing element

1 string reference to 'references_dialog_process_widget'
references_dialog_element_info_alter in ./references_dialog.module
Implements hook_element_info_alter().

File

./references_dialog.module, line 381
This the main module file.

Code

function references_dialog_process_widget(&$element) {
  if (!isset($element['#entity_type'])) {
    return $element;
  }
  $field = field_info_field($element['#field_name']);
  $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  $widget_settings = $instance['widget']['settings'];
  $widget_type = $instance['widget']['type'];
  $widgets = references_dialog_widgets();

  // Bail if we don't have anything to do here.
  if (!in_array($widget_type, array_keys($widgets))) {
    return $element;
  }
  $dialog_widget = references_dialog_widget_load($widget_type);

  // Attach javascript and CSS needed.
  $attached = references_dialog_attached();
  $element['#attached']['js'][] = $attached['js'][0];
  $element['#attached']['js'][] = references_dialog_js_settings($element['#id'], array(
    'format' => $dialog_widget['format'],
  ));
  $element['#attached']['css'][] = $attached['css'][0];
  $element['#attached']['library'][] = $attached['library'][0];
  $dialog_links = array();
  foreach ($dialog_widget['operations'] as $operation => $settings) {
    if (isset($widget_settings['references_dialog_' . $operation]) && $widget_settings['references_dialog_' . $operation]) {
      $links = $settings['function']($element, $widget_settings, $field, $instance);
      foreach ($links as $link) {
        $link['attributes']['class'][] = $operation . '-dialog';
        $dialog_links[] = references_dialog_link($link);
      }
    }
  }
  if (count($dialog_links)) {

    // We add a div directly into the markup here since we really need it in order
    // to make sure the javascript works.
    $element['#suffix'] = reference_dialog_links_themed($element['#id'], $dialog_links);
  }
  return $element;
}