You are here

class CommerceDiscountOfferInlineEntityFormController in Commerce Discount 7

Defines the inline entity form controller for Commerce Discount Offers.

Hierarchy

Expanded class hierarchy of CommerceDiscountOfferInlineEntityFormController

1 string reference to 'CommerceDiscountOfferInlineEntityFormController'
commerce_discount_entity_info in ./commerce_discount.module
Implements hook_entity_info().

File

includes/commerce_discount_offer.inline_entity_form.inc, line 11
Provides Inline Entity Form integration for the Commerce Discount module.

View source
class CommerceDiscountOfferInlineEntityFormController extends EntityInlineEntityFormController {

  /**
   * {@inheritdoc}
   */
  public function entityForm($entity_form, &$form_state) {

    // If we're cloning, we want to clone the offer entity too.
    if ($form_state['op'] === 'clone') {
      unset($entity_form['#entity']->discount_offer_id);
      $entity_form['#entity']->is_new = TRUE;
    }
    $offer = $entity_form['#entity'];
    if (!empty($form_state['values'])) {
      $offer_values = drupal_array_get_nested_value($form_state['values'], $entity_form['#parents']);

      // If the type was changed via AJAX, change it on the entity.
      if (!empty($offer_values['type'])) {

        // We have to use the form_state to hold this, because if other
        // elements set #limit_validation_errors, form state values will not
        // contain what we need.
        $form_state['commerce_discount_offer_selection_' . $entity_form['#ief_id']] = $offer_values['type'];
      }
    }
    if (isset($form_state['commerce_discount_offer_selection_' . $entity_form['#ief_id']])) {
      $offer->type = $form_state['commerce_discount_offer_selection_' . $entity_form['#ief_id']];
    }

    // Get discount type.
    $discount_type = commerce_discount_type($form_state['commerce_discount']->type);
    $offer_types = array();
    foreach (commerce_discount_offer_types() as $type => $info) {
      if (in_array($discount_type['entity type'], $info['entity types'])) {
        $offer_types[$type] = $info['label'];
      }
    }

    // Ensures the discount type includes the passed offer type. If not, set the
    // offer type to the first found valid offer.
    if (!in_array($offer->type, array_keys($offer_types))) {
      $offer->type = reset(array_keys($offer_types));
    }
    $ief_id = $entity_form['#ief_id'];
    field_attach_form('commerce_discount_offer', $offer, $entity_form, $form_state, LANGUAGE_NONE);
    $fields_wrapper = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'commerce-offer-fields-wrapper',
        ),
      ),
      '#weight' => 2,
      '#parents' => array(
        'commerce_discount_fields',
        'commerce_discount_offer',
        LANGUAGE_NONE,
        'form',
      ),
    );

    // Transfer field elements onto fields container.
    foreach (element_children($entity_form) as $key) {
      $fields_wrapper[$key] = $entity_form[$key];
      unset($entity_form[$key]);
    }

    // Set the fields container back to the form.
    $entity_form['commerce_discount_offer_fields'] = $fields_wrapper;

    // Add offer type options.
    $entity_form['type'] = array(
      '#type' => 'radios',
      '#title' => t('Choose offer type'),
      '#title_display' => 'invisible',
      '#options' => $offer_types,
      '#required' => FALSE,
      '#default_value' => $offer->type,
      '#ajax' => array(
        'callback' => 'inline_entity_form_get_element',
        'wrapper' => 'inline-entity-form-' . $ief_id,
      ),
      '#weight' => 1,
    );

    // Add wrapper class for CSS to avoid form item collisions.
    $entity_form['#attributes']['class'][] = 'commerce-offer-type-wrapper';
    $entity_form['#type'] = 'fieldset';
    $entity_form['#title'] = t('Choose offer type');
    return $entity_form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceDiscountOfferInlineEntityFormController::entityForm public function Returns the entity form to be shown through the IEF widget. Overrides EntityInlineEntityFormController::entityForm
EntityInlineEntityFormController::$entityType protected property
EntityInlineEntityFormController::$settings public property
EntityInlineEntityFormController::createClone public function Creates a clone of the given entity. 2
EntityInlineEntityFormController::css public function Returns an array of css filepaths for the current entity type, keyed by theme name. 1
EntityInlineEntityFormController::defaultLabels public function Returns the default entity type labels. 2
EntityInlineEntityFormController::defaultSettings public function Returns an array of default settings in the form of key => value. 2
EntityInlineEntityFormController::delete public function Delete permanently saved entities. 1
EntityInlineEntityFormController::entityFormSubmit public function Handles the submission of an entity form. 4
EntityInlineEntityFormController::entityFormValidate public function Validates the entity form. 2
EntityInlineEntityFormController::entityType public function Returns the entity type managed by this controller.
EntityInlineEntityFormController::getSetting public function Returns a setting value.
EntityInlineEntityFormController::labels public function Returns an array of entity type labels fit for display in the UI.
EntityInlineEntityFormController::removeForm public function Returns the remove form to be shown through the IEF widget. 1
EntityInlineEntityFormController::removeFormSubmit public function Handles the submission of a remove form. Decides what should happen to the entity after the removal confirmation.
EntityInlineEntityFormController::save public function Permanently saves the given entity. 2
EntityInlineEntityFormController::settingsForm public function Returns the settings form for the current entity type. 2
EntityInlineEntityFormController::tableFields public function Returns an array of fields used to represent an entity in the IEF table. 4
EntityInlineEntityFormController::__construct public function 1