Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
drupal.org
link
Commits
e01630b4
Commit
e01630b4
authored
Apr 20, 2009
by
John C Fiala
Browse files
task #438756 by jcfiala: Bring link module to Drupal standards.
parent
91b6a753
Changes
5
Hide whitespace changes
Inline
Side-by-side
link.install
View file @
e01630b4
<?php
// $Id$
/**
* @file
* Install file for the link module.
*/
/**
* Implementation of hook_install().
*/
...
...
link.module
View file @
e01630b4
...
...
@@ -85,7 +85,7 @@ function link_field_settings($op, $field) {
}
$form
[
'display'
]
=
array
(
'#tree'
=>
true
,
'#tree'
=>
TRUE
,
);
$form
[
'display'
][
'url_cutoff'
]
=
array
(
'#type'
=>
'textfield'
,
...
...
@@ -103,7 +103,7 @@ function link_field_settings($op, $field) {
'user'
=>
t
(
'Allow the user to choose'
),
);
$form
[
'attributes'
]
=
array
(
'#tree'
=>
true
,
'#tree'
=>
TRUE
,
);
$form
[
'attributes'
][
'target'
]
=
array
(
'#type'
=>
'radios'
,
...
...
@@ -187,8 +187,8 @@ function link_field($op, &$node, $field, &$items, $teaser, $page) {
case
'validate'
:
$optional_field_found
=
FALSE
;
foreach
(
$items
as
$delta
=>
$value
)
{
_link_validate
(
$items
[
$delta
],
$delta
,
$field
,
$node
,
$optional_field_found
);
foreach
(
$items
as
$delta
=>
$value
)
{
_link_validate
(
$items
[
$delta
],
$delta
,
$field
,
$node
,
$optional_field_found
);
}
if
(
$field
[
'url'
]
==
'optional'
&&
$field
[
'title'
]
==
'optional'
&&
$field
[
'required'
]
&&
!
$optional_field_found
)
{
...
...
@@ -197,7 +197,7 @@ function link_field($op, &$node, $field, &$items, $teaser, $page) {
break
;
case
'presave'
:
foreach
(
$items
as
$delta
=>
$value
)
{
foreach
(
$items
as
$delta
=>
$value
)
{
_link_process
(
$items
[
$delta
],
$delta
,
$field
,
$node
);
}
break
;
...
...
@@ -266,16 +266,16 @@ function _link_validate(&$item, $delta, $field, $node, &$optional_field_found) {
if
(
$item
[
'url'
]
&&
!
(
isset
(
$field
[
'widget'
][
'default_value'
][
$delta
][
'url'
])
&&
$item
[
'url'
]
==
$field
[
'widget'
][
'default_value'
][
$delta
][
'url'
]
&&
!
$field
[
'required'
]))
{
// Validate the link.
if
(
link_validate_url
(
trim
(
$item
[
'url'
]))
==
FALSE
)
{
form_set_error
(
$field
[
'field_name'
]
.
']['
.
$delta
.
'][url'
,
t
(
'Not a valid URL.'
));
form_set_error
(
$field
[
'field_name'
]
.
']['
.
$delta
.
'][url'
,
t
(
'Not a valid URL.'
));
}
// Require a title for the link if necessary.
if
(
$field
[
'title'
]
==
'required'
&&
strlen
(
trim
(
$item
[
'title'
]))
==
0
)
{
form_set_error
(
$field
[
'field_name'
]
.
']['
.
$delta
.
'][title'
,
t
(
'Titles are required for all links.'
));
form_set_error
(
$field
[
'field_name'
]
.
']['
.
$delta
.
'][title'
,
t
(
'Titles are required for all links.'
));
}
}
// Require a link if we have a title.
if
(
$field
[
'url'
]
!==
'optional'
&&
strlen
(
$item
[
'title'
])
>
0
&&
strlen
(
trim
(
$item
[
'url'
]))
==
0
)
{
form_set_error
(
$field
[
'field_name'
]
.
']['
.
$delta
.
'][url'
,
t
(
'You cannot enter a title without a link url.'
));
form_set_error
(
$field
[
'field_name'
]
.
']['
.
$delta
.
'][url'
,
t
(
'You cannot enter a title without a link url.'
));
}
// In a totally bizzaro case, where URLs and titles are optional but the field is required, ensure there is at least one link.
if
(
$field
[
'url'
]
==
'optional'
&&
$field
[
'title'
]
==
'optional'
&&
(
strlen
(
trim
(
$item
[
'url'
]))
!=
0
||
strlen
(
trim
(
$item
[
'title'
]))
!=
0
))
{
...
...
@@ -327,7 +327,7 @@ function _link_sanitize(&$item, $delta, &$field, &$node) {
// Create a shortened URL for display.
$display_url
=
$type
==
LINK_EMAIL
?
str_replace
(
'mailto:'
,
''
,
$url
)
:
url
(
$url
,
array
(
'query'
=>
isset
(
$item
[
'query'
])
?
$item
[
'query'
]
:
NULL
,
'fragment'
=>
isset
(
$item
[
'fragment'
])
?
$item
[
'fragment'
]
:
NULL
,
'absolute'
=>
TRUE
));
if
(
$field
[
'display'
][
'url_cutoff'
]
&&
strlen
(
$display_url
)
>
$field
[
'display'
][
'url_cutoff'
])
{
$display_url
=
substr
(
$display_url
,
0
,
$field
[
'display'
][
'url_cutoff'
])
.
"..."
;
$display_url
=
substr
(
$display_url
,
0
,
$field
[
'display'
][
'url_cutoff'
])
.
"..."
;
}
$item
[
'display_url'
]
=
$display_url
;
...
...
@@ -351,7 +351,7 @@ function _link_sanitize(&$item, $delta, &$field, &$node) {
// Add attributes defined at the widget level
$attributes
=
array
();
if
(
!
empty
(
$item
[
'attributes'
])
&&
is_array
(
$item
[
'attributes'
]))
{
foreach
(
$item
[
'attributes'
]
as
$attribute
=>
$attbvalue
)
{
foreach
(
$item
[
'attributes'
]
as
$attribute
=>
$attbvalue
)
{
if
(
isset
(
$item
[
'attributes'
][
$attribute
])
&&
$field
[
'attributes'
][
$attribute
]
==
'user'
)
{
$attributes
[
$attribute
]
=
$attbvalue
;
}
...
...
@@ -359,7 +359,7 @@ function _link_sanitize(&$item, $delta, &$field, &$node) {
}
// Add attributes defined at the field level
if
(
is_array
(
$field
[
'attributes'
]))
{
foreach
(
$field
[
'attributes'
]
as
$attribute
=>
$attbvalue
)
{
foreach
(
$field
[
'attributes'
]
as
$attribute
=>
$attbvalue
)
{
if
(
!
empty
(
$attbvalue
)
&&
$attbvalue
!=
'default'
&&
$attbvalue
!=
'user'
)
{
$attributes
[
$attribute
]
=
$attbvalue
;
}
...
...
@@ -422,7 +422,7 @@ function theme_link($element) {
$element
[
'url'
][
'#title'
]
=
$element
[
'#title'
]
.
' '
.
$element
[
'url'
][
'#title'
];
$element
[
'title'
][
'#title'
]
=
$element
[
'#title'
]
.
' '
.
$element
[
'title'
][
'#title'
];
}
elseif
(
$element
[
'url'
])
{
elseif
(
$element
[
'url'
])
{
$element
[
'url'
][
'#title'
]
=
$element
[
'#title'
];
}
}
...
...
@@ -430,12 +430,12 @@ function theme_link($element) {
$output
=
''
;
$output
.
=
'<div class="link-field-subrow clear-block">'
;
if
(
isset
(
$element
[
'title'
]))
{
$output
.
=
'<div class="link-field-title link-field-column">'
.
theme
(
'textfield'
,
$element
[
'title'
])
.
'</div>'
;
$output
.
=
'<div class="link-field-title link-field-column">'
.
theme
(
'textfield'
,
$element
[
'title'
])
.
'</div>'
;
}
$output
.
=
'<div class="link-field-url'
.
(
isset
(
$element
[
'title'
])
?
' link-field-column'
:
''
)
.
'">'
.
theme
(
'textfield'
,
$element
[
'url'
])
.
'</div>'
;
$output
.
=
'<div class="link-field-url'
.
(
isset
(
$element
[
'title'
])
?
' link-field-column'
:
''
)
.
'">'
.
theme
(
'textfield'
,
$element
[
'url'
])
.
'</div>'
;
$output
.
=
'</div>'
;
if
(
!
empty
(
$element
[
'attributes'
][
'target'
]))
{
$output
.
=
'<div class="link-attributes">'
.
theme
(
'checkbox'
,
$element
[
'attributes'
][
'target'
])
.
'</div>'
;
$output
.
=
'<div class="link-attributes">'
.
theme
(
'checkbox'
,
$element
[
'attributes'
][
'target'
])
.
'</div>'
;
}
return
$output
;
}
...
...
@@ -617,7 +617,7 @@ function link_token_values($type, $object = NULL) {
function
link_views_api
()
{
return
array
(
'api'
=>
2
,
'path'
=>
drupal_get_path
(
'module'
,
'link'
)
.
'/views'
,
'path'
=>
drupal_get_path
(
'module'
,
'link'
)
.
'/views'
,
);
}
...
...
@@ -634,12 +634,12 @@ function link_cleanup_url($url, $protocol = "http") {
if
(
$type
==
LINK_EXTERNAL
)
{
// Check if there is no protocol specified.
$protocol_match
=
preg_match
(
"/^([a-z0-9][a-z0-9\.\-_]*:\/\/)/i"
,
$url
);
$protocol_match
=
preg_match
(
"/^([a-z0-9][a-z0-9\.\-_]*:\/\/)/i"
,
$url
);
if
(
empty
(
$protocol_match
))
{
// But should there be? Add an automatic http:// if it starts with a domain name.
$domain_match
=
preg_match
(
'/^(([a-z0-9]([a-z0-9\-_]*\.)+)('
.
LINK_DOMAINS
.
'|[a-z]{2}))/i'
,
$url
);
$domain_match
=
preg_match
(
'/^(([a-z0-9]([a-z0-9\-_]*\.)+)('
.
LINK_DOMAINS
.
'|[a-z]{2}))/i'
,
$url
);
if
(
!
empty
(
$domain_match
))
{
$url
=
$protocol
.
"://"
.
$url
;
$url
=
$protocol
.
"://"
.
$url
;
}
}
}
...
...
@@ -660,7 +660,7 @@ function link_validate_url($text) {
$allowed_protocols
=
variable_get
(
'filter_allowed_protocols'
,
array
(
'http'
,
'https'
,
'ftp'
,
'news'
,
'nntp'
,
'telnet'
,
'mailto'
,
'irc'
,
'ssh'
,
'sftp'
,
'webcal'
));
$protocol
=
'(('
.
implode
(
"|"
,
$allowed_protocols
)
.
'):\/\/)'
;
$protocol
=
'(('
.
implode
(
"|"
,
$allowed_protocols
)
.
'):\/\/)'
;
$authentication
=
'([a-z0-9]+(:[a-z0-9]+)?@)'
;
$domain
=
'(([a-z0-9]([a-z0-9\-_\[\]])*)(\.(([a-z0-9\-_\[\]])+\.)*('
.
LINK_DOMAINS
.
'|[a-z]{2}))?)'
;
$ipv4
=
'([0-9]{1,3}(\.[0-9]{1,3}){3})'
;
...
...
@@ -668,7 +668,7 @@ function link_validate_url($text) {
$port
=
'(:([0-9]{1,5}))'
;
// Pattern specific to eternal links.
$external_pattern
=
'/^'
.
$protocol
.
'?'
.
$authentication
.
'?
'
.
'
('
.
$domain
.
'|'
.
$ipv4
.
'|'
.
$ipv6
.
' |localhost)'
.
$port
.
'?'
;
$external_pattern
=
'/^'
.
$protocol
.
'?'
.
$authentication
.
'?('
.
$domain
.
'|'
.
$ipv4
.
'|'
.
$ipv6
.
' |localhost)'
.
$port
.
'?'
;
// Pattern specific to internal links.
$internal_pattern
=
"/^([a-z0-9_\-+\[\]]+)"
;
...
...
@@ -679,10 +679,10 @@ function link_validate_url($text) {
$anchor
=
"(#[a-z0-9_\-\.~+%=&,$'():;*@\[\]]*)"
;
// The rest of the path for a standard URL.
$end
=
$directories
.
'?'
.
$query
.
'?'
.
$anchor
.
'?'
.
'$/i'
;
$end
=
$directories
.
'?'
.
$query
.
'?'
.
$anchor
.
'?'
.
'$/i'
;
$user
=
'[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\'\[\]]+'
;
$email_pattern
=
'/^mailto:'
.
$user
.
'@'
.
'('
.
$domain
.
'|'
.
$ipv4
.
'|'
.
$ipv6
.
'|localhost)'
.
$query
.
'?$/'
;
$email_pattern
=
'/^mailto:'
.
$user
.
'@'
.
'('
.
$domain
.
'|'
.
$ipv4
.
'|'
.
$ipv6
.
'|localhost)'
.
$query
.
'?$/'
;
if
(
strpos
(
$text
,
'<front>'
)
===
0
)
{
return
LINK_FRONT
;
...
...
views/link.views.inc
View file @
e01630b4
<?php
// $Id$
/**
* @file
* Contains functions handling views integration.
*/
/**
* Implementation of hook_views_handlers().
...
...
@@ -6,7 +11,7 @@
function
link_views_handlers
()
{
return
array
(
'info'
=>
array
(
'path'
=>
drupal_get_path
(
'module'
,
'link'
)
.
'/views'
,
'path'
=>
drupal_get_path
(
'module'
,
'link'
)
.
'/views'
,
),
'handlers'
=>
array
(
'link_views_handler_argument_target'
=>
array
(
...
...
@@ -40,7 +45,7 @@ function link_views_content_field_data($field) {
// Build out additional views data for the link "title" field.
$data
[
$table_alias
][
$field
[
'field_name'
]
.
'_title'
]
=
array
(
'group'
=>
t
(
'Content'
),
'title'
=>
t
(
'@label title'
,
array
(
'@label'
=>
t
(
$field_types
[
$field
[
'type'
]][
'label'
])))
.
': '
.
t
(
$field
[
'widget'
][
'label'
])
.
' ('
.
$field
[
'field_name'
]
.
')'
,
'title'
=>
t
(
'@label title'
,
array
(
'@label'
=>
t
(
$field_types
[
$field
[
'type'
]][
'label'
])))
.
': '
.
t
(
$field
[
'widget'
][
'label'
])
.
' ('
.
$field
[
'field_name'
]
.
')'
,
'help'
=>
$data
[
$table_alias
][
$field
[
'field_name'
]
.
'_url'
][
'help'
],
'argument'
=>
array
(
'field'
=>
$db_info
[
'columns'
][
'title'
][
'column'
],
...
...
@@ -73,7 +78,7 @@ function link_views_content_field_data($field) {
// TODO: Add a protocol argument.
$data
[
$table_alias
][
$field
[
'field_name'
]
.
'_protocol'
]
=
array
(
'group'
=>
t
(
'Content'
),
'title'
=>
t
(
'@label protocol'
,
array
(
'@label'
=>
t
(
$field_types
[
$field
[
'type'
]][
'label'
])))
.
': '
.
t
(
$field
[
'widget'
][
'label'
])
.
' ('
.
$field
[
'field_name'
]
.
')'
,
'title'
=>
t
(
'@label protocol'
,
array
(
'@label'
=>
t
(
$field_types
[
$field
[
'type'
]][
'label'
])))
.
': '
.
t
(
$field
[
'widget'
][
'label'
])
.
' ('
.
$field
[
'field_name'
]
.
')'
,
'help'
=>
$data
[
$table_alias
][
$field
[
'field_name'
]
.
'_url'
][
'help'
],
'filter'
=>
array
(
'field'
=>
$db_info
[
'columns'
][
'url'
][
'column'
],
...
...
@@ -90,11 +95,11 @@ function link_views_content_field_data($field) {
// TODO: Add a target filter.
$data
[
$table_alias
][
$field
[
'field_name'
]
.
'_target'
]
=
array
(
'group'
=>
t
(
'Content'
),
'title'
=>
t
(
'@label target'
,
array
(
'@label'
=>
t
(
$field_types
[
$field
[
'type'
]][
'label'
])))
.
': '
.
t
(
$field
[
'widget'
][
'label'
])
.
' ('
.
$field
[
'field_name'
]
.
')'
,
'title'
=>
t
(
'@label target'
,
array
(
'@label'
=>
t
(
$field_types
[
$field
[
'type'
]][
'label'
])))
.
': '
.
t
(
$field
[
'widget'
][
'label'
])
.
' ('
.
$field
[
'field_name'
]
.
')'
,
'help'
=>
$data
[
$table_alias
][
$field
[
'field_name'
]
.
'_url'
][
'help'
],
'argument'
=>
array
(
'field'
=>
$db_info
[
'columns'
][
'attributes'
][
'column'
],
'title'
=>
t
(
'@label target'
,
array
(
'@label'
=>
t
(
$field_types
[
$field
[
'type'
]][
'label'
])))
.
': '
.
t
(
$field
[
'widget'
][
'label'
])
.
' ('
.
$field
[
'field_name'
]
.
')'
,
'title'
=>
t
(
'@label target'
,
array
(
'@label'
=>
t
(
$field_types
[
$field
[
'type'
]][
'label'
])))
.
': '
.
t
(
$field
[
'widget'
][
'label'
])
.
' ('
.
$field
[
'field_name'
]
.
')'
,
'tablename'
=>
$db_info
[
'table'
],
'handler'
=>
'link_views_handler_argument_target'
,
'additional fields'
=>
array
(),
...
...
views/link_views_handler_argument_target.inc
View file @
e01630b4
<?php
// $Id$
/**
* @file
* Argument handler to filter results by target.
*/
/**
* Argument handler to filter results by target.
*/
...
...
views/link_views_handler_filter_protocol.inc
View file @
e01630b4
<?php
// $Id$
/**
* @file
* Contains filter handlers for protocol filters with views.
*/
/**
* Filter handler for limiting a view to URLs of a certain protocol.
*/
...
...
@@ -76,7 +81,7 @@ class link_views_handler_filter_protocol extends views_handler_filter_string {
$where_conditions
=
array
();
foreach
(
$protocols
as
$protocol
)
{
// Simple case, the URL begins with the specified protocol.
$condition
=
$field
.
' LIKE \''
.
$protocol
.
'%\''
;
$condition
=
$field
.
' LIKE \''
.
$protocol
.
'%\''
;
// More complex case, no protocol specified but is automatically cleaned up
// by link_cleanup_url(). RegEx is required for this search operation.
...
...
@@ -85,12 +90,12 @@ class link_views_handler_filter_protocol extends views_handler_filter_string {
// PostGreSQL code has NOT been tested. Please report any problems to the link issue queue.
// pgSQL requires all slashes to be double escaped in regular expressions.
// See http://www.postgresql.org/docs/8.1/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP
$condition
.
=
' OR '
.
$field
.
' ~* \''
.
'^(([a-z0-9]([a-z0-9\\-_]*\\.)+)('
.
LINK_DOMAINS
.
'|[a-z][a-z]))'
.
'\''
;
$condition
.
=
' OR '
.
$field
.
' ~* \''
.
'^(([a-z0-9]([a-z0-9\\-_]*\\.)+)('
.
LINK_DOMAINS
.
'|[a-z][a-z]))'
.
'\''
;
}
else
{
// mySQL requires backslashes to be double (triple?) escaped within character classes.
// See http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_regexp
$condition
.
=
' OR '
.
$field
.
' REGEXP \''
.
'^(([a-z0-9]([a-z0-9\\\-_]*\.)+)('
.
LINK_DOMAINS
.
'|[a-z][a-z]))'
.
'\''
;
$condition
.
=
' OR '
.
$field
.
' REGEXP \''
.
'^(([a-z0-9]([a-z0-9\\\-_]*\.)+)('
.
LINK_DOMAINS
.
'|[a-z][a-z]))'
.
'\''
;
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment