You are here

function commerce_pricelist_draggable_form in Commerce Pricelist 7

Build the draggable form containing all the pricelists.

Return value

array A form array set for theming by theme_commerce_pricelist_draggable_form()

1 string reference to 'commerce_pricelist_draggable_form'
commerce_pricelist_menu in ./commerce_pricelist.module
Implements hook_menu().

File

includes/commerce_pricelist.admin.inc, line 181
Summary

Code

function commerce_pricelist_draggable_form($form_state) {
  $list_count = 0;
  $content = array();

  // Load all of our entities ordered by weight.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'commerce_pricelist_list')
    ->propertyOrderBy('weight');
  $result = $query
    ->execute();
  if (!empty($result['commerce_pricelist_list'])) {
    $list_count = count($result['commerce_pricelist_list']);
    $per_page = 50;

    // Initialize the pager
    $current_page = pager_default_initialize($list_count, $per_page);

    // Split your list into page sized chunks
    $chunks = array_chunk($result['commerce_pricelist_list'], $per_page, TRUE);

    // Show the appropriate items from the list
    $entities = commerce_pricelist_list_load_multiple(array_keys($chunks[$current_page]));
  }
  if (!empty($entities)) {

    // Identify that the elements in 'pricelists' are a collection, to
    // prevent Form API from flattening the array when submitted.
    $form['pricelists']['#tree'] = TRUE;

    // Iterate through each database result.
    foreach ($entities as $item) {
      $query = new EntityFieldQuery();
      $count = $query
        ->entityCondition('entity_type', 'commerce_pricelist_item')
        ->propertyCondition('pricelist_id', $item->list_id)
        ->count()
        ->execute();

      // Let modules implementing commerce_pricelists_list_info_alter()
      // modify list item.
      $info = array();
      drupal_alter('commerce_pricelists_list_info', $info, $item);

      // Create a form entry for this item.
      //
      // Each entry will be an array using the the unique id for that item as
      // the array key, and an array of table row data as the value.
      $form['pricelists'][$item->list_id] = array(
        // We'll use a form element of type '#markup' to display the item name.
        'title' => array(
          '#markup' => check_plain($item->title),
        ),
        'status' => array(
          '#type' => 'checkbox',
          '#title' => t('Active'),
          '#default_value' => $item->status,
        ),
        // The 'weight' field will be manipulated as we move the items around in
        // the table using the tabledrag activity.  We use the 'weight' element
        // defined in Drupal's Form API.
        'weight' => array(
          '#type' => 'weight',
          '#title' => t('Weight'),
          '#default_value' => $item->weight,
          '#delta' => 10,
          '#title_display' => 'invisible',
        ),
        'data' => array(
          '#markup' => implode(' ', $info),
        ),
        'rows' => array(
          '#markup' => $count,
        ),
        'view' => array(
          '#markup' => l(t('View'), 'admin/commerce/pricelist/commerce_pricelist_list/' . $item->list_id),
        ),
        'add' => array(
          '#markup' => l(t('Add price'), 'admin/commerce/pricelist/commerce_pricelist_list/' . $item->list_id . '/add', array(
            'query' => array(
              'destination' => current_path(),
            ),
          )),
        ),
        'edit' => array(
          '#markup' => l(t('Edit'), 'admin/commerce/pricelist/commerce_pricelist_list/' . $item->list_id . '/edit'),
        ),
      );
    }

    // Now we add our submit button, for submitting the form results.
    //
    // The 'actions' wrapper used here isn't strictly necessary for tabledrag,
    // but is included as a Form API recommended practice.
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save Changes'),
    );
    $form['pager']['#markup'] = theme('pager', array(
      'quantity',
      $list_count,
    ));
    return $form;
  }
  else {

    // There were no entities. Tell the user.
    $content[] = array(
      '#type' => 'item',
      '#markup' => t('No price lists currently exist.'),
    );
  }
  return $content;
}