Commit ce351333 authored by JonBob's avatar JonBob

Addition of new content_format() function to prepare for output choices in Views.

parent 26b96ef2
......@@ -14,8 +14,8 @@ function content_install() {
description mediumtext NOT NULL,
help mediumtext NOT NULL,
title_label varchar(255) NOT NULL default '',
PRIMARY KEY (type_name)
) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
PRIMARY KEY (type_name)
) /*!40100 DEFAULT CHARACTER SET utf8 */;");
db_query("CREATE TABLE {node_field} (
field_name varchar(32) NOT NULL default '',
type varchar(127) NOT NULL default '',
......@@ -23,8 +23,8 @@ function content_install() {
required int NOT NULL default '0',
multiple int NOT NULL default '0',
db_storage int NOT NULL default '0',
PRIMARY KEY (field_name)
) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
PRIMARY KEY (field_name)
) /*!40100 DEFAULT CHARACTER SET utf8 */;");
db_query("CREATE TABLE {node_field_instance} (
field_name varchar(32) NOT NULL default '',
type_name varchar(32) NOT NULL default '',
......@@ -33,8 +33,8 @@ function content_install() {
widget_type varchar(32) NOT NULL default '',
widget_settings mediumtext NOT NULL,
description mediumtext NOT NULL,
PRIMARY KEY (field_name,type_name)
) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
PRIMARY KEY (field_name,type_name)
) /*!40100 DEFAULT CHARACTER SET utf8 */;");
break;
case 'pgsql':
......@@ -44,7 +44,7 @@ function content_install() {
description text NOT NULL,
help text NOT NULL,
title_label varchar(255) NOT NULL default '',
PRIMARY KEY (type_name)
PRIMARY KEY (type_name)
)");
db_query("CREATE TABLE {node_field} (
field_name varchar(32) NOT NULL default '',
......@@ -53,7 +53,7 @@ function content_install() {
required integer NOT NULL default '0',
multiple integer NOT NULL default '0',
db_storage integer NOT NULL default '0',
PRIMARY KEY (field_name)
PRIMARY KEY (field_name)
)");
db_query("CREATE TABLE {node_field_instance} (
field_name varchar(32) NOT NULL default '',
......@@ -63,7 +63,7 @@ function content_install() {
widget_type varchar(32) NOT NULL default '',
widget_settings text NOT NULL,
description text NOT NULL,
PRIMARY KEY (field_name,type_name)
PRIMARY KEY (field_name,type_name)
)");
break;
}
......
......@@ -677,23 +677,36 @@ function _content_type_info($reset = FALSE) {
);
foreach (module_list() as $module) {
$module_types = module_invoke($module, 'field_info');
if ($module_types) {
foreach ($module_types as $name => $field_info) {
$module_field_types = module_invoke($module, 'field_info');
if ($module_field_types) {
foreach ($module_field_types as $name => $field_info) {
$info['field types'][$name] = $field_info;
$info['field types'][$name]['module'] = $module;
$info['field types'][$name]['formatters'] = array();
}
}
$module_types = module_invoke($module, 'widget_info');
if ($module_types) {
foreach ($module_types as $name => $widget_info) {
$module_widgets = module_invoke($module, 'widget_info');
if ($module_widgets) {
foreach ($module_widgets as $name => $widget_info) {
$info['widget types'][$name] = $widget_info;
$info['widget types'][$name]['module'] = $module;
}
}
}
foreach (module_list() as $module) {
$module_formatters = module_invoke($module, 'field_formatter_info');
if ($module_formatters) {
foreach ($module_formatters as $name => $formatter_info) {
foreach ($formatter_info['field types'] as $field_type) {
$info['field types'][$field_type]['formatters'][$name] = $formatter_info;
$info['field types'][$field_type]['formatters'][$name]['module'] = $module;
}
}
}
}
$field_result = db_query('SELECT * FROM {node_field} nf');
while ($field = db_fetch_array($field_result)) {
$global_settings = $field['global_settings'] ? unserialize($field['global_settings']) : array();
......@@ -823,6 +836,36 @@ function content_transpose_array_rows_cols($array) {
return $result;
}
/**
* Format a field item for display.
*
* @param $field
* Either a field array or the name of the field.
* @param $item
* The field item to be formatted (such as $node->field_foo[0]).
* @param $formatter
* The name of the formatter to use.
* @param $node
* Optionally, the containing node object for context purposes.
*
* @return
* A string containing the contents of the field item sanitized for display.
* It will have been passed through the necessary check_plain() or check_markup()
* functions as necessary.
*/
function content_format($field, $item, $formatter = 'default', $node = NULL) {
if (!is_array($field)) {
$field = content_fields($field);
}
$field_types = _content_field_types();
$formatters = $field_types[$field['type']]['formatters'];
if (!in_array($formatter, $formatters)) {
$formatter = 'default';
}
return module_invoke($formatters[$formatter]['module'], 'field_formatter', $field, $item, $formatter, $node);
}
/**
* Format an individual field for display.
*
......
......@@ -88,19 +88,12 @@ function content_views_tables() {
function content_views_field_handler($field_info, $field_data, $value, $data) {
$field = $field_info['content_field'];
$node_field_item = array();
$item = array();
foreach ($field_info['content_db_info']['columns'] as $column => $attributes) {
$view_column_name = 'node_data_'. $field['field_name'] .'_'. $attributes['column'];
$node_field_item[$column] = $data->$view_column_name;
$item[$column] = $data->$view_column_name;
}
$function = $field_info['content_field_module'] .'_field_view_item';
if (function_exists($function)) {
$result = $function($field, $node_field_item);
}
else {
$result = implode("\n", $node_field_item);
}
return $result;
return content_format($field, $item);
}
/**
......@@ -177,28 +170,17 @@ function content_views_argument_handler($op, &$query, $argtype, $arg = '') {
break;
case 'link':
$field_types = _content_field_types();
$module = $field_types[$field['type']]['module'];
$item = array();
foreach ($db_info['columns'] as $column => $attributes) {
$view_column_name = $attributes['column'];
$item[$column] = $query->$view_column_name;
}
$view = module_invoke($module, 'field_view_item', $field, $item);
// Strip any links that appear in the view, so that the new link we create works here.
$view = preg_replace('!</?a(\s+[^>]*)?>!i', '', $view);
return l($view, $arg .'/'. $query->$main_column['column'], array(), NULL, NULL, FALSE, TRUE);
return l(content_format($field, $item, 'plain'), $arg .'/'. $query->$main_column['column'], array(), NULL, NULL, FALSE, TRUE);
case 'title':
$field_types = _content_field_types();
$module = $field_types[$field['type']]['module'];
$item = array(key($db_info['columns']) => $query);
return module_invoke($module, 'field_view_item', $field, $item);
return content_format($field, $item);
}
}
......@@ -69,30 +69,53 @@ function nodereference_field_settings($op, $field) {
/**
* Implementation of hook_field().
*/
function nodereference_field($op, &$node, $field, &$node_field, $teaser, $page) {
function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'view':
foreach ($node_field as $delta => $item) {
$node_field[$delta]['view'] = nodereference_field_view_item($field, $item, $node);
foreach ($items as $delta => $item) {
$items[$delta]['view'] = content_format($field, $item, 'default', $node);
}
return theme('field', $node, $field, $node_field, $teaser, $page);
return theme('field', $node, $field, $items, $teaser, $page);
}
}
/**
* Implementation of hook_field_view_item().
* Implementation of hook_field_format_info().
*/
function nodereference_field_view_item($field, $node_field_item) {
if (!isset($node_field_item['nid'])) {
return '';
function nodereference_field_formatter_info() {
return array(
'default' => array(
'label' => 'Default',
'field types' => array('nodereference'),
),
'plain' => array(
'label' => 'Plain text',
'field types' => array('nodereference'),
),
);
}
/**
* Implementation of hook_field_formatter().
*/
function nodereference_field_formatter($field, $item, $formatter, $node) {
$text = '';
if (isset($item['nid'])) {
$referenced_node = node_load($item['nid']);
if ($referenced_node) {
$text = l($referenced_node->title, 'node/'. $referenced_node->nid);
}
}
$referenced_node = node_load($node_field_item['nid']);
if ($referenced_node) {
return l($referenced_node->title, 'node/'. $referenced_node->nid);
switch ($formatter) {
case 'plain':
return strip_tags($text);
default:
return $text;
}
}
/**
* Implementation of hook_widget_info().
*/
......
......@@ -105,21 +105,21 @@ function number_field_settings($op, $field) {
/**
* Implementation of hook_field().
*/
function number_field($op, &$node, $field, &$node_field, $teaser, $page) {
function number_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'view':
foreach ($node_field as $delta => $item) {
$node_field[$delta]['view'] = number_field_view_item($field, $item);
foreach ($items as $delta => $item) {
$items[$delta]['view'] = content_format($field, $item, 'default', $node);
}
return theme('field', $node, $field, $node_field, $teaser, $page);
return theme('field', $node, $field, $items, $teaser, $page);
case 'validate':
$allowed_values = explode("\n", $field['allowed_values']);
$allowed_values = array_map('trim', $allowed_values);
$allowed_values = array_filter($allowed_values, 'strlen');
if (is_array($node_field)) {
foreach ($node_field as $delta => $item) {
if (is_array($items)) {
foreach ($items as $delta => $item) {
if ($item['value'] != '') {
if (is_numeric($field['min']) && $item['value'] < $field['min']) {
form_set_error($field['field_name'], t('The value of %name may be no smaller than %min.', array('%name' => t($field['widget']['label']), '%min' => $field['min'])));
......@@ -138,10 +138,22 @@ function number_field($op, &$node, $field, &$node_field, $teaser, $page) {
}
/**
* Implementation of hook_field_view_item().
* Implementation of hook_field_format_info().
*/
function number_field_formatter_info() {
return array(
'default' => array(
'label' => 'Default',
'field types' => array('number_integer', 'number_decimal'),
),
);
}
/**
* Implementation of hook_field_formatter().
*/
function number_field_view_item($field, $node_field_item) {
return check_plain($node_field_item['value']);
function number_field_formatter($field, $item, $formatter, $node) {
return check_plain($item['value']);
}
......
......@@ -104,21 +104,21 @@ function text_field_settings($op, $field) {
/**
* Implementation of hook_field().
*/
function text_field($op, &$node, $field, &$node_field, $teaser, $page) {
function text_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'view':
foreach ($node_field as $delta => $item) {
$node_field[$delta]['view'] = text_field_view_item($field, $item, $node);
foreach ($items as $delta => $item) {
$items[$delta]['view'] = content_format($field, $item, 'default', $node);
}
return theme('field', $node, $field, $node_field, $teaser, $page);
return theme('field', $node, $field, $items, $teaser, $page);
case 'validate':
$allowed_values = explode("\n", $field['allowed_values']);
$allowed_values = array_map('trim', $allowed_values);
$allowed_values = array_filter($allowed_values, 'strlen');
if (is_array($node_field)) {
foreach ($node_field as $delta => $item) {
if (is_array($items)) {
foreach ($items as $delta => $item) {
if ($item['value'] != '') {
if (count($allowed_values) && !in_array($item['value'], $allowed_values)) {
form_set_error($field['field_name'], t('Illegal value for %name.', array('%name' => t($field['widget']['label']))));
......@@ -131,27 +131,55 @@ function text_field($op, &$node, $field, &$node_field, $teaser, $page) {
}
/**
* Implementation of hook_field_view_item().
* Implementation of hook_field_format_info().
*/
function text_field_formatter_info() {
return array(
'default' => array(
'label' => 'Default',
'field types' => array('text'),
),
'plain' => array(
'label' => 'Plain text',
'field types' => array('text'),
),
'trimmed' => array(
'label' => 'Trimmed',
'field types' => array('text'),
),
);
}
/**
* Implementation of hook_field_formatter().
*
* The extra $node argument is necessary so that filter access can be checked on
* The $node argument is necessary so that filter access can be checked on
* node preview.
*/
function text_field_view_item($field, $node_field_item, $node = NULL) {
if (!isset($node_field_item['value'])) {
function text_field_formatter($field, $item, $formatter, $node) {
if (!isset($item['value'])) {
return '';
}
// check permissions in check_markup if previewing.
$preview = ($node && isset($node->in_preview));
if ($field['text_processing']) {
return check_markup($node_field_item['value'], $node_field_item['format'], $preview);
$text = check_markup($item['value'], $item['format'], is_null($node) || isset($node->in_preview));
}
else {
return check_plain($node_field_item['value']);
$text = check_plain($item['value']);
}
switch ($formatter) {
case 'plain':
return strip_tags($text);
case 'trimmed':
return node_teaser($text, $field['text_processing'] ? $item['format'] : NULL);
default:
return $text;
}
}
/**
* Implementation of hook_widget_info().
*/
......@@ -194,7 +222,7 @@ function text_widget_settings($op, $widget) {
/**
* Implementation of hook_widget().
*/
function text_widget($op, &$node, $field, &$node_field) {
function text_widget($op, &$node, $field, &$items) {
switch ($op) {
case 'form':
$form = array();
......@@ -205,7 +233,7 @@ function text_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']]['#type'] = 'fieldset';
$form[$field['field_name']]['#title'] = t($field['widget']['label']);
$delta = 0;
foreach ($node_field as $data) {
foreach ($items as $data) {
if ($data['value']) {
if ($field['widget']['rows'] == 1) {
$form[$field['field_name']][$delta]['value'] = array(
......@@ -255,7 +283,7 @@ function text_widget($op, &$node, $field, &$node_field) {
);
}
if ($field['text_processing']) {
$form[$field['field_name']][$delta]['format'] = filter_form($node_field[$delta]['format'], $form[$field['field_name']][$delta]['value']['#weight'] + 1, array($field['field_name'], $delta, 'format'));
$form[$field['field_name']][$delta]['format'] = filter_form($items[$delta]['format'], $form[$field['field_name']][$delta]['value']['#weight'] + 1, array($field['field_name'], $delta, 'format'));
}
}
}
......@@ -264,7 +292,7 @@ function text_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']][0]['value'] = array(
'#type' => 'textfield',
'#title' => t($field['widget']['label']),
'#default_value' => isset($node_field[0]['value']) ? $node_field[0]['value'] : '',
'#default_value' => isset($items[0]['value']) ? $items[0]['value'] : '',
'#required' => $field['required'],
'#description' => $field['widget']['description'],
'#maxlength' => $field['max_length'] ? $field['max_length'] : '',
......@@ -275,7 +303,7 @@ function text_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']][0]['value'] = array(
'#type' => 'textarea',
'#title' => t($field['widget']['label']),
'#default_value' => $node_field[0]['value'],
'#default_value' => $items[0]['value'],
'#required' => $field['required'],
'#rows' => $field['widget']['rows'],
'#description' => $field['widget']['description'],
......@@ -283,7 +311,7 @@ function text_widget($op, &$node, $field, &$node_field) {
);
}
if ($field['text_processing']) {
$form[$field['field_name']][0]['format'] = filter_form($node_field[0]['format'], $form[$field['field_name']][0]['value']['#weight'] + 1, array($field['field_name'], 0, 'format'));
$form[$field['field_name']][0]['format'] = filter_form($items[0]['format'], $form[$field['field_name']][0]['value']['#weight'] + 1, array($field['field_name'], 0, 'format'));
}
}
return $form;
......@@ -291,9 +319,9 @@ function text_widget($op, &$node, $field, &$node_field) {
case 'process form values':
// Don't save empty fields.
if ($field['multiple']) {
foreach ($node_field as $delta => $item) {
foreach ($items as $delta => $item) {
if ($item['value'] == '') {
unset($node_field[$delta]);
unset($items[$delta]);
}
}
}
......
......@@ -66,30 +66,53 @@ function userreference_field_settings($op, $field) {
/**
* Implementation of hook_field().
*/
function userreference_field($op, &$node, $field, &$node_field, $teaser, $page) {
function userreference_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'view':
foreach ($node_field as $delta => $item) {
$node_field[$delta]['view'] = userreference_field_view_item($field, $item, $node);
foreach ($items as $delta => $item) {
$items[$delta]['view'] = content_format($field, $item, 'default', $node);
}
return theme('field', $node, $field, $node_field, $teaser, $page);
return theme('field', $node, $field, $items, $teaser, $page);
}
}
/**
* Implementation of hook_field_view_item().
* Implementation of hook_field_format_info().
*/
function userreference_field_view_item($field, $node_field_item) {
if (isset($node_field_item['uid'])) {
$referenced_user = user_load(array('uid' => $node_field_item['uid']));
function userreference_field_formatter_info() {
return array(
'default' => array(
'label' => 'Default',
'field types' => array('userreference'),
),
'plain' => array(
'label' => 'Plain text',
'field types' => array('userreference'),
),
);
}
/**
* Implementation of hook_field_formatter().
*/
function userreference_field_formatter($field, $item, $formatter, $node) {
$text = '';
if (isset($item['uid'])) {
$referenced_user = user_load(array('uid' => $item['uid']));
if ($referenced_user) {
return theme('username', $referenced_user);
$text = theme('username', $referenced_user);
}
}
return '';
switch ($formatter) {
case 'plain':
return strip_tags($text);
default:
return $text;
}
}
/**
* Implementation of hook_widget_info().
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment