You are here

public function CommerceProductInlineEntityFormController::entityForm in Inline Entity Form 7

Overrides EntityInlineEntityFormController::entityForm().

Overrides EntityInlineEntityFormController::entityForm

File

includes/commerce_product.inline_entity_form.inc, line 143
Defines the inline entity form controller for Commerce Products.

Class

CommerceProductInlineEntityFormController
@file Defines the inline entity form controller for Commerce Products.

Code

public function entityForm($entity_form, &$form_state) {
  global $user;

  // Get the labels (product / variation).
  $labels = $this
    ->labels();
  $product = $entity_form['#entity'];
  $extra_fields = field_info_extra_fields('commerce_product', $product->type, 'form');

  // Assign newly created products to the current user.
  if (empty($product->product_id)) {
    $product->uid = $user->uid;
  }
  $entity_form['product_attributes'] = array(
    '#type' => 'fieldset',
    '#title' => t('Attributes'),
    '#attributes' => array(
      'class' => array(
        'container-inline',
        'ief-product-attributes',
        'ief-entity-fieldset',
      ),
    ),
  );
  $entity_form['product_details'] = array(
    '#type' => 'fieldset',
    '#title' => t('Details'),
    '#attributes' => array(
      'class' => array(
        'ief-product-details',
        'ief-entity-fieldset',
      ),
    ),
  );
  $entity_form['sku'] = array(
    '#type' => 'textfield',
    '#title' => t('SKU'),
    '#description' => t('Supply a unique identifier using letters, numbers, hyphens, and underscores. Commas may not be used.'),
    '#default_value' => $product->sku,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#fieldset' => 'product_details',
    '#weight' => $extra_fields['sku']['weight'],
  );
  $entity_form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('@label title', array(
      '@label' => drupal_ucfirst($labels['singular']),
    )),
    '#default_value' => $product->title,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#fieldset' => 'product_details',
    // The label might be missing if the Title module has replaced it.
    '#weight' => !empty($extra_fields['title']) ? $extra_fields['title']['weight'] : -9,
  );
  $entity_form['status'] = array(
    '#type' => 'radios',
    '#title' => t('Status'),
    '#default_value' => $product->status,
    '#options' => array(
      1 => t('Active'),
      0 => t('Disabled'),
    ),
    '#required' => TRUE,
    '#fieldset' => 'product_details',
    '#weight' => $extra_fields['status']['weight'],
  );

  // Attach fields.
  $langcode = entity_language('commerce_product', $product);
  field_attach_form('commerce_product', $product, $entity_form, $form_state, $langcode);

  // Hide or disable the SKU field if it is auto-generated by Commerce AutoSKU.
  if (module_exists('commerce_autosku') && ($settings = commerce_autosku_get_settings($product))) {
    $entity_form['sku']['#required'] = FALSE;
    $entity_form['sku']['#disabled'] = TRUE;
    if ($settings['advanced']['hide_sku']) {
      $entity_form['sku']['#access'] = FALSE;
    }
    else {
      $entity_form['sku']['#description'] = t('Will be auto-generated when the form is saved.');
    }
  }

  // Hide the title field if it is auto-generated.
  if ($this->settings['autogenerate_title']) {
    $entity_form['title']['#required'] = FALSE;
    $entity_form['title']['#access'] = FALSE;

    // Hide the replacement field added by the Title module as well.
    if (module_exists('title')) {
      $title_field = title_field_replacement_info('commerce_product', 'title');
      if ($title_field) {
        $title_field_name = $title_field['field']['field_name'];
        if (isset($entity_form[$title_field_name])) {
          $entity_form[$title_field_name]['#access'] = FALSE;
          $entity_form[$title_field_name]['#required'] = FALSE;
        }
      }
    }
  }

  // Arrange attributes.
  $attributes = $this
    ->attributes($product->type);
  if (empty($attributes)) {

    // Hide the fieldset, it will be empty.
    $entity_form['product_attributes']['#access'] = FALSE;
  }
  else {
    foreach ($attributes as $field_name => $attribute) {
      $entity_form[$field_name]['#fieldset'] = 'product_attributes';
    }
  }

  // Arrange non-attribute fields.
  foreach (field_info_instances('commerce_product', $product->type) as $name => $instance) {
    $field_name = $instance['field_name'];
    $field = field_info_field($field_name);
    if (!isset($attributes[$field_name])) {
      $entity_form[$field_name]['#fieldset'] = 'product_details';
    }
  }
  $product_type = commerce_product_type_load($product->type);
  $product->revision = $product_type['revision'];
  return $entity_form;
}