You are here

function commerce_license_field_widget_form in Commerce License 7

Implements hook_field_widget_form().

File

./commerce_license.module, line 1012
Provides a framework for selling access to local or remote resources.

Code

function commerce_license_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($instance['widget']['type'] == 'inline_entity_form_license') {

    // Using #title_display = invisible doesn't work here.
    $element['#title'] = '';
    if (!empty($form_state['default_product'])) {
      $product_wrapper = entity_metadata_wrapper('commerce_product', $form_state['default_product']);
      if (empty($form_state['default_product']->commerce_license_type)) {

        // This product is not licensable.
        return array();
      }
      $product_wrapper = entity_metadata_wrapper('commerce_product', $form_state['default_product']);
      $license_type = $product_wrapper->commerce_license_type
        ->value();

      // Inject the desired bundle.
      $field['settings']['handler_settings']['target_bundles'] = array(
        $license_type,
      );
    }

    // Workaround the IEF condition.
    $instance['widget']['type'] = 'inline_entity_form_single';
    $element = inline_entity_form_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);

    // Remove the fieldset around the license form, it's not needed.
    $element['#type'] = 'container';
    return $element;
  }
  elseif ($instance['widget']['type'] == 'commerce_license_duration') {
    $element += array(
      '#type' => 'container',
      '#attached' => array(
        'css' => array(
          drupal_get_path('module', 'commerce_license') . '/theme/commerce-license.css',
        ),
      ),
      '#attributes' => array(
        'class' => array(
          'commerce-license-duration-wrapper',
        ),
      ),
      '#element_validate' => array(
        'commerce_license_duration_validate',
      ),
    );

    // Move description to the top
    if (!empty($element['#description'])) {
      $element['description'] = array(
        '#markup' => '<div class="description">' . $element['#description'] . '</div>',
        '#weight' => -10,
      );
      unset($element['#description']);
    }
    $default_mode = 'unlimited';
    $default_value = 5;
    $default_unit = 86400;
    if (!empty($items[0]) && !empty($items[0]['value'])) {
      $default_mode = 'limited';
      list($default_value, $default_unit) = commerce_license_duration_from_timestamp($items[0]['value']);
    }
    $element['mode'] = array(
      '#type' => 'radios',
      '#title' => $element['#title'],
      '#options' => array(
        'unlimited' => t('Unlimited'),
        'limited' => t('Limited'),
      ),
      '#default_value' => $default_mode,
      '#attributes' => array(
        'class' => array(
          'commerce-license-duration-mode',
        ),
      ),
    );

    // Get the correct path to the mode element, taking into account field
    // parents (set when IEF is used, for example).
    $mode_parents = array(
      $element['#field_name'],
      LANGUAGE_NONE,
      0,
      'mode',
    );
    $mode_parents = array_merge($element['#field_parents'], $mode_parents);
    $mode_path = array_shift($mode_parents);
    foreach ($mode_parents as $mode_parent) {
      $mode_path .= "[{$mode_parent}]";
    }
    $description = t('Note: Months are 30 days long');
    $element['duration'] = array(
      '#type' => 'container',
      'value' => array(
        '#type' => 'textfield',
        '#title' => t('Duration'),
        '#title_display' => 'invisible',
        '#size' => 5,
        '#default_value' => $default_value,
        '#element_validate' => array(
          'element_validate_integer_positive',
        ),
      ),
      'unit' => array(
        '#type' => 'select',
        '#default_value' => $default_unit,
        '#options' => commerce_license_duration_units(),
      ),
      'description' => array(
        '#markup' => '<div class="description">' . $description . '</div>',
      ),
      '#attributes' => array(
        'class' => array(
          'commerce-license-duration container-inline',
        ),
      ),
      '#states' => array(
        'invisible' => array(
          ':input[name="' . $mode_path . '"]' => array(
            'value' => 'unlimited',
          ),
        ),
      ),
    );
    return $element;
  }
}