You are here

function entityreference_dragdrop_field_widget_form in Entity Reference Drag & Drop 7

Implements hook_field_widget_form().

File

./entityreference_dragdrop.module, line 31
Provides a Drag & Drop widget for entity-reference fields.

Code

function entityreference_dragdrop_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // If we have more than one element per page,
  // this element-counter keeps them separated.
  $ec =& drupal_static(__FUNCTION__, 0);
  $handler = entityreference_get_selection_handler($field, $instance);
  $options = $handler
    ->getReferencableEntities();
  $selected = entityreference_dragdrop_get_selected_options($items, $options);
  $available = entityreference_dragdrop_get_available_options($items, $options);
  $markup_available = entityreference_dragdrop_build_html($available, $field, $instance, $ec, array(
    'entityreference-dragdrop-available',
  ));
  $markup_selected = entityreference_dragdrop_build_html($selected, $field, $instance, $ec, array(
    'entityreference-dragdrop-selected',
  ));
  $label = '<div class="entityreference-dragdrop-label">' . $instance['label'] . '</div>';
  $message = $field['cardinality'] == -1 ? '' : '<div class="entityreference-dragdrop-message" data-ec="' . $ec . '">' . t("This field cannot hold more than @card values.", array(
    '@card' => $field['cardinality'],
  )) . '</div>';
  $filter_1 = $filter_2 = array();
  if ($instance['widget']['settings']['display_filter']) {
    $filter_1 = $filter_2 = array(
      '#type' => 'textfield',
      '#attributes' => array(
        'class' => array(
          'entityreference-dragdrop-filter',
        ),
      ),
    );
  }
  $html_1 = '<div class="entityreference-dragdrop-container entityreference-dragdrop-container-available">';
  $html_1 .= '<div class="entityreference-dragdrop-list-header">';
  $html_1 .= $instance['widget']['settings']['available_entities_label'];
  $html_1 .= '</div>';
  $html_1 .= drupal_render($filter_1);
  $html_1 .= $markup_available;
  $html_1 .= '</div>';
  $html_2 = '<div class="entityreference-dragdrop-container entityreference-dragdrop-container-selected">';
  $html_2 .= '<div class="entityreference-dragdrop-list-header">';
  $html_2 .= $instance['widget']['settings']['selected_entities_label'];
  $html_2 .= '</div>';
  $html_2 .= drupal_render($filter_2);
  $html_2 .= $markup_selected;
  $html_2 .= '</div>';
  $element += array(
    '#type' => 'hidden',
    '#default_value' => implode(',', array_keys($selected)),
    '#element_validate' => array(
      'entityreference_dragdrop_element_validate',
    ),
    '#suffix' => $label . '<div class="entityreference-dragdrop-widget-wrapper">' . $html_1 . $html_2 . '</div>' . $message . '<div class="entityreference-dragdrop-clear"></div>',
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'entityreference_dragdrop') . '/entityreference_dragdrop.js',
        array(
          'data' => array(
            'entityreference_dragdrop' => array(
              $ec => $field['cardinality'],
            ),
          ),
          'type' => 'setting',
        ),
      ),
      'css' => array(
        drupal_get_path('module', 'entityreference_dragdrop') . '/entityreference_dragdrop.css',
      ),
      'library' => array(
        array(
          'system',
          'ui.sortable',
        ),
      ),
    ),
    '#attributes' => array(
      'class' => array(
        'entityreference-dragdrop-values',
      ),
      'data-ec' => array(
        $ec,
      ),
    ),
  );

  // Increment the element-counter
  $ec++;
  return $element;
}