You are here

formatter_field.module in Formatter Field 7

Same filename and directory in other branches
  1. 8 formatter_field.module

File

formatter_field.module
View source
<?php

/**
 * @file
 */

/**
 * Implements hook_field_info().
 */
function formatter_field_field_info() {
  return array(
    'formatter' => array(
      'label' => t('Formatter'),
      'description' => t('This field stores formatter information for another field.'),
      'settings' => array(),
      'instance_settings' => array(
        'field_name' => '',
      ),
      'default_widget' => 'formatter_select',
      'default_formatter' => 'hidden',
    ),
  );
}

/**
 * Implements hook_field_is_empty().
 */
function formatter_field_field_is_empty($item, $field) {
  if (empty($item['type'])) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implements hook_field_instance_settings_form().
 */
function formatter_field_field_instance_settings_form($field, $instance) {
  $settings = $instance['settings'];
  $instances = field_read_instances(array(
    'entity_type' => $instance['entity_type'],
    'bundle' => $instance['bundle'],
  ));
  foreach ($instances as $other_instance) {
    if ($other_instance['field_name'] != $instance['field_name']) {
      $options[$other_instance['field_name']] = $other_instance['label'];
    }
  }
  $form['field_name'] = array(
    '#type' => 'select',
    '#title' => t('Field to be formatted'),
    '#default_value' => $settings['field_name'],
    '#required' => TRUE,
    '#options' => $options,
    '#description' => t('The field to be formatted using the settings in this field.'),
  );
  return $form;
}

/**
 * Implements hook_field_widget_info().
 */
function formatter_field_field_widget_info() {
  return array(
    'formatter_select' => array(
      'label' => t('Formatter select'),
      'field types' => array(
        'formatter',
      ),
      'settings' => array(),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function formatter_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Field to be formatted.
  $formatted_field = field_read_field($instance['settings']['field_name']);

  // Field instance to be formatted.
  $formatted_instance = field_read_instance($instance['entity_type'], $instance['settings']['field_name'], $instance['bundle']);

  // Fetch formatter options, excluding the "from_field" formatter.
  module_load_include('inc', 'field_ui', 'field_ui.admin');
  $formatter_options = field_ui_formatter_options($formatted_field['type']);
  unset($formatter_options['from_field']);
  $formatter_options['hidden'] = t('<Hidden>');

  // Populate $items from $form_state values if available.  This is necessary
  // for the #ajax functionality.
  if (!empty($form_state['values'][$instance['field_name']])) {
    $path = array(
      $field['field_name'],
      $langcode,
    );
    $items = drupal_array_get_nested_value($form_state['values'], $path);
  }
  if (!empty($items[$delta]['settings']) && is_string($items[$delta]['settings'])) {
    $items[$delta]['settings'] = unserialize($items[$delta]['settings']);
  }
  $element['#element_validate'] = array(
    'formatter_field_field_widget_form_validate',
  );
  $wrapper_id = 'formatter-field-settings-form--' . $field['field_name'];
  $element['type'] = array(
    '#type' => 'select',
    '#title' => t('Formatter for @title', array(
      '@title' => $formatted_instance['label'],
    )),
    '#options' => $formatter_options,
    '#default_value' => isset($items[$delta]['type']) ? $items[$delta]['type'] : '',
    '#ajax' => array(
      'callback' => 'formatter_field_field_widget_form_js',
      'wrapper' => $wrapper_id,
      'effect' => 'fade',
    ),
  );
  $settings_form = array();

  // Retrieve the settings form segment, if $items[$delta] is available.
  if (!empty($items[$delta])) {

    // Prepare $formatted_instance to be passed to hook_field_formatter_settings_form().
    // Display settings are stored in the _custom_display view mode.
    $formatter = field_info_formatter_types($items[$delta]['type']);
    $view_mode = '_custom_display';
    $formatted_instance['display'][$view_mode] = $items[$delta] + array(
      'module' => $formatter['module'],
    );
    $function = $formatter['module'] . '_field_formatter_settings_form';
    if (function_exists($function)) {
      $settings_form = $function($formatted_field, $formatted_instance, $view_mode, $form, $form_state);
    }
  }
  $element['settings'] = $settings_form + array(
    '#type' => 'container',
    '#prefix' => '<div id="' . $wrapper_id . '">',
    '#suffix' => '</div>',
    '#tree' => 1,
  );
  return $element;
}

/**
 * #element_validate callback for formatter_field_field_widget_form().
 *
 * Serialize the settings before saving.
 */
function formatter_field_field_widget_form_validate($element, &$form_state) {
  $values = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  if (!isset($values['settings'])) {
    $values['settings'] = array();
  }
  $values['settings'] = serialize($values['settings']);
  form_set_value($element, $values, $form_state);
}

/**
 * #ajax callback for formatter_field_field_widget_form().
 */
function formatter_field_field_widget_form_js($form, &$form_state) {
  $trigger = $form_state['triggering_element'];
  $path = $trigger['#parents'];
  array_pop($path);
  $path[] = 'settings';
  return drupal_array_get_nested_value($form, $path);
}

/**
 * Implements hook_field_formatter_info().
 */
function formatter_field_field_formatter_info() {
  $fields = field_info_field_types();
  unset($fields['formatter']);
  return array(
    'from_field' => array(
      'label' => t('Formatter from field'),
      'field types' => array_keys($fields),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function formatter_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  if ($display['type'] == 'from_field') {
    $instances = field_read_instances(array(
      'entity_type' => $instance['entity_type'],
      'bundle' => $instance['bundle'],
    ));
    foreach ($instances as $formatter_instance) {
      $formatter_instance_field = field_info_field($formatter_instance['field_name']);
      if ($formatter_instance_field['module'] == 'formatter_field' && $formatter_instance['settings']['field_name'] == $field['field_name']) {
        $formatter_items = field_get_items($entity_type, $entity, $formatter_instance['field_name']);
        if (empty($formatter_items) || empty($formatter_items[0]) || !is_array($formatter_items[0])) {
          continue;
        }
        $display = $formatter_items[0] + $display;
        if (is_string($display['settings'])) {
          $display['settings'] = unserialize($display['settings']);
        }
        $formatter_type = field_info_formatter_types($display['type']);
        $display['module'] = $formatter_type['module'];
        list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
        $items = array(
          $id => $items,
        );
        field_default_prepare_view($entity_type, array(
          $id => $entity,
        ), $field, array(
          $id => $instance,
        ), $langcode, $items, $display);
        $element = module_invoke($display['module'], 'field_formatter_view', $entity_type, $entity, $field, $instance, $langcode, $items[$id], $display);
        break;
      }
    }
  }
  return $element;
}