You are here

function mvf_field_settings_form in Measured Value Field 7

Implements hook_field_settings_form().

File

./mvf.module, line 279
Define a field type of measured value.

Code

function mvf_field_settings_form($field, $instance, $has_data) {
  $form = array();

  // Firstly we recursively merge field settings forms for each of our
  // sub-fields.
  foreach ($field['settings'] as $subfield => $settings) {
    switch ($subfield) {
      case 'value':
      case 'unit':

        // Mocking up field.
        $mock_field = mvf_field_mockup($field, $subfield);

        // Mocking up instance.
        $mock_instance = mvf_instance_mockup($field, $instance, $subfield);
        $extra = module_invoke($field['settings']['meta_info'][$subfield]['module'], 'field_settings_form', $mock_field, $mock_instance, $has_data);

        // Doing any customizations in the output of a sub field hook.
        switch ($subfield) {
          case 'unit':

            // We have to add our custom validate function that will "repair"
            // what brakes entity reference native validate function.
            $extra['#element_validate'][] = 'mvf_entityreference_field_settings_validate';

            // We hardcore entity type to be 'units_unit'. It would be only
            // confusing to end user letting him see this setting. We have to do
            // it in a process function, because entityreference module defines
            // its actual form elements in its own process function. We have no
            // other choice.
            $extra['#process'][] = 'mvf_entityreference_field_settings_process';
            break;
        }
        $form[$subfield] = array(
          '#tree' => TRUE,
        );
        if (is_array($extra) && !empty($extra)) {
          $form[$subfield] += array(
            '#type' => 'fieldset',
            '#title' => t('@label Settings', array(
              '@label' => $field['settings']['meta_info'][$subfield]['label'],
            )),
            '#collapsible' => TRUE,
          ) + $extra;
        }
        break;
      case 'meta_info':
        $form['meta_info'] = array(
          '#type' => 'fieldset',
          '#title' => t('Sub Widgets'),
          '#tree' => TRUE,
          '#collapsible' => TRUE,
        );

        // Get a list of widgets for each of the sub fields.
        $info = _field_info_collate_types();
        foreach ($settings as $subfield2 => $meta_info) {

          // Filtering out only those widgets that apply to our sub field.
          $widgets = array();
          foreach ($info['widget types'] as $widget_type => $widget) {
            if (in_array($meta_info['field_type'], $widget['field types']) && !in_array($widget_type, $meta_info['not_supported_widgets'])) {
              $widgets[$widget_type] = $widget;
            }
          }
          $options = array();
          foreach ($widgets as $widget_type => $widget) {
            $options[$widget_type] = $widget['label'];
          }
          $form['meta_info'][$subfield2] = array();
          $form['meta_info'][$subfield2]['widget'] = array(
            '#type' => 'select',
            '#title' => t('@label Widget', array(
              '@label' => $meta_info['label'],
            )),
            '#description' => t('Please, choose the widget for @label part of the field.', array(
              '@label' => $meta_info['label'],
            )),
            '#required' => TRUE,
            '#options' => $options,
            '#default_value' => $meta_info['widget'],
          );

          // Adding another hidden info.
          foreach ($meta_info as $k => $v) {
            if (!isset($form['meta_info'][$subfield2][$k])) {
              $form['meta_info'][$subfield2][$k] = array(
                '#type' => 'value',
                '#value' => $v,
              );
            }
          }
        }
        break;
    }
  }
  return $form;
}