Commit 9847701b authored by Nathan Haug's avatar Nathan Haug
Browse files

Applied patch provided by KarenS to use the current CCK DB scheme. #92804....

Applied patch provided by KarenS to use the current CCK DB scheme. #92804. Awesome! Thanks again Karen.
parent 96a22328
...@@ -5,32 +5,41 @@ ...@@ -5,32 +5,41 @@
* Implementation of hook_install(). * Implementation of hook_install().
*/ */
function link_install() { function link_install() {
switch ($GLOBALS['db_type']) { return;
case 'mysql': }
case 'mysqli':
db_query("CREATE TABLE {node_field_link_data} ( /**
nid int unsigned NOT NULL default '0', * Removed link.module created tables, move data to content.module tables
vid int unsigned NOT NULL default '0', */
field_name varchar(32) NOT NULL default '', function link_update_1() {
delta int unsigned NOT NULL default '0', $ret = array();
field_url varchar(255) NOT NULL default '',
field_title varchar(255) NOT NULL default '',
attributes text NOT NULL default '',
PRIMARY KEY (vid,field_name,delta)
) /*!40100 DEFAULT CHARACTER SET utf8 */;");
break;
case 'pgsql': include_once(drupal_get_path('module', 'content') .'/content.module');
db_query("CREATE TABLE {node_field_link_data} ( include_once(drupal_get_path('module', 'content') .'/content_admin.inc');
nid integer NOT NULL default '0' CHECK (nid >= 0),
vid integer NOT NULL default '0' CHECK (nid >= 0), $fields = content_fields();
field_name varchar(32) NOT NULL default '',
delta integer NOT NULL default '0' CHECK (nid >= 0), foreach ($fields as $field) {
field_url varchar(255) NOT NULL default '', switch ($field['type']) {
field_title varchar(255) NOT NULL default '', case 'link':
attributes text NOT NULL default '', $columns = array(
PRIMARY KEY (vid,field_name,delta) 'url' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''"),
)"); 'title' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''"),
break; 'attributes' => array('type' => 'mediumtext', 'not null' => TRUE, 'default' => "''"),
);
content_alter_db_field(array(), array(), $field, $columns);
$db_info = content_database_info($field);
if ($field['multiple']) {
$ret[] = update_sql('INSERT INTO {'. $db_info['table'] .'} (vid, delta, nid, '. $field['field_name'] .'_url, '. $field['field_name'] .'_title, '. $field['field_name'] ."_attributes) SELECT vid, delta, nid, field_url, field_title, attributes FROM {node_field_link_data} WHERE field_name = '". $field['field_name'] ."'");
}
else {
$ret[] = update_sql('INSERT INTO {'. $db_info['table'] .'} (vid, nid, '. $field['field_name'] .'_url, '. $field['field_name'] .'_title, '. $field['field_name'] ."_attributes) SELECT vid, nid, field_url, field_title, attributes FROM {node_field_link_data} WHERE field_name = '". $field['field_name'] ."'");
}
}
} }
$ret[] = update_sql('DROP TABLE {node_field_link_data}');
db_query('DELETE FROM {cache}');
return $ret;
} }
...@@ -74,61 +74,37 @@ function link_field_settings($op, $field) { ...@@ -74,61 +74,37 @@ function link_field_settings($op, $field) {
case 'save': case 'save':
return array('attributes', 'title'); return array('attributes', 'title');
case 'database columns':
return array(
'url' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''"),
'title' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''"),
'attributes' => array('type' => 'mediumtext', 'not null' => TRUE, 'default' => "''"),
);
case 'filters':
return array(
'default' => array(
'operator' => 'views_handler_operator_like',
'handler' => 'views_handler_filter_like',
),
);
} }
} }
/** /**
* Implementation of hook_field(). * Implementation of hook_field().
*/ */
function link_field($op, &$node, $field, &$node_field, $teaser, $page) { function link_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) { switch ($op) {
case 'load':
switch($field['type']) {
case 'link':
$result = db_query("SELECT field_url, field_title, attributes FROM {node_field_link_data} WHERE vid = %d AND field_name = '%s' ORDER BY delta", $node->vid, $field['field_name']);
while ($value = db_fetch_array($result)) {
$values[] = array('value' => array('link' => $value['field_url'], 'title' => $value['field_title'], 'attributes' => unserialize($value['attributes'])));
}
$additions = array($field['field_name'] => $values);
break;
}
return $additions;
case 'view': case 'view':
foreach ($node_field as $delta => $item) { foreach ($items as $delta => $item) {
$node_field[$delta]['view'] = _link_field_view($field, $item['value'], $item, $node); $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 'insert': break;
switch($field['type']) {
case 'link':
foreach ($node_field as $delta => $item) {
if ($item['value']['link']) {
db_query("INSERT INTO {node_field_link_data} (nid, vid, field_name, delta, field_url, field_title, attributes) VALUES (%d, %d, '%s', %d, '%s', '%s', '%s')", $node->nid, $node->vid, $field['field_name'], $delta, $item['value']['link'], $item['value']['title'], serialize($item['value']['attributes']));
}
}
break;
}
return;
case 'update':
// Delete and insert, rather than update, in case a field was added.
switch($field['type']) {
case 'link':
db_query("DELETE FROM {node_field_link_data} WHERE vid = %d AND field_name = '%s'", $node->vid, $field['field_name']);
link_field('insert', $node, $field, $node_field, $teaser, $page);
break;
}
return;
case 'delete':
// Delete using nid rather than vid to purge all revisions.
switch($field['type']) {
case 'link':
db_query("DELETE FROM {node_field_link_data} WHERE nid = %d AND field_name = '%s'", $node->nid, $field['field_name']);
break;
}
return;
} }
} }
...@@ -162,7 +138,7 @@ function link_widget($op, &$node, $field, &$node_field) { ...@@ -162,7 +138,7 @@ function link_widget($op, &$node, $field, &$node_field) {
$delta = 0; $delta = 0;
// Render link fields for all the entered values // Render link fields for all the entered values
foreach ($node_field as $data) { foreach ($node_field as $data) {
if ($data['value']['link']) { if ($data['url']) {
_link_widget_form($form[$field['field_name']][$delta], $field, $data, $delta); _link_widget_form($form[$field['field_name']][$delta], $field, $data, $delta);
$delta++; $delta++;
} }
...@@ -173,29 +149,24 @@ function link_widget($op, &$node, $field, &$node_field) { ...@@ -173,29 +149,24 @@ function link_widget($op, &$node, $field, &$node_field) {
} }
} // end if multiple } // end if multiple
else { else {
// Convert an old 'singe' value field into the new array based format
if ($node_field['value']) {
$node_field[0]['value'] = $node_field['value'];
unset($node_field['value']);
}
_link_widget_form($form[$field['field_name']][0], $field, $node_field[0]); _link_widget_form($form[$field['field_name']][0], $field, $node_field[0]);
} }
return $form; return $form;
case 'validate': case 'validate':
foreach($node_field as $delta => $value) { foreach($node_field as $delta => $value) {
if ($value['value']['link']) { if ($value['url']) {
// Validate the link // Validate the link
if (!link_validate_link($value['value']['link'])) { if (!link_validate_link($value['url'])) {
form_set_error($field['field_name'] .']['. $delta. '][value][link', t('Not a valid URL.')); form_set_error($field['field_name'] .']['. $delta. '][value][url', t('Not a valid URL.'));
} }
// Require a title for the link if necessary // Require a title for the link if necessary
elseif ($field['title'] == 'required' && strlen(trim($value['value']['title'])) == 0) { elseif ($field['title'] == 'required' && strlen(trim($value['title'])) == 0) {
form_set_error($field['field_name'] .']['. $delta. '][value][title', t('Titles are required for all links.')); form_set_error($field['field_name'] .']['. $delta. '][value][title', t('Titles are required for all links.'));
} }
} }
// Require a link if we have a title // Require a link if we have a title
elseif (strlen($value['value']['title']) > 0) { elseif (strlen($value['title']) > 0) {
form_set_error($field['field_name'] .']['. $delta. '][value][link', t('You cannot enter a title without a link.')); form_set_error($field['field_name'] .']['. $delta. '][value][link', t('You cannot enter a title without a link.'));
} }
} }
...@@ -217,32 +188,34 @@ function link_widget($op, &$node, $field, &$node_field) { ...@@ -217,32 +188,34 @@ function link_widget($op, &$node, $field, &$node_field) {
*/ */
function _link_widget_form (&$form_item, $field, $node_field, $delta = 0) { function _link_widget_form (&$form_item, $field, $node_field, $delta = 0) {
$form_item['value'] = array(
$form_item = array(
'#tree' => true, '#tree' => true,
'#weigth' => $field['widget']['weight'], // Add a microweight to keep fields in first-in first-out order
'#weight' => $field['widget']['weight'].".00".$delta,
); );
$form_item['value']['link'] = array( $form_item['url'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#maxlength' => '255', '#maxlength' => '255',
'#title' => $field['title'] == 'none' ? t($field['widget']['label']) : t($field['widget']['label'])." ".t('URL'), '#title' => $field['title'] == 'none' ? t($field['widget']['label']) : t($field['widget']['label'])." ".t('URL'),
'#default_value' => $node_field['value']['link'], '#default_value' => $node_field['url'],
'#required' => ($delta == 0) ? $field['required'] : FALSE, '#required' => ($delta == 0) ? $field['required'] : FALSE,
'#description' => $field['widget']['description'], '#description' => $field['widget']['description'],
); );
if ($field['title'] != 'none') { if ($field['title'] != 'none') {
$form_item['value']['title'] = array( $form_item['title'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#maxlength' => '255', '#maxlength' => '255',
'#title' => t($field['widget']['label'])." ".t('Title'), '#title' => t($field['widget']['label'])." ".t('Title'),
'#default_value' => $node_field['value']['title'], '#default_value' => $node_field['title'],
'#required' => ($delta == 0 && $field['title'] == 'required') ? $field['required'] : FALSE, '#required' => ($delta == 0 && $field['title'] == 'required') ? $field['required'] : FALSE,
); );
} }
if ($field['attributes']['target'] == 'user') { if ($field['attributes']['target'] == 'user') {
$form_item['value']['attributes']['target'] = array( $form_item['attributes']['target'] = array(
'#type' => 'checkbox', '#type' => 'checkbox',
'#title' => t('Open URL in a New Window'), '#title' => t('Open URL in a New Window'),
'#default_value' => $node_field['value']['attributes']['target'], '#default_value' => $node_field['attributes']['target'],
'#return_value' => "_blank", '#return_value' => "_blank",
); );
} }
...@@ -250,20 +223,42 @@ function _link_widget_form (&$form_item, $field, $node_field, $delta = 0) { ...@@ -250,20 +223,42 @@ function _link_widget_form (&$form_item, $field, $node_field, $delta = 0) {
function _link_widget_process (&$node_field, $delta = 0) { function _link_widget_process (&$node_field, $delta = 0) {
// Remove the target attribute if not selected // Remove the target attribute if not selected
if (!$node_field['value']['attributes']['target'] || $node_field['value']['attributes']['target'] == "default") { if (!$node_field['attributes']['target'] || $node_field['attributes']['target'] == "default") {
unset($node_field['value']['attributes']['target']); unset($node_field['attributes']['target']);
} }
} }
/** /**
* Implementation of hook_field_view() which performs any translation necessary. * Implementation of hook_field_formatter_info().
*/
function link_field_formatter_info() {
return array(
'default' => array(
'label' => 'Default, as link',
'field types' => array('link'),
),
'plain' => array(
'label' => 'Plain, no link',
'field types' => array('link'),
),
);
}
/**
* Implementation of hook_field_formatter().
*
*/ */
function _link_field_view($field, $value, $addlfields = array(), $node = NULL) { function link_field_formatter($field, $item, $formatter) {
if ($formatter == 'plain') {
return check_plain($item['url']);
}
$attributes = array(); $attributes = array();
// Add attributes defined at the widget level // Add attributes defined at the widget level
if (is_array($value['attributes'])) { if (is_array($item['attributes'])) {
foreach($value['attributes'] as $attribute => $attbvalue) { foreach($item['attributes'] as $attribute => $attbvalue) {
if (isset($field['attributes'][$attribute]) && $field['attributes'][$attribute] == 'user') { if (isset($item['attributes'][$attribute]) && $field['attributes'][$attribute] == 'user') {
$attributes[$attribute] = $attbvalue; $attributes[$attribute] = $attbvalue;
} }
} }
...@@ -277,82 +272,16 @@ function _link_field_view($field, $value, $addlfields = array(), $node = NULL) { ...@@ -277,82 +272,16 @@ function _link_field_view($field, $value, $addlfields = array(), $node = NULL) {
} }
} }
// Build the link with a title // Build the link with a title
if (strlen(trim($value['title']))) { if (strlen(trim($item['title']))) {
$output = l($value['title'],link_cleanup_url($value['link']),$attributes); $output = l($item['title'],link_cleanup_url($item['url']),$attributes);
} }
// Build the link with the URL as the title (max 80 characters) // Build the link with the URL as the title (max 80 characters)
else { else {
$output = l(strlen($value['link']) > 80 ? substr($value['link'],0,80)."..." : $value['link'],link_cleanup_url($value['link']),$attributes); $output = l(strlen($item['url']) > 80 ? substr($item['url'],0,80)."..." : $item['url'],link_cleanup_url($item['url']),$attributes);
} }
return $output; return $output;
} }
/**
* Implementation of hook_views_tables().
*/
function link_views_tables() {
$tables = array();
$fields = content_fields();
foreach ($fields as $field) {
if ($field['type'] == 'link') {
$tables['node_field_link_data_'. $field['field_name']] = array(
'name' => 'node_field_link_data',
'join' => array(
'left' => array(
'table' => 'node',
'field' => 'vid',
),
'right' => array(
'field' => 'vid',
),
'extra' => array('field_name' => $field['field_name']),
),
'fields' => array(
'field_url' => array(
'name' => 'Link URL: '. $field['field_name'],
'sortable' => TRUE,
),
'field_title' => array(
'name' => 'Link Title: '. $field['field_name'],
'sortable' => TRUE,
),
),
'sorts' => array(
'field_url' => array('name' => 'Link URL: '. $field['field_name']),
'field_title' => array('name' => 'Link Title: '. $field['field_name']),
),
'filters' => array(
'field_url' => array(
'name' => 'Link URL: '. $field['field_name'],
'operator' => array(
'=' => 'is',
'contains' => 'contains',
'begins' => 'begins with',
'ends' => 'ends wth',
'LIKE' => 'matches pattern',
),
'operator-handler' => '_text_filter_operator',
),
'field_title' => array(
'name' => 'Link Title: '. $field['field_name'],
'operator' => array(
'=' => 'is',
'contains' => 'contains',
'begins' => 'begins with',
'ends' => 'ends wth',
'LIKE' => 'matches pattern',
),
'operator-handler' => '_text_filter_operator',
),
),
);
}
}
return $tables;
}
/** /**
* Forms a valid URL if possible from an entered address. * Forms a valid URL if possible from an entered address.
* Trims whitespace and automatically adds an http:// to addresses without a protocol specified * Trims whitespace and automatically adds an http:// to addresses without a protocol specified
......
Supports Markdown
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