You are here

function sarnia_field_formatter_settings_form in Sarnia 7

Implements hook_field_formatter_settings_form().

File

./sarnia.field_formatter.inc, line 135
Field formatter hook implementations.

Code

function sarnia_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $formatter = $instance['display'][$view_mode]['type'];
  $settings = $instance['display'][$view_mode]['settings'];

  // Add a "Solr property" setting to each formatter settings form. When these
  // formatters are used in the context of SarniaViewsHandlerField::get_options(),
  // this form element is stripped off in favor of a handler-wide form element.
  $sarnia_type = sarnia_entity_type_load($instance['entity_type']);
  $solr_property_options = sarnia_index_get_field_options($sarnia_type['search_api_index']);
  $form['solr_property'] = array(
    '#type' => 'select',
    '#title' => t('Solr property'),
    '#description' => t('Choose a Solr property to format.'),
    '#options' => $solr_property_options,
    '#default_value' => $settings['solr_property'],
  );

  // Formatter-specific settings.
  if ($formatter == 'sarnia_formatter_date') {

    // Summon up all the system-provided date formats.
    $options = array();
    foreach (module_invoke_all('date_formats') as $format) {
      $options[$format['format']] = date($format['format'], $_SERVER['REQUEST_TIME']);
    }

    // Add a 'custom' option to the end of the list.
    $options[0] = '- ' . t('Custom') . ' -';
    $form['date_format_predefined'] = array(
      '#type' => 'select',
      '#title' => t('Date format'),
      '#options' => $options,
      '#default_value' => $settings['date_format_predefined'],
    );
    $form['date_format_custom'] = array(
      '#type' => 'textfield',
      '#title' => t('Custom date format'),
      '#default_value' => $settings['date_format_custom'],
      '#states' => array(
        'visible' => array(
          ':input[name="options[settings][date_format_predefined]"]' => array(
            'value' => '0',
          ),
        ),
      ),
    );
    $form['data_is_timestamp'] = array(
      '#type' => 'checkbox',
      '#title' => t('This data is already a unix timestamp'),
      '#default_value' => $settings['data_is_timestamp'],
    );
  }
  elseif ($formatter == 'sarnia_formatter_number') {
    $form['separate_thousands'] = array(
      '#type' => 'checkbox',
      '#title' => t('Separate thousands and hide decimals'),
      '#default_value' => $settings['separate_thousands'],
    );
  }
  elseif ($formatter == 'sarnia_formatter_multimedia' || $formatter == 'sarnia_formatter_image') {
    $form['base_path'] = array(
      '#type' => 'textfield',
      '#title' => t('Base URL or path'),
      '#description' => t('A shared base URL or path for all images.'),
      '#default_value' => $settings['base_path'],
    );
    $options = $solr_property_options;
    array_unshift($options, '- ' . t('None') . ' -');
    $form['path_solr_property'] = array(
      '#type' => 'select',
      '#title' => t('Path component'),
      '#description' => t('Choose a Solr property to use as a path component or base URL.'),
      '#options' => $options,
      '#default_value' => $settings['path_solr_property'],
    );
    $form['alt_solr_property'] = array(
      '#type' => 'select',
      '#title' => t('Alternate text'),
      '#description' => t('Choose a Solr property to use as alternate text.'),
      '#options' => $options,
      '#default_value' => $settings['alt_solr_property'],
    );
    $form['title_solr_property'] = array(
      '#type' => 'select',
      '#title' => t('Title (mouseover) text'),
      '#description' => t('Choose a Solr property to use as title text.'),
      '#options' => $options,
      '#default_value' => $settings['title_solr_property'],
    );
    sarnia_element_add_combobox($form['path_solr_property']);
    sarnia_element_add_combobox($form['alt_solr_property']);
    sarnia_element_add_combobox($form['title_solr_property']);
  }
  elseif ($formatter == 'sarnia_formatter_text') {
    $options = array();
    foreach (filter_formats() as $format => $info) {
      $options[$format] = $info->name;
    }
    $form['input_format'] = array(
      '#type' => 'select',
      '#title' => t('Input format'),
      '#options' => $options,
      '#default_value' => $settings['input_format'],
    );
  }
  elseif ($formatter == 'sarnia_formatter_multimedia_count') {
    $form['show_icon'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show icon'),
      '#description' => t('Show file type icons.'),
      '#default_value' => $settings['show_icon'],
    );
  }
  elseif ($formatter == 'sarnia_formatter_openlayers') {

    // Disable the default Solr property.
    // @TODO maybe use this to select a "title" field?
    $form['solr_property']['#access'] = FALSE;
    $form['lat_solr_property'] = array(
      '#type' => 'select',
      '#title' => t('Latitude'),
      '#description' => t('Solr property to use as the latitude.'),
      '#options' => $solr_property_options,
      '#default_value' => $settings['lat_solr_property'],
    );
    $form['lon_solr_property'] = array(
      '#type' => 'select',
      '#title' => t('Longitude'),
      '#description' => t('Solr property to use as the longitude.'),
      '#options' => $solr_property_options,
      '#default_value' => $settings['lon_solr_property'],
    );
    sarnia_element_add_combobox($form['lat_solr_property']);
    sarnia_element_add_combobox($form['lon_solr_property']);

    // Get preset options, filtered to those which have the GeoField placeholder layer
    $maps = openlayers_maps();
    $options = array();
    foreach ($maps as $map) {
      if (in_array('sarnia_formatter_layer', $map->data['layers'])) {
        $options[$map->name] = $map->title;
      }
    }
    $form['openlayers_map'] = array(
      '#type' => 'select',
      '#title' => t('OpenLayers map'),
      '#description' => t('Select an OpenLayers map to use. Only maps which have the "Sarnia Formatter" layer may be selected.'),
      '#options' => $options,
      '#default_value' => $settings['openlayers_map'],
    );
  }
  return $form;
}