You are here

views_customfield.module in Views Custom Field 5

Same filename and directory in other branches
  1. 6 views_customfield.module

File

views_customfield.module
View source
<?php

/**
 * Implementation of hook_views_tables()
 *
 * @return array
 */
function views_customfield_views_tables() {
  $tables['views_customfield'] = array(
    'fields' => array(
      'text' => array(
        'name' => t('Customfield: PHP code'),
        'help' => t('Display row-specific custom text using PHP.'),
        'sortable' => FALSE,
        'notafield' => TRUE,
        'handler' => 'views_customfield_handler_field_phpcode',
        /**
         * @see http://drupal.org/node/99792#option (complex option fields)
         */
        'option' => array(
          'value' => array(
            '#type' => 'textarea',
            '#title' => t('Value'),
            '#description' => t('The text that should be displayed.') . ' ' . t('Include &lt;?php ?&gt; delimiters when using PHP code.') . ' ' . t('Available variables:') . '<br/>' . t('$data: contains the retrieved record from the database (e.g. $data->nid).') . '<br/>' . t('$static: can be used to store reusable data per row.'),
            '#rows' => 5,
          ),
          '#type' => 'views_customfield_option',
          '#process' => array(
            '_views_customfield_option_process' => array(),
          ),
          '#after_build' => array(
            '_views_customfield_option_after_build',
          ),
        ),
      ),
      'rownumber' => array(
        'name' => t('Customfield: Rownumber'),
        'help' => t('Display rownumber.'),
        'sortable' => FALSE,
        'notafield' => TRUE,
        'handler' => 'views_customfield_handler_field_rownumber',
        'option' => 'string',
      ),
    ),
  );
  if (module_exists('validator_phpcode')) {
    $tables['views_customfield']['fields']['text']['validate'] = 'views_customfield_handler_field_phpcode_validate';
  }
  return $tables;
}

/**
 * The #process takes the serialized #default_value and feeds
 * the forms beneath it.
 *
 * @param array $element
 * @return array
 */
function _views_customfield_option_process($element) {
  $values = unserialize($element['#default_value']);
  if (!is_array($values)) {

    // set default values for options that have no stored value.
    $values = array(
      'value' => '',
    );
  }
  $element['value']['#default_value'] = $values['value'];
  return $element;
}

/**
 * Put the value back.
 *
 * @param array $element
 * @return array
 */
function _views_customfield_option_after_build($element) {
  $values = array();
  $values['value'] = $element['value']['#value'];
  $element['#value'] = serialize($values);
  form_set_value($element, $element['#value']);
  return $element;
}

/**
 * Creates output for the views_customfield_option form-element type.
 * Contains a small hack to improve the UI when editing a view.
 * 
 * @return string
 */
function theme_views_customfield_option($element) {
  return '</td><td colspan="7"></td></tr><tr><td colspan="4">' . $element['#children'];
}

/**
 * Validate field settings.
 * 
 * Make sure PHP code doesn't contain any errors.
 *
 * @see http://drupal.org/node/99565#fields
 * 
 * @param array $fielddata
 * @param array $view
 * @param array $form
 */
function views_customfield_handler_field_phpcode_validate($fielddata, $view, $form) {
  $options = unserialize($fielddata['options']);
  $code = $options['value'];
  if ($error = validator_phpcode_has_errors($code)) {

    // TODO not very efficient, but $fielddata doesn't contain position-info
    // Some other way possible?
    for ($i = 0; $i < $view['field']['count']; $i++) {
      if ($form['field'][$i]['fullname']['#value'] == 'views_customfield.text' && $form['field'][$i]['options']['value']['#value'] == $code) {
        $msg = t('Views Custom field') . ': ' . $error[0] . ' on line ' . $error[1];
        form_error($form['field'][$i]['options']['value'], $msg);
        return;
      }
    }
  }
}

/**
 * Views field handler: Display row-specific custom text using PHP.
 *
 * @param array $fieldinfo
 * @param array $fielddata
 * @param string $value
 * @param object $data
 * @return string
 */
function views_customfield_handler_field_phpcode($fieldinfo, $fielddata, $value, $data) {
  static $static;

  // TODO should be possible to unserialize just once per view/field
  $options = unserialize($fielddata['options']);
  return views_customfield_handler_field_phpcode_eval($options['value'], $static[$fielddata['position']], $data);
}

/**
 * Evaluate a string of PHP code. Mostly copied from drupal_eval().
 *
 * @param string $code
 * @param mixed $static
 * @param array $data
 * @return string
 */
function views_customfield_handler_field_phpcode_eval($code, &$static, $data) {
  global $theme_path, $theme_info, $conf;

  // Store current theme path.
  $old_theme_path = $theme_path;

  // Restore theme_path to the theme, as long as drupal_eval() executes,
  // so code evaluted will not see the caller module as the current theme.
  // If theme info is not initialized get the path from theme_default.
  if (!isset($theme_info)) {
    $theme_path = drupal_get_path('theme', $conf['theme_default']);
  }
  else {
    $theme_path = dirname($theme_info->filename);
  }
  ob_start();
  print eval('?>' . $code);
  $output = ob_get_contents();
  ob_end_clean();

  // Recover original theme path.
  $theme_path = $old_theme_path;
  return $output;
}

/**
 * Views field handler: Display rownumber.
 *
 * @param array $fieldinfo
 * @param array $fielddata
 * @param string $value
 * @param object $data
 * @return string
 */
function views_customfield_handler_field_rownumber($fieldinfo, $fielddata, $value, $data) {
  global $current_view;
  static $i = array();
  if (!$i[$current_view->name . $fielddata['position']]) {
    $i[$current_view->name . $fielddata['position']] = 0;
  }
  return $i[$current_view->name . $fielddata['position']]++;
}

Functions

Namesort descending Description
theme_views_customfield_option Creates output for the views_customfield_option form-element type. Contains a small hack to improve the UI when editing a view.
views_customfield_handler_field_phpcode Views field handler: Display row-specific custom text using PHP.
views_customfield_handler_field_phpcode_eval Evaluate a string of PHP code. Mostly copied from drupal_eval().
views_customfield_handler_field_phpcode_validate Validate field settings.
views_customfield_handler_field_rownumber Views field handler: Display rownumber.
views_customfield_views_tables Implementation of hook_views_tables()
_views_customfield_option_after_build Put the value back.
_views_customfield_option_process The #process takes the serialized #default_value and feeds the forms beneath it.