You are here

function commerce_pricelist_item_list_entities in Commerce Pricelist 7

Returns a render array with all commerce_pricelist_list entities.

In this basic example we know that there won't be many entities, so we'll just load them all for display. See pager_example.module to implement a pager. Most implementations would probably do this with the contrib Entity API module, or a view using views module, but we avoid using non-core features in the Examples project.

See also

pager_example.module

3 calls to commerce_pricelist_item_list_entities()
commerce_pricelist_form_commerce_product_ui_product_form_alter in ./commerce_pricelist.module
Add price list fieldgroup to product form. Implements hook_form_BASE_FORM_ID_alter().
commerce_pricelist_item_info_page in includes/commerce_pricelist.admin.inc
Basic information for the page.
commerce_pricelist_list_view in includes/commerce_pricelist.admin.inc
Menu callback to display an entity.

File

includes/commerce_pricelist.admin.inc, line 425
Summary

Code

function commerce_pricelist_item_list_entities($pricelist_id = NULL, $sku = FALSE) {
  $content = array();
  $entities = array();
  $item_count = 0;
  if ($pricelist_id) {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'commerce_pricelist_item')
      ->propertyCondition('pricelist_id', $pricelist_id, '=');
    if ($sku) {

      // Filter by SKU.
      $query
        ->propertyCondition('sku', $sku, '=');
    }
    $result = $query
      ->execute();
    if (!empty($result)) {
      $item_count = count($result['commerce_pricelist_item']);
      $per_page = 50;

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

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

      // Show the appropriate items from the list
      $entities = commerce_pricelist_item_load_multiple(array_keys($chunks[$current_page]));
    }
  }
  else {

    // Load all of our entities.
    $entities = commerce_pricelist_item_load_multiple();
  }
  if (!empty($entities)) {
    $rows = array();
    foreach ($entities as $entity) {

      // Create tabular rows for our entities.
      $rows[] = array(
        'data' => array(
          'sku' => check_plain($entity->sku),
          'valid from' => _commerce_pricelist_display_date($entity->valid_from),
          'valid to' => _commerce_pricelist_display_date($entity->valid_to),
          'quantity' => $entity->quantity,
          // @todo Format currency through commerce
          'amount' => $entity->price_amount / 100,
          'currency' => check_plain($entity->currency_code),
          'edit' => l(t('Edit'), 'admin/commerce/pricelist/commerce_pricelist_item/' . $entity->item_id . '/edit', array(
            'query' => array(
              'destination' => current_path(),
            ),
          )),
        ),
      );
    }

    // Put our entities into a themed table. See theme_table() for details.
    $content['entity_table'] = array(
      '#theme' => 'table',
      '#rows' => $rows,
      '#header' => array(
        t('sku'),
        t('Valid from'),
        t('Valid to'),
        t('Quantity'),
        t('Price'),
        t('Currency'),
        t('Operations'),
      ),
    );
  }
  else {

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