You are here

function microdata_get_instance_mapping_form in Microdata 7

Form builder helper function.

Creates the field instance mapping form to be added to Field UI forms. This is also used by microdata_bundle_mapping_form to add the instance's settings to the bundle in Microdata's custom UI.

2 calls to microdata_get_instance_mapping_form()
microdata_bundle_mapping_form in ./microdata.admin.inc
Form builder helper function.
microdata_form_field_ui_field_edit_form_alter in includes/microdata.form_alter.inc
Implements hook_form_FORM_ID_alter().

File

./microdata.admin.inc, line 244
Microdata administration and module settings UI.

Code

function microdata_get_instance_mapping_form($field_name, $instance) {
  $entity_type = is_array($instance['entity_type']) ? $instance['entity_type']['#value'] : $instance['entity_type'];
  $bundle = is_array($instance['bundle']) ? $instance['bundle']['#value'] : $instance['bundle'];
  $entity_info = entity_get_info($entity_type);
  $mapping = microdata_get_mapping($entity_type, $bundle);
  $form_element = array();

  // Only display the config options if there is a recognized value_type for
  // this field.
  if (isset($mapping[$field_name]['#value_type'])) {
    $form_element['field']['itemprop'] = _microdata_get_form_elements('itemprop', $field_name, $mapping);
    if ($mapping[$field_name]['#value_type'] === 'item_option') {
      $form_element['field']['is_item'] = _microdata_get_form_elements('is_item', $field_name, $mapping[$field_name]);
      $form_element['field']['item_fieldset'] = _microdata_get_form_elements('item_fieldset', $field_name, $mapping);
      $form_element['field']['item_fieldset']['#states'] = array(
        'visible' => array(
          ':input[name="microdata[fields][' . $field_name . '][field][is_item]"]' => array(
            'checked' => TRUE,
          ),
        ),
      );
      $form_element['field']['item_fieldset']['itemtype'] = _microdata_get_form_elements('itemtype', $field_name, $mapping);
    }
  }

  // If the field declares properties, then add form fields for subfield
  // mappings.
  $subfields = _microdata_get_field_properties($entity_type, $bundle, $field_name);
  if (!empty($subfields)) {
    $form_element['subfields'] = array(
      '#type' => 'fieldset',
      '#title' => t('Properties'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
    );
    $mapping = $mapping[$field_name];

    // Iterate through subfields and add the same form fields for each one.
    foreach ($subfields as $subfield_name => $subfield) {
      $form_element['subfields'][$subfield_name] = array();
      $form_state['input']['microdata']['subfields'][$subfield_name] = array();
      $subfield_form =& $form_element['subfields'][$subfield_name];
      $subfield_input =& $form_state['input']['microdata']['subfields'][$subfield_name];
      $subfield_form['itemprop'] = _microdata_get_form_elements('itemprop', $subfield_name, $mapping, $subfield['label']);
      $subfield_input['itemprop'] = $mapping['#itemprop'];
      if ($mapping[$subfield_name]['#value_type'] == 'item') {
        $subfield_form['item_fieldset'] = _microdata_get_form_elements('item_fieldset', $field_name, $mapping);
        $subfield_form['item_fieldset']['itemtype'] = _microdata_get_form_elements('itemtype', $subfield_name, $mapping, $subfield['label']);
        $subfield_input['item_fieldset']['itemtype'] = $mapping['#itemtype'];
      }
    }
  }
  return $form_element;
}