Extend the number of services
Basic
From the version 2.x has been implemented a mechanism for extend easily the number of services through the implementation of hook_service_links() which have to return an array like that:
// Basic example
function mymodule_service_links() {
$links = array();
$links['service_id'] = array(
'name' => 'Service Name',
'description' => t('Share this content in XXX!'),
'link' => 'http://myservice.org/?add_url=<encoded-url>&t=<encoded-title>',
'icon' => drupal_get_path('module', 'mymodulename') . 'icon.png',
);
return $links;
}
- service_id
- This field represent an univoque id used internally by Service Links engine, don't use spaces and capital character here, dont' use an exisisting id.
- name
- The real name of your service, it will be used when it's shown as text.
- description
- It appear like a link's tooltip and explain briefly where the click bring.
- link
- It's not difficult to extract the needed parameter for build the link and in most of cases the Service give some suggestion too. The tags allowed are: <[raw-]encoded-title>, <[raw-]encoded-url>, <[raw-]encoded-source>, <node-id>, <[raw-]encoded-teaser>, <[raw-]encoded-short-url>, <[raw-]encoded-query> (the squares indicate an optional string)
- icon
- The icon field can be omitted if the related image is available under the standard service_links/images/ folder and its name is like: service_id.png, warning filling this field with a full path like drupal_get_path() does, deny any icon override by the alternative folder.
Advanced
More fields are availables for specific situations:
- javascript
- Insert the file specified every time the service is loaded, the location can be also an external server.
- css
- Add the CSS file specified.
- callback
- A callback function will receive the service and the params to substitute.
- attributes
- An associative array with HTML attributes which complete the link can be added here.
Service Links API
There are two functions that help developers to include the services provided by Service Links in their code:
- service_links_render($node, $nodelink, $style);
- service_links_render_some($service_id, $node, $nodelink, $style);
- $service_id
- An array or a single string which contain the id(s) of services to render
- $node
- It is the object which contain the fields needed for build the tags, when NULL (not-node pages) Service Links try to get most info it can thorugh Drupal functions
- $nodelink
- This param affect the type or render, when TRUE the result is an array of arrays used into the link section in the bottom part of the node, when FALSE the result of render is an array of HTML links (default = FALSE)
- $style
- The standard style can be overwrite setting up this param (default = SERVICE_LINKS_STYLE_NONE) with one of these constants: SERVICE_LINKS_STYLE_TEXT, SERVICE_LINKS_STYLE_IMAGE, SERVICE_LINKS_STYLE_IMAGE_AND_TEXT
Override the theme functions
Service Links allow themers to override the normal render through the theme functions declared by hook_theme() (moved into service_links.theme.inc from 2.1):
- service_links_node_format($links, $label);
- service_links_block_format($items, $style);
- service_links_fisheye_format($items);
- service_links_build_links($text, $image, $url, $nodelink, $style, $attributes);
Their names are enough explicative to understand what part of render is influenced, who want place these services (or some of them) into an explicit position within the template file (node[-type].tpl.php) can use the two variables availables for each node: $service_links and $service_links_rendered which represent, respectively, the array of services with all the parameters loaded and the single HTML rendered string.
Playing with template_preprocess_page() and template_preprocess_node (template.php), more variables can be created using the API provided by Service Links!