social_media_links.module in Social Media Links Block and Field 7
Same filename and directory in other branches
Functions for the Social Media Links module.
File
social_media_links.moduleView source
<?php
/**
* @file
* Functions for the Social Media Links module.
*/
/**
* Implements hook_theme().
*/
function social_media_links_theme($existing, $type, $theme, $path) {
return array(
'social_media_links_platforms_table' => array(
'render element' => 'form',
),
'social_media_links_platforms' => array(
'variables' => array(
'platform_values' => array(),
'icon_style' => array(),
'appearance' => array(),
'link_attributes' => array(),
'attributes' => array(),
),
),
'social_media_links_platform' => array(
'variables' => array(
'info' => array(),
'name' => NULL,
'value' => NULL,
'icon_style' => array(),
'appearance' => array(),
'attributes' => array(),
),
),
);
}
/**
* Implements hook_ctools_plugin_directory().
*/
function social_media_links_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && $plugin == 'content_types') {
return 'plugins/' . $plugin;
}
return FALSE;
}
/**
* Implements hook_block_info().
*/
function social_media_links_block_info() {
$blocks['social-media-links'] = array(
'info' => t('Social Media Links'),
// We cache per role, so that the edit/configure links display only if the
// user has access.
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function social_media_links_block_configure($delta = '') {
if ($delta === 'social-media-links') {
$values = array(
'platforms' => variable_get('social_media_links_platforms', array()),
'appearance' => variable_get('social_media_links_appearance', array()),
'icon_style' => variable_get('social_media_links_icon_style', ''),
'link_attributes' => variable_get('social_media_links_link_attributes', array()),
);
$form = social_media_links_form($values);
return $form;
}
}
function social_media_links_form($values) {
$form = array();
// Platforms.
$form['platforms'] = array(
'#type' => 'fieldset',
'#title' => t('Platforms'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE,
'#theme' => 'social_media_links_platforms_table',
);
$platform_values = isset($values['platforms']) ? $values['platforms'] : array();
$platforms = social_media_links_platforms();
if (!empty($platform_values)) {
// Combine the platforms with the weight value for sorting.
foreach ($platforms as $key => $value) {
if (isset($platform_values[$key]['weight'])) {
$platforms[$key]['weight'] = $platform_values[$key]['weight'];
}
}
uasort($platforms, 'drupal_sort_weight');
}
$i = -10;
foreach ($platforms as $key => $value) {
$form['platforms'][$key] = array(
'platform_value' => array(
'#type' => 'textfield',
'#title' => $value['title'],
'#title_display' => 'invisible',
'#size' => 40,
'#field_prefix' => $value['base url'],
'#default_value' => isset($platform_values[$key]['platform_value']) ? $platform_values[$key]['platform_value'] : '',
),
'weight' => array(
'#type' => 'weight',
'#title' => t('Weight'),
'#title_display' => 'invisible',
'#delta' => 10,
'#default_value' => isset($platform_values[$key]['weight']) ? $platform_values[$key]['weight'] : $i + 1,
'#attributes' => array(
'class' => array(
'weight',
),
),
),
);
$i++;
}
// Appearance.
$form['appearance'] = array(
'#type' => 'fieldset',
'#title' => t('Appearance'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$appearance_values = isset($values['appearance']) ? $values['appearance'] : array();
$form['appearance']['orientation'] = array(
'#type' => 'select',
'#title' => t('Orientation'),
'#default_value' => isset($appearance_values['orientation']) ? $appearance_values['orientation'] : 'h',
'#options' => array(
'v' => t('vertical'),
'h' => t('horizontal'),
),
);
$form['appearance']['show_name'] = array(
'#type' => 'checkbox',
'#title' => t('Show name'),
'#description' => t('Show the platform name next to the icon.'),
'#default_value' => isset($appearance_values['show_name']) ? $appearance_values['show_name'] : 0,
);
// Link Attributes.
$form['link_attributes'] = array(
'#type' => 'fieldset',
'#title' => t('Link attributes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$link_values = isset($values['link_attributes']) ? $values['link_attributes'] : array();
$form['link_attributes']['target'] = array(
'#type' => 'select',
'#title' => t('Default target'),
'#default_value' => isset($link_values['target']) ? $link_values['target'] : '<none>',
'#options' => array(
'<none>' => t('Remove target attribute'),
'_blank' => t('Open in a new browser window or tab (_blank)'),
'_self' => t('Open in the current window (_self)'),
'_parent' => t('Open in the frame that is superior to the frame the link is in (_parent)'),
'_top' => t('Cancel all frames and open in full browser window (_top)'),
),
);
$form['link_attributes']['rel'] = array(
'#type' => 'select',
'#title' => t('Default rel'),
'#default_value' => isset($link_values['rel']) ? $link_values['rel'] : '<none>',
'#options' => array(
'<none>' => t('Remove rel attribute'),
'nofollow' => t('Set nofollow'),
),
);
// Icon Sets.
$form['icons'] = array(
'#type' => 'fieldset',
'#title' => t('Icon Sets'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$options = social_media_links_iconsets_options();
$form['icons']['icon_style'] = array(
'#type' => 'select',
'#title' => t('Icon Style'),
'#default_value' => $values['icon_style'],
'#options' => $options,
);
// Generate the icon set table.
$header = array(
array(
'data' => t('Name'),
'style' => 'width: 150px;',
),
t('Sizes'),
t('Icon examples and download instructions'),
);
$rows = array();
$icons = social_media_links_iconsets();
foreach ($icons as $iconset_name => $iconset) {
if (isset($iconset['download url'])) {
$name = '<strong>' . l($iconset['name'], $iconset['download url'], array(
'attributes' => array(
'target' => '_blank',
),
)) . '</strong>';
}
else {
$name = '<strong>' . $iconset['name'] . '</strong>';
}
if (isset($iconset['publisher'])) {
$name .= '<br />' . t('by') . ' ';
if (isset($iconset['publisher url'])) {
$name .= l($iconset['publisher'], $iconset['publisher url'], array(
'attributes' => array(
'target' => '_blank',
),
));
}
else {
$name .= $iconset['publisher'];
}
}
$row = array(
$name,
implode('<br />', $iconset['styles']),
);
if (!empty($iconset['path'])) {
$row[] = _social_media_links_generate_example_table($platforms, $iconset);
}
else {
$searchdirs = social_media_links_searchdirs($iconset_name);
$str = '<strong>' . t('Not installed.') . '</strong><br />';
$str .= t('To install: !download and install it into', array(
'!download' => l(t('Download'), $iconset['download url'], array(
'attributes' => array(
'target' => '_blank',
),
)),
));
$str .= ' <code> DRUPAL_ROOT/' . preg_replace('/,([^,]+) ?$/', " " . t('or') . " \$1", implode(', ', $searchdirs), 1) . '</code>.';
if (isset($iconset['additional instructions'])) {
$str .= ' ' . $iconset['additional instructions'];
}
$row[] = $str;
}
$rows[] = $row;
$vars = array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
'id' => 'social-media-links-iconsets',
),
);
$form['icons']['installed'] = array(
'#markup' => theme('table', $vars),
);
}
return $form;
}
/**
* Implements hook_block_save().
*/
function social_media_links_block_save($delta = '', $edit = array()) {
if ($delta === 'social-media-links') {
variable_set('social_media_links_platforms', $edit['platforms']);
variable_set('social_media_links_appearance', $edit['appearance']);
variable_set('social_media_links_link_attributes', $edit['link_attributes']);
variable_set('social_media_links_icon_style', $edit['icon_style']);
}
}
/**
* Implements hook_block_view().
*/
function social_media_links_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'social-media-links':
$platforms = variable_get('social_media_links_platforms', array());
$platforms = _social_media_links_cleanup_platforms($platforms);
if (count($platforms) > 0) {
$block['subject'] = t('Follow Us');
$block['content'] = array(
'#theme' => 'social_media_links_platforms',
'#platform_values' => $platforms,
'#icon_style' => variable_get('social_media_links_icon_style'),
'#appearance' => variable_get('social_media_links_appearance'),
'#link_attributes' => variable_get('social_media_links_link_attributes'),
'#attributes' => array(
'class' => array(
'social-media-links',
'platforms',
),
),
);
// Add css to the block.
$block['content']['#attached']['css'][] = drupal_get_path('module', 'social_media_links') . '/social_media_links.css';
}
break;
}
return $block;
}
/**
* Preprocesses variables for social media links platforms.
*
* @see theme_social_media_links_platforms()
*/
function template_preprocess_social_media_links_platforms(&$variables) {
$platform_infos = social_media_links_platforms();
$icon_style = explode(':', $variables['icon_style']);
// Apply the link settings to the render array.
foreach ($variables['link_attributes'] as $key => $value) {
if ($value == '<none>') {
unset($variables['link_attributes'][$key]);
}
}
// Sort and loop over the platforms.
$variables['platforms'] = array();
uasort($variables['platform_values'], 'drupal_sort_weight');
foreach ($variables['platform_values'] as $platform_name => $platform_value) {
// Build the platform data array.
$variables['platforms'][$platform_name] = array(
'#theme' => 'social_media_links_platform',
'#name' => $platform_name,
'#info' => $platform_infos[$platform_name],
'#value' => $platform_value['platform_value'],
'#icon_style' => $icon_style,
'#appearance' => $variables['appearance'],
'#attributes' => $variables['link_attributes'],
);
}
if ($variables['appearance']['orientation'] == 'h') {
$variables['attributes']['class'][] = 'inline horizontal';
}
else {
$variables['attributes']['class'][] = 'vertical';
}
}
/**
* Processes variables for social-media-links-platform.tpl.php.
*
* @see theme_social_media_links_platform()
*/
function template_preprocess_social_media_links_platform(&$variables) {
$info = $variables['info'];
$name = $variables['name'];
$value = $variables['value'];
$icon_style = $variables['icon_style'];
// Call the url callback of the platform to create the link url.
$variables['link'] = $info['base url'] . $value;
if (isset($info['url callback'])) {
$platform_url_changed = call_user_func($info['url callback'], $info['base url'], $value);
if ($platform_url_changed) {
$variables['link'] = $platform_url_changed;
}
}
$variables['attributes']['title'] = check_plain($info['title']);
$variables['icon_path'] = social_media_links_icon($icon_style[0], $name, $icon_style[1]);
$variables['icon_alt'] = isset($info['image alt']) ? $info['image alt'] : $info['title'] . ' ' . t('icon');
}
/**
* Get the available platforms.
*
* @return array
* Returns a list of platforms.
*/
function social_media_links_platforms() {
$platforms =& drupal_static(__FUNCTION__);
// Grab from cache or build the array.
if (isset($platforms)) {
return $platforms;
}
include_once 'social_media_links.platforms.inc';
$platforms = array();
$platforms = module_invoke_all('social_media_links_platform_info');
drupal_alter('social_media_links_platform_info', $platforms);
return $platforms;
}
/**
* Get all registered icon sets.
*
* @return array
* Return a list of all registered platforms.
*/
function social_media_links_iconsets() {
$sets =& drupal_static(__FUNCTION__);
// Grab from cache or build the array.
if (isset($sets)) {
return $sets;
}
include_once 'social_media_links.iconsets.inc';
$sets = array();
$sets = module_invoke_all('social_media_links_iconset_info');
foreach ($sets as $key => $set) {
if ($path = social_media_links_iconset_path($key)) {
$sets[$key]['path'] = $path;
}
}
drupal_alter('social_media_links_iconset_info', $sets);
return $sets;
}
/**
* Get informations about a specific icon set.
*
* @param string $iconset
* Name of the icon set.
*
* @return array
* Informations about the icon set.
*/
function social_media_links_iconset($iconset) {
$sets = social_media_links_iconsets();
return isset($sets[$iconset]) ? $sets[$iconset] : FALSE;
}
/**
* Get the path of a platform icon.
*
* @param string $iconset
* Name of the icon set.
* @param string $platform
* Name of the platform.
* @param string $style
* Icon size.
*
* @return string
* Path to the icon image.
*/
function social_media_links_icon($iconset, $platform, $style) {
$iconset = social_media_links_iconset($iconset);
return call_user_func($iconset['path callback'], $platform, $style);
}
/**
* Returns an array with the possible locations of the installed iconsets.
*
* @param string $iconset
* Optional: Name of the iconset.
*
* @return array
* Possible path locations of the iconsets.
*/
function social_media_links_searchdirs($iconset = '') {
$searchdirs = array();
$profile = drupal_get_path('profile', drupal_get_profile());
$config = conf_path();
// Similar to 'modules' and 'themes' directories in the root directory,
// certain distributions may want to place libraries into a 'libraries'
// directory in Drupal's root directory.
$searchdirs[] = 'libraries';
// Similar to 'modules' and 'themes' directories inside an installation
// profile, installation profiles may want to place libraries into a
// 'libraries' directory. But exclude/ignore the standard drupal profiles.
if (!in_array($profile, array(
'minimal',
'standard',
'testing',
))) {
$searchdirs[] = "{$profile}/libraries";
}
// Always search sites/all/libraries.
$searchdirs[] = 'sites/all/libraries';
// Also search sites/<domain>/libraries.
$searchdirs[] = "{$config}/libraries";
if (!empty($iconset)) {
foreach ($searchdirs as $key => $dir) {
$searchdirs[$key] = $dir . '/' . $iconset;
}
}
return $searchdirs;
}
/**
* Return an array of library directories.
*
* @return array
* A list of library directories.
*/
function social_media_links_libraries() {
$directories =& drupal_static(__FUNCTION__);
// Grab from cache or build the array.
if (isset($directories)) {
return $directories;
}
$searchdirs = social_media_links_searchdirs();
// Add the social_media_links module directory.
$searchdirs[] = drupal_get_path('module', 'social_media_links') . '/libraries';
$directories = array();
$nomask = array(
'CVS',
);
foreach ($searchdirs as $dir) {
if (is_dir($dir) && ($handle = opendir($dir))) {
while (FALSE !== ($file = readdir($handle))) {
if (!in_array($file, $nomask) && $file[0] != '.') {
if (is_dir("{$dir}/{$file}")) {
$directories[$file] = "{$dir}/{$file}";
}
}
}
closedir($handle);
}
}
return $directories;
}
/**
* Return the library path of an icon set.
*
* @param string $iconset
* Name of the icon set.
*
* @return string
* Path of the icon set.
*/
function social_media_links_iconset_path($iconset) {
$path = FALSE;
$libraries = social_media_links_libraries();
if (array_key_exists($iconset, $libraries)) {
$path = $libraries[$iconset];
}
return $path;
}
/**
* Get the list options for the icon set style select.
*
* @return array
* Array with the select options.
*/
function social_media_links_iconsets_options() {
$iconsets = social_media_links_iconsets();
$options = array();
foreach ($iconsets as $iconset_name => $iconset) {
if (!empty($iconset['path'])) {
foreach ($iconset['styles'] as $sid => $style) {
$options[$iconset['name']][$iconset_name . ':' . $sid] = $style;
}
}
}
return $options;
}
/**
* Theme function for the platforms.
*/
function theme_social_media_links_platforms(&$variables) {
$output = '';
$platforms = $variables['platforms'];
$attributes = $variables['attributes'];
// Open the ul element.
$output .= '<ul' . drupal_attributes($attributes) . '>';
$num_platforms = count($platforms);
$i = 1;
foreach ($platforms as $name => $platform) {
$class = array(
$name,
);
// Add first and last classes to the list of platforms to help out themers.
if ($i == 1) {
$class[] = 'first';
}
if ($i == $num_platforms) {
$class[] = 'last';
}
$output .= '<li ' . drupal_attributes(array(
'class' => $class,
)) . '>';
// Render the platform item.
$output .= drupal_render($platform);
$output .= '</li>';
$i++;
}
// Close the ul element.
$output .= '</ul>';
return $output;
}
/**
* Theme function for a single platform element.
*/
function theme_social_media_links_platform(&$variables) {
$output = '';
$icon = theme_image(array(
'path' => $variables['icon_path'],
'alt' => $variables['icon_alt'],
'attributes' => array(),
));
$options = array();
$options['attributes'] = $variables['attributes'];
$options['html'] = TRUE;
$output .= l($icon, $variables['link'], $options);
if (!empty($variables['appearance']['show_name'])) {
if ($variables['appearance']['orientation'] == 'h') {
$output .= '<br />';
}
$title = check_plain($variables['attributes']['title']);
$output .= '<span>' . l($title, $variables['link'], $options) . '</span>';
}
return $output;
}
/**
* Returns HTML for the platform table with tabledrag.
*/
function theme_social_media_links_platforms_table(&$variables) {
$form = $variables['form'];
$rows = array();
foreach (element_children($form) as $platform) {
$form[$platform]['weight']['#attributes']['class'] = array(
'platforms-order-weight',
);
$rows[] = array(
'data' => array(
'<strong>' . $form[$platform]['platform_value']['#title'] . '<strong>',
array(
'class' => array(
'platform-cross',
),
'data' => drupal_render($form[$platform]['platform_value']),
),
array(
'class' => array(
'tabledrag-hide',
),
'data' => drupal_render($form[$platform]['weight']),
),
),
'class' => array(
'draggable',
),
);
}
$header = array(
t('Platform'),
'',
array(
'class' => array(
'tabledrag-hide',
),
'data' => t('Weight'),
),
);
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
'id' => 'platforms-order',
),
));
drupal_add_tabledrag('platforms-order', 'order', 'silbing', 'platforms-order-weight');
return $output;
}
/**
* Helper function to clean up the platforms array from the empty
* values.
*
* @param array $platforms
* The array with the platforms.
*
* @return array
* The cleaned platforms array.
*/
function _social_media_links_cleanup_platforms($platforms) {
foreach ((array) $platforms as $key => $platform) {
if (empty($platform['platform_value'])) {
unset($platforms[$key]);
}
}
return $platforms;
}
/**
* Helper function to generate the icon set example table.
*
* @param array $platforms
* Keyed array with all available platforms.
* @param array $iconset
* Array with the information about the icon set.
*
* @return string
* The generated table markup.
*/
function _social_media_links_generate_example_table($platforms, $iconset) {
if (!is_array($platforms) && count($platforms) == 0) {
return '';
}
$groups = array_chunk($platforms, 10, TRUE);
$output = '';
// Use the first iconset style for the sample table.
$style = key($iconset['styles']);
foreach ($groups as $group) {
$header = array();
$row = array();
foreach ($group as $platform_name => $platform) {
$header[] = array(
'data' => $platform['title'],
'style' => 'text-align: center; ',
);
$vars = array(
'path' => call_user_func($iconset['path callback'], $platform_name, $style),
);
if (file_exists($vars['path'])) {
$row[] = array(
'data' => theme('image', $vars) . ' ',
'style' => 'text-align: center;',
);
}
else {
$row[] = array(
'data' => '–',
'style' => 'text-align: center;',
);
}
}
$vars = array(
'header' => $header,
'rows' => array(
'data' => $row,
),
);
$output .= theme('table', $vars);
$output .= '<br />';
}
return $output;
}
Functions
Name![]() |
Description |
---|---|
social_media_links_block_configure | Implements hook_block_configure(). |
social_media_links_block_info | Implements hook_block_info(). |
social_media_links_block_save | Implements hook_block_save(). |
social_media_links_block_view | Implements hook_block_view(). |
social_media_links_ctools_plugin_directory | Implements hook_ctools_plugin_directory(). |
social_media_links_form | |
social_media_links_icon | Get the path of a platform icon. |
social_media_links_iconset | Get informations about a specific icon set. |
social_media_links_iconsets | Get all registered icon sets. |
social_media_links_iconsets_options | Get the list options for the icon set style select. |
social_media_links_iconset_path | Return the library path of an icon set. |
social_media_links_libraries | Return an array of library directories. |
social_media_links_platforms | Get the available platforms. |
social_media_links_searchdirs | Returns an array with the possible locations of the installed iconsets. |
social_media_links_theme | Implements hook_theme(). |
template_preprocess_social_media_links_platform | Processes variables for social-media-links-platform.tpl.php. |
template_preprocess_social_media_links_platforms | Preprocesses variables for social media links platforms. |
theme_social_media_links_platform | Theme function for a single platform element. |
theme_social_media_links_platforms | Theme function for the platforms. |
theme_social_media_links_platforms_table | Returns HTML for the platform table with tabledrag. |
_social_media_links_cleanup_platforms | Helper function to clean up the platforms array from the empty values. |
_social_media_links_generate_example_table | Helper function to generate the icon set example table. |