? .bzr Index: geshifield.info =================================================================== RCS file: geshifield.info diff -N geshifield.info --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ geshifield.info 5 Apr 2008 23:36:46 -0000 @@ -0,0 +1,5 @@ +; $Id$ +name = "GeSHi CCK field" +description = "Provides a CCK field for source code with syntax highlighting" +dependencies = content +package = "Filters" Index: geshifield.module =================================================================== RCS file: geshifield.module diff -N geshifield.module --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ geshifield.module 5 Apr 2008 23:36:46 -0000 @@ -0,0 +1,238 @@ + array('label' => 'Source code text field with GeSHi syntax highlighting'), + ); +} + +/** + * Implementation of hook_field_settings(). + */ +function geshifield_field_settings($op, $field) { + switch ($op) { + case 'form': + // no options or settings for now (@todo) + return array(); + + case 'validate': + // nothing to validate for now (@todo) + return; + + case 'save': + // nothing to save for now (@todo) + return array(); + + case 'database columns': + return array( + 'sourcecode' => array( + 'type' => 'longtext', + 'not null' => TRUE, + 'default' => "''", + 'sortable' => TRUE, + ), + 'language' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => "''", + 'sortable' => TRUE, + ), + ); + + return; + + case 'filters': + // no views filters for now (@todo) + return array(); + } +} + +/** + * Implementation of hook_field(). + */ +function geshifield_field($op, &$node, $field, &$items, $teaser, $page) { + // Nothing to do here + // Typically only the validition operation is needed in this hook + // but for geshifield even this is not needed +} + +/** + * Implementation of hook_field_formatter_info(). + */ +function geshifield_field_formatter_info() { + return array( + 'default' => array( + 'label' => 'Default', + 'field types' => array('geshifield'), + ), + 'nohighlighting' => array( + 'label' => 'No syntax highlighting', + 'field types' => array('geshifield'), + ), + 'trimmed' => array( + 'label' => t('Trimmed'), + 'field types' => array('geshifield'), + ), + ); +} + +/** + * Implementation of hook_field_formatter(). + */ +function geshifield_field_formatter($field, $item, $formatter, $node) { + if (empty($item['sourcecode'])) { + return ''; + } + require_once('geshifilter.pages.inc'); + + switch ($formatter) { + case 'default': + return geshifilter_geshi_process($item['sourcecode'], $item['language']); + + case 'nohighlighting': + return geshifilter_geshi_process($item['sourcecode'], 'text'); + + case 'trimmed': + return geshifilter_geshi_process(node_teaser($item['sourcecode'], NULL), $item['language']); + } + + return $text; +} + +/** + * Implementation of hook_widget_info(). + */ +function geshifield_widget_info() { + return array( + 'geshifield_textfield' => array( + 'label' => 'Text Field', + 'field types' => array('geshifield'), + ), + ); +} + +/** + * Implementation of hook_widget_settings(). + */ +function geshifield_widget_settings($op, $widget) { + switch ($op) { + case 'form': + $form = array(); + $form['rows'] = array( + '#type' => 'textfield', + '#title' => t('Rows'), + '#default_value' => $widget['rows'] ? $widget['rows'] : 20, + '#required' => TRUE, + ); + return $form; + + case 'validate': + if (!is_numeric($widget['rows']) || intval($widget['rows']) != $widget['rows'] || $widget['rows'] <= 0) { + form_set_error('rows', t('"Rows" must be a positive integer.')); + } + break; + + case 'save': + return array('rows'); + } +} + + + + +/** + * Implementation of hook_widget(). + */ +function geshifield_widget($op, &$node, $field, &$items) { + + switch ($op) { + case 'form': + $form = array(); + + $form[$field['field_name']] = array('#tree' => TRUE); + + $enabled_languages = _geshifilter_get_enabled_languages(); + + if ($field['multiple']) { + // handle the multiple values case + $form[$field['field_name']]['#type'] = 'fieldset'; + $form[$field['field_name']]['#description'] = t($field['widget']['description']); + // show the current values (and count them with $delta) + $delta = 0; + foreach ($items as $data) { + if ($data['sourcecode']) { + $form[$field['field_name']][$delta]['sourcecode'] = array( + '#type' => 'textarea', + '#title' => t($field['widget']['label']), + '#default_value' => $data['sourcecode'], + '#required' => ($delta == 0) ? $field['required'] : FALSE, + '#rows' => $field['widget']['rows'], + ); + $form[$field['field_name']][$delta]['language'] = array( + '#type' => 'select', + '#title' => t('Syntax highlighting mode'), + '#default_value' => $data['language'], + '#options' => $enabled_languages, + '#description' => ($delta == 0) ? t('Select the syntax highlighting mode to use for the source code.') : '', + ); + $delta++; + } + } + // add two extra textareas for extra values + foreach (range($delta, $delta + 2) as $delta) { + $form[$field['field_name']][$delta]['sourcecode'] = array( + '#type' => 'textarea', + '#title' => t($field['widget']['label']), + '#default_value' => '', + '#required' => ($delta == 0) ? $field['required'] : FALSE, + '#rows' => $field['widget']['rows'], + ); + $form[$field['field_name']][$delta]['language'] = array( + '#type' => 'select', + '#title' => t('Syntax highlighting mode'), + '#default_value' => $data['language'], + '#options' => $enabled_languages, + '#description' => ($delta == 0) ? t('Select the syntax highlighting mode to use for the source code.') : '', + ); + } + } + else { + $form[$field['field_name']][0]['sourcecode'] = array( + '#type' => 'textarea', + '#title' => t($field['widget']['label']), + '#default_value' => $items[0]['sourcecode'], + '#required' => $field['required'], + '#rows' => $field['widget']['rows'], + '#description' => t($field['widget']['description']), + '#weight' => $field['widget']['weight'], + ); + $form[$field['field_name']][0]['language'] = array( + '#type' => 'select', + '#title' => t('Syntax highlighting mode'), + '#default_value' => $items[0]['language'], + '#options' => $enabled_languages, + '#description' => t('Select the syntax highlighting mode to use for the source code.'), + ); + } + return $form; + + case 'process form values': + // Don't save empty fields except the first value + foreach ($items as $delta => $item) { + if ($item['sourcecode'] == '' && $delta > 0) { + unset($items[$delta]); + } + } + break; + } +}