You are here

function microdata_form_field_ui_field_edit_form_alter in Microdata 7

Implements hook_form_FORM_ID_alter().

File

includes/microdata.form_alter.inc, line 71
Provides inline structured data using Microdata format.

Code

function microdata_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  module_load_include('inc', 'microdata', 'microdata.admin');

  // @todo Fix this?
  // If the form is set to rebuild, it skips form_execute_handlers at line 829
  // in form.inc.
  if (!empty($form_state['process_input'])) {
    form_execute_handlers('submit', $form, $form_state);
  }
  $field_name = $form['#field']['field_name'];
  $instance = $form['instance'];
  $label = isset($instance['label']) ? $instance['label']['#default_value'] : $instance['field_name'];
  $entity_type = $instance['entity_type']['#value'];
  $entity_info = entity_get_info($entity_type);
  $bundle = $instance['bundle']['#value'];
  $bundle_label = $entity_info['bundles'][$bundle]['label'];
  $mapping = microdata_get_mapping($entity_type, $bundle);
  $module = $form['#field']['module'];

  // Show the itemtype of the entity.
  if (!empty($mapping['#itemtype'])) {
    foreach ($mapping['#itemtype'] as $itemtype) {
      $itemtypes[] = l($itemtype, $itemtype);
    }
    $itemtype_msg = t('@label uses the itemtype !itemtype.', array(
      '@label' => $bundle_label,
      '!itemtype' => implode(', ', $itemtypes),
    ));
  }
  elseif (isset($entity_info['bundles'][$bundle]['admin']['real path'])) {
    $edit_path = $entity_info['bundles'][$bundle]['admin']['real path'];
    $itemtype_msg = t('No itemtype has been defined for %bundle. Define one on the !edit.', array(
      '%bundle' => $bundle_label,
      '!edit' => l(check_plain($bundle_label) . ' edit page', $edit_path),
    ));
  }
  else {
    $itemtype_msg = t('No itemtype has been defined for %bundle.', array(
      '%bundle' => $bundle_label,
    ));
  }

  // Create the fieldset tab.
  $form['microdata'] = array(
    '#type' => 'fieldset',
    '#title' => t('@label Microdata Mapping', array(
      '@label' => $label,
    )),
    '#description' => $itemtype_msg,
    '#prefix' => '<div id="microdata-fieldset-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  );

  // Add the mapping form fields for the field. If 'Use suggested mapping' was
  // clicked, the input values will be populated with the default mapping
  // values.
  $form['microdata']['fields'][$field_name]['#type'] = 'container';
  $form['microdata']['fields'][$field_name] = array_merge($form['microdata']['fields'][$field_name], microdata_get_instance_mapping_form($field_name, $instance));
  $form_state['input']['microdata']['fields'][$field_name]['field'] = array();

  // Get the suggested mapping from $form_state if it is there.
  if (empty($form_state['field_mapping'])) {
    $field_mapping = $mapping[$field_name];
  }
  else {

    // Merge in the original mapping, which has the value types for the field
    // and properties.
    $field_mapping = array_merge($mapping[$field_name], $form_state['field_mapping']);
  }

  // Add the suggested mapping for the field.
  $main_field_input =& $form_state['input']['microdata']['fields'][$field_name]['field'];
  $main_field_input['itemprop'] = $field_mapping['#itemprop'];
  $main_field_input['is_item'] = isset($field_mapping['#is_item']) ? $field_mapping['#is_item'] : NULL;
  $main_field_input['item_fieldset']['itemtype'] = $field_mapping['#itemtype'];

  // Add the suggested mappings for the properties.
  foreach (_microdata_get_field_properties($entity_type, $bundle, $field_name) as $subfield_name => $subfield) {
    $form_state['input']['microdata']['fields'][$field_name]['subfields'][$subfield_name] = array();
    $subfield_mapping = $field_mapping[$subfield_name];
    $subfield_input =& $form_state['input']['microdata']['fields'][$field_name]['subfields'][$subfield_name];
    if (isset($subfield_mapping['#itemprop'])) {
      $subfield_input['itemprop'] = $subfield_mapping['#itemprop'];
    }
    if (isset($subfield_mapping['#itemtype'])) {
      $subfield_input['item_fieldset']['itemtype'] = $subfield_mapping['#itemtype'];
    }
  }

  // Add the button for suggested mappings.
  $suggestions = module_invoke($module, 'microdata_suggestions');
  drupal_alter('microdata_suggestions', $suggestions);
  if (!empty($suggestions)) {
    $form_state['suggested_mappings'] = $suggestions['fields'][$form['#field']['type']];
    $defined_mappings = array_keys($form_state['suggested_mappings']);
    $form['suggested_mapping'] = array(
      '#type' => 'radios',
      '#title' => t('Suggested Microdata Mappings'),
      '#options' => array_combine($defined_mappings, $defined_mappings),
    );
    $form['suggested_mapping']['get_suggested_mapping'] = array(
      '#type' => 'submit',
      '#value' => t('Use suggested mapping'),
      '#executes_submit_callback' => TRUE,
      '#submit' => array(
        'microdata_get_default_field_mapping',
      ),
      // See the examples in ajax_example.module for more details on the
      // properties of #ajax.
      '#ajax' => array(
        'callback' => 'microdata_get_default_field_mapping_callback',
        'wrapper' => 'microdata-fieldset-wrapper',
      ),
      '#weight' => 10,
    );
  }
  $form['submit']['#weight'] = 1;

  // Add submit and validate handlers.
  $form['#validate'] = array_merge($form['#validate'], array(
    'microdata_form_field_ui_field_edit_form_validate',
  ));
  $form['#submit'] = array_merge($form['#submit'], array(
    'microdata_form_field_ui_field_edit_form_submit',
  ));
}