You are here

function entityreference_dragdrop_build_html in Entity Reference Drag & Drop 7

Builds HTML for dragable list of items.

1 call to entityreference_dragdrop_build_html()
entityreference_dragdrop_field_widget_form in ./entityreference_dragdrop.module
Implements hook_field_widget_form().

File

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

Code

function entityreference_dragdrop_build_html($options, $field, $instance, $ec, $classes = array()) {
  $view_mode = $instance['widget']['settings']['view_mode'];
  $target_type = $field['settings']['target_type'];
  $list_items = array();
  if ($view_mode != 'title') {
    if (!empty($options)) {
      $entities = entity_load($target_type, array_keys($options));
      $renderable_entities = entity_view($target_type, $entities, $view_mode);
      foreach ($renderable_entities[$target_type] as $id => $entity) {
        if (is_array($entity)) {
          $list_items[$id] = $entity;
        }
        else {
          $list_items[$id] = array(
            '#markup' => $options[$id],
          );
        }
      }
    }
  }
  else {
    foreach ($options as $id => $label) {
      $list_items[$id] = array(
        '#markup' => $label,
      );
    }
  }

  // Build HTML to represent options.
  $html = '<ul data-ec="' . $ec . '" class="entityreference-dragdrop ' . implode(" ", $classes) . '">';
  foreach ($list_items as $id => $item) {
    $html .= '<li data-ec="' . $ec . '" data-id="' . $id . '" data-label="' . $options[$id] . '">' . drupal_render($item) . '</li>';
  }
  $html .= '</ul>';
  return $html;
}