summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJani Palsamäki2011-09-08 09:56:01 (GMT)
committer Jani Palsamäki2011-09-08 09:56:01 (GMT)
commit5b48493d737289907223e0f23ee7c7e56a30d7de (patch)
tree2a1c27ef4c65b7ee11899236c5cd84a1b27f911d
parentd4bb00ca804aa8a5203c0744effd9ce43f820f87 (diff)
Merged code contributed by Matthias Glastra. The output is now rendered via Render API.
-rw-r--r--addthis.module67
-rw-r--r--classes/AddThis.php213
-rw-r--r--includes/addthis.field.inc7
3 files changed, 240 insertions, 47 deletions
diff --git a/addthis.module b/addthis.module
index ada13c1..8e30045 100644
--- a/addthis.module
+++ b/addthis.module
@@ -92,3 +92,70 @@ function addthis_form_alter(&$form, &$form_state, $form_id) {
$form['instance']['default_value_widget']['#access'] = FALSE;
}
}
+
+/**
+ * Implementation of hook_theme().
+ */
+function addthis_theme($existing, $type, $theme, $path) {
+ return array(
+ 'addthis_element' => array(
+ 'render element' => 'addthis_element'
+ )
+ );
+}
+
+/**
+ * Theme the elements that are created in the AddThis module. This is
+ * created with hook_addthis_element.
+ */
+function theme_addthis_element($variables) {
+ // Get the element
+ $element = $variables['addthis_element'];
+
+ // Refactor the classes in the array #classes if there.
+ if (isset($element['#classes']) && count($element['#classes']) > 0) {
+ $att_class = implode(' ', $element['#classes']);
+ if (isset($element['#attributes']['class'])) {
+ $att_class = trim($element['#attributes']['class']) . ' ' . $att_class;
+ }
+ // Finally implode it and add it to or override it in the attributes.
+ $element['#attributes']['class'] = $att_class;
+ }
+
+ // When rendered children are passed put them in the value
+ // to wrap the element we are rendering. If #children is there it means
+ // we are called as theme_wrapper.
+ if (isset($element['#children']) && !empty($element['#children'])) {
+ $element['#value'] = $element['#children'];
+ }
+
+ // Set default if not there
+ if (!isset($element['#tag_empty'])) {
+ $element['#tag_empty'] = FALSE;
+ }
+ // When the #theme_wrappers is set we can't render a empty tag.
+ // Or if #tag_empty is set to FALSE.
+ if (isset($element['#theme_wrappers']) && !isset($element['#value']) ||
+ isset($element['#tag_empty']) && $element['#tag_empty'] === FALSE && !isset($element['#value'])) {
+ $element['#value'] = '';
+ // Note that when #theme is set and not #theme_wrappers the tag will
+ // always be rendered not empty unless #tag_empty is set to TRUE.
+ }
+
+ // Render the html
+ if (!isset($element['#value'])) {
+ return '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . " />\n";
+ }
+ else {
+ $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>';
+ if (isset($element['#value_prefix'])) {
+ $output .= $element['#value_prefix'];
+ }
+ $output .= $element['#value'];
+ if (isset($element['#value_suffix'])) {
+ $output .= $element['#value_suffix'];
+ }
+ $output .= '</' . $element['#tag'] . ">\n";
+ return $output;
+ }
+}
diff --git a/classes/AddThis.php b/classes/AddThis.php
index 1fab420..2579fe9 100644
--- a/classes/AddThis.php
+++ b/classes/AddThis.php
@@ -271,63 +271,163 @@ class AddThis {
}
private function getLargeButtonWidgetMarkup($entity) {
- return '<a class="addthis_button" '
- . $this->getAddThisAttributesMarkup($entity)
- . $this->markupGenerator->generateAttribute('href', self::getFullBookmarkUrl())
- . '><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="'
- . t('Bookmark and Share')
- . '" style="border:0"/></a>';
+ // This is a render array representation.
+ $element = array(
+ // Note that theme_wrappers needs an array.
+ '#theme_wrappers' => array('addthis_element'),
+ '#tag' => 'a',
+ '#classes' => array(
+ 'addthis_button',
+ ),
+ '#attributes' => array(
+ 'href' => self::getFullBookmarkUrl(),
+ )
+ );
+ $element['#attributes'] += $this->getAddThisAttributesMarkup($entity);
+
+ // Here we start the children of the a element.
+ $image = array(
+ '#theme' => 'addthis_element',
+ '#tag' => 'img',
+ '#tag_empty' => TRUE,
+ '#classes' => array(
+ ),
+ '#attributes' => array(
+ 'src' => 'http://s7.addthis.com/static/btn/v2/lg-share-en.gif',
+ 'width' => '125',
+ 'height' => '16',
+ 'alt' => t('Bookmark and Share'),
+ 'style' => 'border:0;'
+ )
+ );
+ $element[] = $image;
+
+ return $element;
}
private function getCompactButtonWidgetMarkup($entity) {
- return '<a class="addthis_button" '
- . self::getAddThisAttributesMarkup($entity)
- . $this->markupGenerator->generateAttribute('href', $this->getFullBookmarkUrl())
- . '><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="'
- . t('Bookmark and Share')
- . '" style="border:0"/></a>';
+ // Create wrapper element a
+ $element = array(
+ '#theme_wrappers' => array('addthis_element'),
+ '#tag' => 'a',
+ '#classes' => array(
+ 'addthis_button'
+ ),
+ '#attributes' => array(
+ 'href' => $this->getFullBookmarkUrl()
+ )
+ );
+ $element['#attributes'] += self::getAddThisAttributesMarkup($entity);
+
+ // Create img button
+ $image = array(
+ '#theme' => 'addthis_element',
+ '#tag' => 'img',
+ '#tag_empty' => TRUE,
+ '#attributes' => array(
+ 'src' => 'http://s7.addthis.com/static/btn/sm-share-en.gif',
+ 'width' => '83',
+ 'height' => '16',
+ 'alt' => t('Bookmark and Share'),
+ 'style' => 'border:0;',
+ ),
+ );
+ $element[] = $image;
+
+ // Return element to render.
+ return $element;
}
private function getToolboxWidgetMarkup($entity) {
- $markup = '<div class="addthis_toolbox addthis_default_style '
- . $this->getLargeButtonsClass()
- . '" '
- . $this->getAddThisAttributesMarkup($entity)
- . '><a '
- . $this->markupGenerator->generateAttribute('href', $this->getFullBookmarkUrl())
- . ' class="addthis_button_compact"></a>';
-
+ // Creating render array for the widget.
+ $element = array(
+ // We use #theme_wrappers to include the rendered children otherwise
+ // we only get a non empty element like <div></div>.
+ '#theme_wrappers' => array('addthis_element'),
+ '#tag' => 'div',
+ '#classes' => array(
+ 'addthis_toolbox',
+ 'addthis_default_style',
+ trim($this->getLargeButtonsClass()),
+ ),
+ '#attributes' => array(),
+ );
+ $element['#attributes'] += $this->getAddThisAttributesMarkup($entity);
+
+ // All service elements
+ $items = array();
+ $items[] = array(
+ '#theme' => 'addthis_element',
+ '#tag' => 'a',
+ '#attributes' => array(
+ 'href' => $this->getFullBookmarkUrl()
+ ),
+ '#classes' => array(
+ 'addthis_button_compact'
+ ),
+ );
$numberOfPreferredServices = self::getNumberOfPreferredServices();
-
for ($i = 1; $i <= $numberOfPreferredServices; $i++) {
- $markup .= "<a class=\"addthis_button_preferred_$i\"></a>";
+ $items[] = array(
+ '#theme' => 'addthis_element',
+ '#tag' => 'a',
+ '#attributes' => array(
+ 'href' => $this->getFullBookmarkUrl()
+ ),
+ '#classes' => array(
+ ('addthis_button_preferred_' . $i),
+ ),
+ );
}
+ $items[] = $this->getTwitterButtonMarkup();
+ $items[] = $this->getFacebookLikeButtonMarkup();
+ $items[] = $this->getGooglePlusOneButtonMarkup();
+ $element += $items;
- $markup .= $this->getTwitterButtonMarkup();
- $markup .= $this->getFacebookLikeButtonMarkup();
- $markup .= $this->getGooglePlusOneButtonMarkup();
- $markup .= '</div>';
-
- return $markup;
+ return $element;
}
private function getSharecountWidgetMarkup($entity) {
- return '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
- . $this->getAddThisAttributesMarkup($entity)
- . '></a></div>';
+ $element = array(
+ '#theme_wrappers' => array('addthis_element'),
+ '#tag' => 'div',
+ '#classes' => array(
+ 'addthis_toolbox',
+ 'addthis_default_style'
+ ),
+ );
+ // <a> element with counter
+ $item = array(
+ '#theme' => 'addthis_element',
+ '#tag' => 'a',
+ '#classes' => array(
+ 'addthis_counter'
+ ),
+ '#attributes' => array(),
+ );
+ $item['#attributes'] += $this->getAddThisAttributesMarkup($entity);
+ $element[] = $item;
+
+ return $element;
}
private function getAddThisAttributesMarkup($entity) {
if (is_object($entity)) {
- return $this->getAddThisTitleAttributeMarkup($entity) . ' ';
+ $attributes = array();
+
+ // Add title
+ $attributes += $this->getAddThisTitleAttributeMarkup($entity);
+
+ // Return the array with attributes
+ return $attributes;
}
- return '';
+ return array();
}
private function getAddThisTitleAttributeMarkup($entity) {
- return $this->markupGenerator->generateAttribute(
- self::TITLE_ATTRIBUTE, drupal_get_title() . ' - ' . check_plain($entity->title)
- );
+ return array(
+ self::TITLE_ATTRIBUTE => (drupal_get_title() . ' - ' . check_plain($entity->title))
+ );
}
private function getLargeButtonsClass() {
@@ -335,27 +435,48 @@ class AddThis {
}
private function getTwitterButtonMarkup() {
- $markup = '';
+ $element = NULL;
if ($this->isTwitterEnabled()) {
- $markup = '<a class="addthis_button_tweet"></a>';
+ $element = array(
+ '#theme_wrappers' => array('addthis_element'),
+ '#tag' => 'a',
+ '#classes' => array(
+ 'addthis_button_tweet'
+ ),
+ );
}
- return $markup;
+ return $element;
}
private function getFacebookLikeButtonMarkup() {
- $markup = '';
+ $element = NULL;
if ($this->isFacebookLikeEnabled()) {
- $markup = '<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>';
+ $element = array(
+ '#theme' => 'addthis_element',
+ '#tag' => 'a',
+ '#classes' => array(
+ 'addthis_button_facebook_like'
+ ),
+ '#attributes' => array(
+ 'fb:like:layout' => 'button_count'
+ )
+ );
}
- return $markup;
+ return $element;
}
private function getGooglePlusOneButtonMarkup() {
- $markup = '';
+ $element = NULL;
if ($this->isGooglePlusOneEnabled()) {
- $markup = '<a class="addthis_button_google_plusone"></a>';
+ $element = array(
+ '#theme' => 'addthis_element',
+ '#tag' => 'a',
+ '#classes' => array(
+ 'addthis_button_google_plusone'
+ )
+ );
}
- return $markup;
+ return $element;
}
private function getServiceNamesAsCommaSeparatedString() {
@@ -374,7 +495,7 @@ class AddThis {
}
private function getFullBookmarkUrl() {
- return check_url($this->getBaseBookmarkUrl() . $this->getProfileIdQueryParameterPrefixedWithAmp());
+ return $this->getBaseBookmarkUrl() . $this->getProfileIdQueryParameterPrefixedWithAmp();
}
private function getProfileIdQueryParameter($prefix) {
diff --git a/includes/addthis.field.inc b/includes/addthis.field.inc
index 694e8ee..92c1285 100644
--- a/includes/addthis.field.inc
+++ b/includes/addthis.field.inc
@@ -62,8 +62,13 @@ function addthis_field_formatter_view($entity_type, $entity, $field, $instance,
$element = array();
$widget_type = $display['type'];
$markup = AddThis::getInstance()->getWidgetMarkup($widget_type, $entity);
- if (!isset($element[0])) {
+ if (!isset($element[0]) && !is_array($markup)) {
+ // Supporting #markup is for legacy in this module.
+ // We move towards using the render array.
$element[0] = array('#markup' => $markup);
+ }
+ elseif (!isset($element[0])) {
+ $element[0] = $markup;
}
return $element;
}