You are here

views_field_formatter.module in Views field formatter 7

Same filename and directory in other branches
  1. 8.2 views_field_formatter.module
  2. 8 views_field_formatter.module

Views field formatter module.

File

views_field_formatter.module
View source
<?php

/**
 * @file
 * Views field formatter module.
 */

/**
 * Implements hook_field_formatter_info().
 */
function views_field_formatter_field_formatter_info() {
  $info = field_info_field_types();
  $formatters = array(
    'views_field_formatter' => array(
      'label' => t('View'),
      'field types' => array_keys($info),
      'settings' => array(
        'views_field_formatter' => array(
          'view' => '',
          'multiple' => FALSE,
        ),
      ),
    ),
  );
  return $formatters;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function views_field_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings']['views_field_formatter'];
  $element = array();

  // Get only enabled views.
  $views = views_get_enabled_views();
  $options = array();
  foreach ($views as $view => $data) {

    // If editing a field from a Views, remove the current view from the list.
    if (isset($form_state['view']) && $form_state['view']->name == $view) {
      continue;
    }

    // Remove Views which don't have the tag 'views_field_formatter'.
    $tags = explode(',', $data->tag);
    if (!in_array('views_field_formatter', $tags)) {
      continue;
    }
    foreach ($data->display as $display => $display_data) {
      $options[$data->human_name][$view . '::' . $display] = $display_data->display_title . ' (' . $view . '::' . $display . ')';
    }
  }
  $element['views_field_formatter'] = array(
    '#type' => 'fieldset',
    '#title' => 'Views field formatter configuration',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  if (!empty($options)) {
    $element['views_field_formatter']['view'] = array(
      '#title' => t('View'),
      '#description' => t('Select the view (<em><a href="@add_view_url">or create a new one</a></em>) that will be used to get the value of the field. Only views with tag <em>views_field_formatter</em> will be visible.', array(
        '@add_view_url' => url('admin/structure/views/add'),
      )),
      '#type' => 'select',
      '#default_value' => $settings['view'],
      '#options' => $options,
    );
    $element['views_field_formatter']['multiple'] = array(
      '#title' => t('Multiple'),
      '#description' => t('If the field is configured as multiple, should we display a view per item ? If selected, there will be one view per item. The arguments passed to that view are in this order: the field item value, the entity id and the item delta.'),
      '#type' => 'checkbox',
      '#default_value' => boolval($settings['multiple']),
    );
    $field_multiple = sprintf(':input[name="fields[%s][settings_edit_form][settings][views_field_formatter][multiple]"]', $field['field_name']);
    $element['views_field_formatter']['implode_character'] = array(
      '#title' => t('Implode with this character'),
      '#description' => t('If it is set, all field values are imploded with this character and sent as one views argument. Empty to disable.'),
      '#type' => 'textfield',
      '#default_value' => $settings['implode_character'],
      '#states' => array(
        'visible' => array(
          $field_multiple => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  else {
    $element['views_field_formatter']['help'] = array(
      '#markup' => t('<p>No available Views were found. <a href="@add_view_url">Create</a> or <a href="@views_templates">add from the views templates</a> a views with tag <em>views_field_formatter</em>.</p>', array(
        '@add_view_url' => url('admin/structure/views/add'),
        '@views_templates' => url('admin/structure/views/add-template'),
      )),
    );
  }
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function views_field_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings']['views_field_formatter'];
  $summary = array();
  list($view, $view_display) = explode('::', $settings['view']);
  $multiple = boolval($settings['multiple']) == TRUE ? 'Enabled' : 'Disabled';
  if (isset($settings['view'])) {
    $summary[] = t('View: @view', array(
      '@view' => $view,
    ));
    $summary[] = t('Display: @display', array(
      '@display' => $view_display,
    ));
    $summary[] = t('Multiple: @multiple', array(
      '@multiple' => $multiple,
    ));
  }
  if ($multiple == 'Enabled') {
    if (!empty($settings['implode_character'])) {
      $summary[] = t('Implode character: @character', array(
        '@character' => $settings['implode_character'],
      ));
    }
  }
  return implode('<br />', $summary);
}

/**
 * Implements hook_field_formatter_view().
 */
function views_field_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  if (!empty($items)) {
    $settings = $display['settings']['views_field_formatter'];
    list($view, $view_display) = explode('::', $settings['view']);
    $field_columns = array_keys($field['columns']);
    $index = array_shift($field_columns);
    $entity_info = entity_get_info($entity_type);
    $key = $entity_info['entity keys']['id'];
    if (isset($field['cardinality']) && $field['cardinality'] != 1 && boolval($settings['multiple']) == TRUE) {
      if (!empty($settings['implode_character'])) {
        $values = array();
        foreach ($items as $item) {
          $values[] = isset($item[$index]) ? $item[$index] : NULL;
        }
        $value = implode($settings['implode_character'], array_filter($values));
        $element[0] = array(
          '#markup' => views_embed_view($view, $view_display, $value, $entity->{$key}, 0),
        );
      }
      else {
        foreach ($items as $delta => $item) {
          $value = isset($item[$index]) ? $item[$index] : NULL;
          $element[$delta] = array(
            '#markup' => views_embed_view($view, $view_display, $value, $entity->{$key}, $delta),
          );
        }
      }
    }
    else {
      $value = isset($items[0][$index]) ? $items[0][$index] : NULL;
      $element[0] = array(
        '#markup' => views_embed_view($view, $view_display, $value, $entity->{$key}, 0),
      );
    }
  }
  return $element;
}

/**
 * Implements hook_views_api().
 */
function views_field_formatter_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'views_field_formatter') . '/views',
  );
}

/**
 * Boolval is only available for PHP >= 5.5, I didn't know that.
 *
 * Anyway, here's the fix.
 */
if (!function_exists('boolval')) {

  /**
   * Boolval function.
   */
  function boolval($val) {
    return (bool) $val;
  }
}