Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
I
image_url_formatter
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
image_url_formatter
Commits
f1299c4e
Commit
f1299c4e
authored
May 19, 2011
by
g089h515r806
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First workable version:maybe need more test.
parent
ed81cb2f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
181 additions
and
0 deletions
+181
-0
image_url_formatter.info
image_url_formatter.info
+5
-0
image_url_formatter.module
image_url_formatter.module
+176
-0
No files found.
image_url_formatter.info
View file @
f1299c4e
name = Image URL Formatter
description = "Add an URL formatter for image field"
core = 7.x
dependencies[] = field
dependencies[] = image
files[] = image_url_formatter.module
\ No newline at end of file
image_url_formatter.module
0 → 100644
View file @
f1299c4e
<?php
/**
* @file
* Add an URL formatter for image field
*/
/**
* Implements hook_theme().
*/
function
image_url_formatter_theme
()
{
return
array
(
'image_url_formatter'
=>
array
(
'variables'
=>
array
(
'item'
=>
NULL
,
'path'
=>
NULL
,
'image_style'
=>
NULL
,
),
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function
image_url_formatter_field_formatter_info
()
{
$formatters
=
array
(
'image_url'
=>
array
(
'label'
=>
t
(
'Image URL'
),
'field types'
=>
array
(
'image'
),
'settings'
=>
array
(
'image_style'
=>
''
,
'image_link'
=>
''
),
),
);
return
$formatters
;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function
image_url_formatter_field_formatter_settings_form
(
$field
,
$instance
,
$view_mode
,
$form
,
&
$form_state
)
{
$display
=
$instance
[
'display'
][
$view_mode
];
$settings
=
$display
[
'settings'
];
$image_styles
=
image_style_options
(
FALSE
);
$element
[
'image_style'
]
=
array
(
'#title'
=>
t
(
'Image style'
),
'#type'
=>
'select'
,
'#default_value'
=>
$settings
[
'image_style'
],
'#empty_option'
=>
t
(
'None (original image)'
),
'#options'
=>
$image_styles
,
);
$link_types
=
array
(
'content'
=>
t
(
'Content'
),
'file'
=>
t
(
'File'
),
);
$element
[
'image_link'
]
=
array
(
'#title'
=>
t
(
'Link image url to'
),
'#type'
=>
'select'
,
'#default_value'
=>
$settings
[
'image_link'
],
'#empty_option'
=>
t
(
'Nothing'
),
'#options'
=>
$link_types
,
);
return
$element
;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function
image_url_formatter_field_formatter_settings_summary
(
$field
,
$instance
,
$view_mode
)
{
$display
=
$instance
[
'display'
][
$view_mode
];
$settings
=
$display
[
'settings'
];
$summary
=
array
();
$image_styles
=
image_style_options
(
FALSE
);
// Unset possible 'No defined styles' option.
unset
(
$image_styles
[
''
]);
// Styles could be lost because of enabled/disabled modules that defines
// their styles in code.
if
(
isset
(
$image_styles
[
$settings
[
'image_style'
]]))
{
$summary
[]
=
t
(
'URL for image style: @style'
,
array
(
'@style'
=>
$image_styles
[
$settings
[
'image_style'
]]));
}
else
{
$summary
[]
=
t
(
'Original image URL'
);
}
$link_types
=
array
(
'content'
=>
t
(
'Linked to content'
),
'file'
=>
t
(
'Linked to file'
),
);
// Display this setting only if image is linked.
if
(
isset
(
$link_types
[
$settings
[
'image_link'
]]))
{
$summary
[]
=
$link_types
[
$settings
[
'image_link'
]];
}
return
implode
(
'<br />'
,
$summary
);
}
/**
* Implements hook_field_formatter_view().
*/
function
image_url_formatter_field_formatter_view
(
$entity_type
,
$entity
,
$field
,
$instance
,
$langcode
,
$items
,
$display
)
{
$element
=
array
();
switch
(
$display
[
'type'
])
{
case
'image_url'
:
// Check if the formatter involves a link.
if
(
$display
[
'settings'
][
'image_link'
]
==
'content'
)
{
$uri
=
entity_uri
(
$entity_type
,
$entity
);
}
elseif
(
$display
[
'settings'
][
'image_link'
]
==
'file'
)
{
$link_file
=
TRUE
;
}
foreach
(
$items
as
$delta
=>
$item
)
{
if
(
isset
(
$link_file
))
{
$uri
=
array
(
'path'
=>
file_create_url
(
$item
[
'uri'
]),
'options'
=>
array
(),
);
}
//debug($item);
$element
[
$delta
]
=
array
(
'#theme'
=>
'image_url_formatter'
,
'#item'
=>
$item
,
'#image_style'
=>
$display
[
'settings'
][
'image_style'
],
'#path'
=>
isset
(
$uri
)
?
$uri
:
''
,
);
}
break
;
}
return
$element
;
}
/**
* Returns HTML for an image url field formatter.
*
* @param $variables
* An associative array containing:
* - item: An array of image data.
* - image_style: An optional image style.
* - path: An array containing the link 'path' and link 'options'.
*
* @ingroup themeable
*/
function
theme_image_url_formatter
(
$variables
)
{
$item
=
$variables
[
'item'
];
$image
=
array
(
'path'
=>
$item
[
'uri'
],
'alt'
=>
$item
[
'alt'
],
);
// Do not output an empty 'title' attribute.
if
(
drupal_strlen
(
$item
[
'title'
])
>
0
)
{
$image
[
'title'
]
=
$item
[
'title'
];
}
$output
=
file_create_url
(
$item
[
'uri'
]);
if
(
$variables
[
'image_style'
])
{
//debug($image);
$image
[
'style_name'
]
=
$variables
[
'image_style'
];
$output
=
image_style_url
(
$image
[
'style_name'
],
$item
[
'uri'
]);
}
if
(
$variables
[
'path'
])
{
$path
=
$variables
[
'path'
][
'path'
];
$options
=
$variables
[
'path'
][
'options'
];
// When displaying an image inside a link, the html option must be TRUE.
$options
[
'html'
]
=
TRUE
;
$output
=
l
(
$output
,
$path
,
$options
);
}
return
$output
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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