You are here

function entityqueue_field_widget_form in Entityqueue 7

Implements hook_field_widget_form().

File

./entityqueue.module, line 937
Allows users to collect entities in arbitrarily ordered lists.

Code

function entityqueue_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($instance['widget']['type'] == 'entityqueue_dragtable') {
    $subform = array();
    $entity_type = $element['#entity_type'];
    $field_name = $element['#field_name'];
    $target_type = $field['settings']['target_type'];

    // The referenced entity_type
    $entity = isset($element['#entity']) ? $element['#entity'] : FALSE;

    // Abort early if we don't have a reference to the parent entity.
    if (!$entity) {
      return $element;
    }
    list($entity_id) = entity_extract_ids($entity_type, $entity);
    $value_key = key($field['columns']);
    $subform['#value_key'] = $value_key;
    $subform['#add_position'] = $instance['widget']['settings']['add_position'];

    // We don't use drupal_html_id() here because our ajax callback needs
    // to be able to know what the table id is. We should never have
    // the same form field multiple times on a page anyway.
    $table_id = drupal_clean_css_identifier('entityqueue-dragtable-' . $field_name);
    $table_classes = array(
      'entityqueue-dragtable',
      'entityqueue-dragtable-field-' . $field_name,
      'entityqueue-dragtable-entity-type-' . $entity_type,
    );
    drupal_add_tabledrag($table_id, 'order', 'sibling', 'item-weight');
    $subform['items'] = array(
      '#theme' => 'entityqueue_dragtable',
      '#attributes' => array(
        'id' => $table_id,
        'class' => $table_classes,
      ),
      '#attached' => array(
        'js' => array(
          drupal_get_path('module', 'entityqueue') . '/js/entityqueue.widget.js' => array(
            'type' => 'file',
          ),
        ),
      ),
    );
    $rows = array();
    $values = isset($form_state['values'][$field_name][$langcode]) ? $form_state['values'][$field_name][$langcode] : $items;
    if (!empty($values)) {
      $entity_ids = array();
      foreach ($values as $key => $item) {
        $entity_ids[] = $item[$value_key];
      }
      $entities = entity_load($target_type, $entity_ids);
    }
    $count = count($values);

    // When ajax element is clicked, don't lose the destination.
    if (current_path() == 'system/ajax') {
      if (isset($form_state['destination'])) {
        $destination = $form_state['destination'];
      }
    }
    else {
      $destination = drupal_get_destination();
      $form_state['destination'] = $destination;
    }
    $weight = 0;

    // Keeps track of existing items index
    foreach ($values as $key => $item) {
      $target_id = $item[$value_key];
      $actions = array();
      if (!isset($entities[$target_id])) {

        // Skip entities that no longer exist.
        continue;
      }
      $uri = entity_uri($target_type, $entities[$target_id]);
      if (entity_access('view', $target_type, $entities[$target_id])) {
        $label = entity_label($target_type, $entities[$target_id]);
        $label = l($label, $uri['path']);
      }
      else {
        $label = t('- Restricted access -');
      }
      $actions['remove'] = array(
        '#type' => 'button',
        '#value' => t('Remove'),
        '#name' => $field_name . '_remove_' . $weight,
        '#validate' => array(),
        '#ajax' => array(
          'callback' => 'entityqueue_field_widget_ajax_callback',
          'wrapper' => $table_id,
        ),
      );
      $entity_actions = array();
      if (entity_access('edit', $target_type, $entities[$target_id])) {
        $entity_actions[] = array(
          'title' => t('edit'),
          'href' => drupal_get_normal_path($uri['path']) . '/edit',
          'query' => drupal_get_destination(),
        );
      }
      if (entity_access('delete', $target_type, $entities[$target_id])) {
        $entity_actions[] = array(
          'title' => t('delete'),
          'href' => drupal_get_normal_path($uri['path']) . '/delete',
          'query' => drupal_get_destination(),
        );
      }
      $subform['items'][$weight] = array(
        'label' => array(
          '#type' => 'markup',
          '#markup' => $label,
        ),
        'entity_actions' => array(
          '#type' => 'actions',
          'separator' => array(
            '#type' => 'markup',
            '#markup' => '—',
          ),
          'actions' => array(
            '#type' => 'markup',
            '#markup' => theme('links__ctools_dropbutton', array(
              'title' => t('Actions'),
              'links' => $entity_actions,
            )),
          ),
          '#access' => count($entity_actions) ? 1 : 0,
        ),
        $value_key => array(
          '#type' => 'value',
          '#value' => $target_id,
        ),
        'actions' => array(
          '#type' => 'container',
          $actions,
        ),
        'weight' => array(
          '#type' => 'weight',
          '#delta' => $count,
          '#default_value' => $weight,
          '#title' => '',
          '#attributes' => array(
            'class' => array(
              'item-weight',
            ),
          ),
        ),
      );
      $weight++;
    }

    // This is stolen from entityreference_field_widget_form() and trimmed down
    // for our purposes.
    $autocomplete_path = 'entityreference/autocomplete/single/';
    $autocomplete_path .= $field_name . '/' . $entity_type . '/' . $instance['bundle'] . '/';
    $id = 'NULL';
    if ($entity_id) {
      $id = $entity_id;
    }
    $autocomplete_path .= $id;
    $subform['add'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'container-inline',
        ),
      ),
      '#weight' => $subform['#add_position'] === 'top' ? -1 : 10,
      'entity' => array(
        '#type' => 'textfield',
        '#maxlength' => 1024,
        '#default_value' => '',
        '#autocomplete_path' => $autocomplete_path,
        '#size' => $instance['widget']['settings']['size'],
        '#entity_type' => $element['#entity_type'],
        '#bundle' => $element['#bundle'],
        '#field_name' => $element['#field_name'],
        '#element_validate' => array(
          '_entityreference_autocomplete_validate',
        ),
        '#attributes' => array(
          'class' => array(
            $table_id . '-add',
          ),
        ),
      ),
      'add' => array(
        '#type' => 'submit',
        '#value' => t('Add item'),
        '#ajax' => array(
          'callback' => 'entityqueue_field_widget_ajax_callback',
          'wrapper' => $table_id,
        ),
      ),
    );
    if (!empty($instance['description'])) {
      $subform['description'] = array(
        '#markup' => field_filter_xss($instance['description']),
        '#prefix' => '<div class="description">',
        '#suffix' => '</div>',
        '#weight' => 11,
      );
    }
    $subform['#element_validate'] = array(
      'entityqueue_widget_dragtable_element_validate',
    );
    return $subform;
  }
}