You are here

commerce_pricing_attributes_ui.module in Commerce Pricing Attributes 7.2

File

commerce_pricing_attributes_ui.module
View source
<?php

/**
 * Implements hook_entity_info_alter().
 */
function commerce_pricing_attributes_ui_entity_info_alter(&$info) {

  // Foreach product type with enabled attributes.
  foreach (commerce_pricing_attributes_enabled_product_types() as $type => $product_type) {

    // Entity bundle paths.
    $info['commerce_pricing_attibutes']['bundles'][$product_type['type']] = array(
      'label' => $product_type['name'],
      'admin' => array(
        'path' => 'admin/commerce/products/types/' . strtr($type, '_', '-') . '/pricing_attributes',
        // TODO: change the access argument.
        'access arguments' => array(
          'administer content',
        ),
      ),
    );
  }
}

/**
 * Implements hook_menu_alter().
 */
function commerce_pricing_attributes_ui_menu_alter(&$items) {

  // Foreach product type with enabled attributes.
  foreach (commerce_pricing_attributes_enabled_product_types() as $type => $product_type) {
    $bundle = strtr($type, '_', '-');

    // Change the title of the menu items to something more clear.
    $items['admin/commerce/products/types/' . $bundle . '/pricing_attributes/fields']['title'] = t('Manage attributes Fields');
    $items['admin/commerce/products/types/' . $bundle . '/pricing_attributes/fields']['weight'] = 20;
    $items['admin/commerce/products/types/' . $bundle . '/pricing_attributes/display']['title'] = t('Manage attributes display');
    $items['admin/commerce/products/types/' . $bundle . '/pricing_attributes/display']['weight'] = 21;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_pricing_attributes_ui_form_commerce_product_ui_product_type_form_alter(&$form, &$form_state) {
  $product_type = !empty($form_state['product_type']) ? $form_state['product_type']['type'] : '';
  $form['product_type']['commerce_pricing_attributes'] = array(
    '#type' => 'fieldset',
    '#title' => 'Pricing Attibutes',
    '#weight' => 10,
  );

  // TODO: If at least one field is enabled in this product type
  // then disable this option from being changed.
  $form['product_type']['commerce_pricing_attributes']['enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enabled'),
    '#default_value' => variable_get('commerce_pricing_atrributes_' . $product_type . '_enabled', 0),
  );
  $form['actions']['submit']['#submit'][] = 'commerce_pricing_attributes_commerce_product_ui_product_type_form_submit';
}

/**
 * Submit callback for commerce_product_ui_product_type_form.
 *
 * Saves the pricing attributes settings per product type.
 */
function commerce_pricing_attributes_ui_commerce_product_ui_product_type_form_submit($form, &$form_state) {

  // Get product type.
  $product_type = !empty($form_state['product_type']['type']) ? $form_state['product_type']['type'] : $form_state['values']['product_type']['type'];

  // Save settings for the current product type.
  if (!empty($form_state['values']['product_type']['commerce_pricing_attributes'])) {
    foreach ($form_state['values']['product_type']['commerce_pricing_attributes'] as $key => $value) {
      variable_set('commerce_pricing_atrributes_' . $product_type . '_' . $key, $value);
    }
  }
}

/**
 * Implements hook_menu_contextual_links_alter().
 *
 * Use this hook to add the attributes tabs to the product type overview page.
 */
function commerce_pricing_attributes_ui_menu_contextual_links_alter(&$links, $router_item, $root_path) {

  // Foreach product type with enabled attributes.
  foreach (commerce_pricing_attributes_enabled_product_types() as $type => $product_type) {
    $bundle = strtr($type, '_', '-');
    if ($root_path == 'admin/commerce/products/types/' . $bundle) {
      $links['commerce-product-type-pricing-attributes-fields'] = menu_get_item($root_path . '/pricing_attributes/fields');
      $links['commerce-product-type-pricing-attributes-display'] = menu_get_item($root_path . '/pricing_attributes/display');
    }
  }
}