You are here

function _commerce_file_field_add_license_elements in Commerce File 7

License settings element generator

2 calls to _commerce_file_field_add_license_elements()
commerce_file_field_instance_settings_form in includes/commerce_file.field.inc
Implements hook_field_instance_settings_form().
commerce_file_field_widget_form in includes/commerce_file.field.inc
Implements hook_field_widget_form().

File

includes/commerce_file.field.inc, line 483
Implement an commerce_file field, based on the file module's file field.

Code

function _commerce_file_field_add_license_elements(&$parent, $level = 'field', $settings = array(), $inherited_settings = array()) {
  $base = array();
  $defaults = array();
  $license_info = _commerce_file_collate_license_info();

  // allow parent element to suggest inheritable
  $parent_inheritable = isset($parent['#inheritable']) ? $parent['#inheritable'] : TRUE;
  $have_inherited_settings = !empty($inherited_settings);
  switch ($level) {
    case 'widget':
      $defaults = field_info_instance_settings(COMMERCE_FILE_FIELD_TYPE);
      break;
    case 'instance':
      $defaults = field_info_instance_settings(COMMERCE_FILE_FIELD_TYPE);
      break;
    default:
      $defaults = field_info_field_settings(COMMERCE_FILE_FIELD_TYPE);
      break;
  }

  // Initialize license base elements
  foreach ($license_info as $k => &$info) {

    // initialize base element if not defined
    if (!isset($info['base_element'])) {
      $info['base_element'] = array();
    }

    // set base element #inheritable property
    if ($have_inherited_settings) {

      // allow setting definition to override if explicitly set to NOT inherit
      // else set to parent's inheritable
      if (!isset($info['property info']['inheritable']) || $info['property info']['inheritable']) {
        $info['base_element']['#inheritable'] = $parent_inheritable;
      }
    }
    else {

      // force to not inheritable if not inherited settings given
      $info['base_element']['#inheritable'] = FALSE;
    }
  }
  unset($info);

  // build inherited settings for license settings
  $inherited = array(
    'descriptions' => array(),
    'values' => array(),
  );
  if ($have_inherited_settings) {
    $inherited_tokens = array();

    // alter license info array
    foreach ($license_info as $k => &$info) {

      // skip if non-inheritable
      if (empty($info['base_element']['#inheritable'])) {
        continue;
      }

      // retrieve inherited value
      $inherited['values'][$k] = $inherited_value_k = _commerce_file_license_resolve_setting_value(NULL, $inherited_settings['data'][$k]);
      if (!isset($inherited_value_k)) {
        continue;
      }

      // if no setting value then set to inherit by default
      if (!isset($settings['data'][$k])) {
        $settings['data'][$k] = COMMERCE_FILE_FIELD_INHERIT;
      }
    }
    unset($info);
  }
  else {
    $inherited_settings = array();
  }

  // merge all defaults
  $values = array_merge($defaults, $inherited_settings, $settings);

  // build form elements
  $elements = array();
  foreach ($license_info as $k => $info) {
    if (!isset($values['data'][$k])) {
      continue;
    }

    // set default value to inherited value if defined
    $default_value = $values['data'][$k];
    $inherited_value = isset($inherited['values'][$k]) ? $inherited['values'][$k] : NULL;

    // initialize element with default value, inherited value, and base element
    $elements[$k] = array(
      '#default_value' => $default_value,
      '#limit_inherited_value' => $inherited_value,
    ) + $info['base_element'];
  }

  // update parent with settings elements
  if (!empty($elements)) {
    $element_container = array(
      '#type' => 'fieldset',
      '#title' => t('Access Limits'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 150,
      '#attributes' => array(
        'class' => array(
          'clearfix',
        ),
      ),
      '#access' => !empty($elements),
    );

    // merge settings into parent element
    $parent = array_merge($parent, array(
      'data' => $element_container + $elements,
    ));
  }
}