Commit 2fc77eaa authored by Brock Boland's avatar Brock Boland
Browse files

Issues #1992322: Added options for field formatters

parent c80aa5e5
Loading
Loading
Loading
Loading
+85 −4
Original line number Diff line number Diff line
@@ -25,13 +25,15 @@ function node_subpages_content_field_config($type, $plugin_config) {
  $formatters = array();
  $fields = field_info_instances('node', $type->type);
  if ($fields) {

    foreach ($fields as $field_key => $field_details) {
      $options[$field_key] = $field_details['label'] . ' (' . $field_key . ')';
      $formatters[$field_key] = _node_subpages_content_field_formatter_options($field_key);
    }
  }

  $form = array(
    '#tree' => TRUE,
  );
  $form['field'] = array(
    '#type' => 'select',
    '#title' => t('Field'),
@@ -54,7 +56,7 @@ function node_subpages_content_field_config($type, $plugin_config) {
        $default_value = isset($fields[$field_key]['display']['default']['type']) ? $fields[$field_key]['display']['default']['type'] : NULL;
      }

      $form[$field_key . '_formatter'] = array(
      $form[$field_key]['formatter'] = array(
        '#type' => 'select',
        '#title' => t('Field Formatter'),
        '#description' => t('Formatter to use for ') . $field_key,
@@ -63,10 +65,36 @@ function node_subpages_content_field_config($type, $plugin_config) {
        '#default_value' => $default_value,
        '#states' => array(
          'visible' => array(
            ':input[name="field"]' => array('value' => $field_key),
            ':input[name="options[field]"]' => array('value' => $field_key),
          ),
        ),
      );

      // Add the options for each formatter
      foreach ($formatter_options as $formatter_key => $formatter_label) {
        $config_form = _node_subpages_content_field_formatter_config_form($formatter_key, $fields[$field_key], $plugin_config);
        if (!empty($config_form)) {
          // Figure out the name of the formatter dropdown
          $parent_element_name = 'options[' . $field_key . '][formatter]';
          // For each of the formatter options:
          foreach (element_children($config_form) as $k) {
            // Don't make it required
            $config_form[$k]['#required'] = FALSE;
            // Don't show it unless this formatter is chosen
            $config_form[$k]['#states'] = array(
              'visible' => array(
                ':input[name="' . $parent_element_name . '"]' => array('value' => $formatter_key),
                // Also make sure that this field is chosen (otherwise, this
                // option might be shown when the hidden formatter dropdowns are
                // set to this formatter)
                ':input[name="options[field]"]' => array('value' => $field_key),
              ),
            );
            // Update the default value
          }
          $form[$field_key][$formatter_key] = $config_form;
        }
      }
    }
  }

@@ -78,7 +106,14 @@ function node_subpages_content_field_config($type, $plugin_config) {
 * Prep the config for save to the DB, by placing into an array
 */
function node_subpages_content_field_config_save_prep($values) {
  return array('field' => $values['field'], 'formatter' => $values[$values['field'] . '_formatter']);
  $field_name = $values['options']['field'];
  $formatter = $values['options'][$field_name]['formatter'];
  $formatter_options = $values['options'][$field_name][$formatter];
  return array(
    'field' => $field_name,
    'formatter' => $formatter,
    'formatter_options' => $formatter_options,
  );
}


@@ -95,6 +130,9 @@ function node_subpages_content_field_content($node, $plugin_config) {
  if (isset($plugin_config['formatter'])) {
    $display['type'] = $plugin_config['formatter'];
  }
  if (isset($plugin_config['formatter_options'])) {
    $display['settings'] = $plugin_config['formatter_options'];
  }
  $field = field_view_field('node', $node, $plugin_config['field'], $display);
  return $field;
}
@@ -162,3 +200,46 @@ function _node_subpages_content_field_formatter_label($field_name, $formatter) {
  }
  return '';
}


/**
 * Get the config form for a field formatter.
 */
function _node_subpages_content_field_formatter_config_form($formatter_key, $instance, $plugin_config = array()) {
  $config_forms = &drupal_static(__FUNCTION__);
  $all_formatters = &drupal_static(__FUNCTION__ . '_formatters');

  if (empty($all_formatters)) {
    $all_formatters = field_info_formatter_types();
  }

  if (!isset($config_forms[$formatter_key])) {
    $config_forms[$formatter_key] = array();

    // Get the formatter details
    $formatter = $all_formatters[$formatter_key];
    // See if the module providing the formatter has a
    // hook__field_formatter_settings_form()
    $function = $formatter['module'] . '_field_formatter_settings_form';
    if (function_exists($function)) {
      // Get the full field info
      $field = field_info_field($instance['field_name']);
      // Junk vars that the _field_formatter_settings_form function needs
      $form = $form_state = array();
      $view_mode = 'default';
      // Force the formatter type on the field instance to the one we're
      // interested in.
      $instance['display'][$view_mode]['type'] = $formatter_key;
      // Set any other settings from the plugin, but only if the plugin config
      // has options for the formatter key in question
      if ($formatter_key == $plugin_config['formatter']) {
        $instance['display'][$view_mode]['settings'] = $plugin_config['formatter_options'];
      }

      // Get the form
      $config_forms[$formatter_key] = $function($field, $instance, $view_mode, $form, $form_state);
    }
  }

  return $config_forms[$formatter_key];
}