summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Glastra2011-09-19 15:56:41 (GMT)
committer Matthias Glastra2011-09-19 15:56:41 (GMT)
commit25b6bbc64d82405d6652f11055dc0094466d29a4 (patch)
tree3dac9dc17fae402233453e6fb70a9cb4cd8a6708
parent5adfa0dedf675ff0e88e1acdbcb81ed9cf4746b7 (diff)
Changed some thing functions
My initial ideas about drupal was working where not right. Drupal does create nice class attributes for tags based on a array. And I split the wrapper element and the simple element.
-rw-r--r--addthis.module68
-rw-r--r--classes/AddThis.php92
2 files changed, 87 insertions, 73 deletions
diff --git a/addthis.module b/addthis.module
index 8e30045..fd74793 100644
--- a/addthis.module
+++ b/addthis.module
@@ -98,13 +98,51 @@ function addthis_form_alter(&$form, &$form_state, $form_id) {
*/
function addthis_theme($existing, $type, $theme, $path) {
return array(
+ 'addthis_wrapper' => array(
+ 'render element' => 'addthis_wrapper'
+ ),
'addthis_element' => array(
'render element' => 'addthis_element'
+ ),
+ 'addthis' => array(
+ 'variables' => array(
+ 'format' => NULL,
+ 'options' => NULL
+ )
)
);
}
/**
+ * Implementation hook_preprocess() for theme_addthis.
+ */
+function template_preprocess_addthis(&$variables) {
+ dpm($variables);
+}
+
+function theme_addthis($variables) {
+ dpm($variables);
+ return render($variables);
+}
+
+function theme_addthis_wrapper($variables) {
+ // Get the element
+ $element = $variables['addthis_wrapper'];
+
+ // Render the html
+ $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>';
+
+ $children = element_children($element);
+ if (count($children) > 0) {
+ foreach($children as $child) {
+ $output .= render($element[$child]);
+ }
+ }
+ $output .= '</' . $element['#tag'] . ">\n";
+ return $output;
+}
+
+/**
* Theme the elements that are created in the AddThis module. This is
* created with hook_addthis_element.
*/
@@ -112,36 +150,6 @@ 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";
diff --git a/classes/AddThis.php b/classes/AddThis.php
index d65452c..8649580 100644
--- a/classes/AddThis.php
+++ b/classes/AddThis.php
@@ -259,13 +259,13 @@ class AddThis {
// This is a render array representation.
$element = array(
// Note that theme_wrappers needs an array.
- '#theme_wrappers' => array('addthis_element'),
+ '#theme' => 'addthis_wrapper',
'#tag' => 'a',
- '#classes' => array(
- 'addthis_button',
- ),
'#attributes' => array(
'href' => self::getFullBookmarkUrl(),
+ 'class' => array(
+ 'addthis_button',
+ )
)
);
$element['#attributes'] += $this->getAddThisAttributesMarkup($entity);
@@ -274,9 +274,6 @@ class AddThis {
$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',
@@ -293,13 +290,13 @@ class AddThis {
private function getCompactButtonWidgetMarkup($entity) {
// Create wrapper element a
$element = array(
- '#theme_wrappers' => array('addthis_element'),
+ '#theme' => 'addthis_wrapper',
'#tag' => 'a',
- '#classes' => array(
- 'addthis_button'
- ),
'#attributes' => array(
- 'href' => $this->getFullBookmarkUrl()
+ 'href' => $this->getFullBookmarkUrl(),
+ 'class' => array(
+ 'addthis_button'
+ )
)
);
$element['#attributes'] += self::getAddThisAttributesMarkup($entity);
@@ -308,7 +305,6 @@ class AddThis {
$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',
@@ -328,14 +324,15 @@ class AddThis {
$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'),
+ '#theme' => 'addthis_wrapper',
'#tag' => 'div',
- '#classes' => array(
- 'addthis_toolbox',
- 'addthis_default_style',
- trim($this->getLargeButtonsClass()),
+ '#attributes' => array(
+ 'class' => array(
+ 'addthis_toolbox',
+ 'addthis_default_style',
+ trim($this->getLargeButtonsClass()),
+ )
),
- '#attributes' => array(),
);
$element['#attributes'] += $this->getAddThisAttributesMarkup($entity);
@@ -344,11 +341,12 @@ class AddThis {
$items[] = array(
'#theme' => 'addthis_element',
'#tag' => 'a',
+ '#value' => '',
'#attributes' => array(
- 'href' => $this->getFullBookmarkUrl()
- ),
- '#classes' => array(
- 'addthis_button_compact'
+ 'href' => $this->getFullBookmarkUrl(),
+ 'class' => array(
+ 'addthis_button_compact'
+ ),
),
);
$numberOfPreferredServices = self::getNumberOfPreferredServices();
@@ -356,11 +354,12 @@ class AddThis {
$items[] = array(
'#theme' => 'addthis_element',
'#tag' => 'a',
+ '#value' => '',
'#attributes' => array(
- 'href' => $this->getFullBookmarkUrl()
- ),
- '#classes' => array(
- ('addthis_button_preferred_' . $i),
+ 'href' => $this->getFullBookmarkUrl(),
+ 'class' => array(
+ ('addthis_button_preferred_' . $i),
+ )
),
);
}
@@ -368,27 +367,31 @@ class AddThis {
$items[] = $this->getFacebookLikeButtonMarkup();
$items[] = $this->getGooglePlusOneButtonMarkup();
$element += $items;
-
+
return $element;
}
private function getSharecountWidgetMarkup($entity) {
$element = array(
- '#theme_wrappers' => array('addthis_element'),
+ '#theme' => 'addthis_wrapper',
'#tag' => 'div',
- '#classes' => array(
- 'addthis_toolbox',
- 'addthis_default_style'
+ '#attributes' => array(
+ 'class' => array(
+ 'addthis_toolbox',
+ 'addthis_default_style'
+ )
),
);
// <a> element with counter
$item = array(
'#theme' => 'addthis_element',
'#tag' => 'a',
- '#classes' => array(
- 'addthis_counter'
+ '#value' => '',
+ '#attributes' => array(
+ 'class' => array(
+ 'addthis_counter'
+ ),
),
- '#attributes' => array(),
);
$item['#attributes'] += $this->getAddThisAttributesMarkup($entity);
$element[] = $item;
@@ -423,10 +426,11 @@ class AddThis {
$element = NULL;
if ($this->isTwitterEnabled()) {
$element = array(
- '#theme_wrappers' => array('addthis_element'),
+ '#theme' => 'addthis_element',
'#tag' => 'a',
- '#classes' => array(
- 'addthis_button_tweet'
+ '#value' => '',
+ '#attributes' => array(
+ 'class' => array('addthis_button_tweet')
),
);
}
@@ -439,10 +443,11 @@ class AddThis {
$element = array(
'#theme' => 'addthis_element',
'#tag' => 'a',
- '#classes' => array(
- 'addthis_button_facebook_like'
- ),
+ '#value' => '',
'#attributes' => array(
+ 'class' => array(
+ 'addthis_button_facebook_like'
+ ),
'fb:like:layout' => 'button_count'
)
);
@@ -456,8 +461,9 @@ class AddThis {
$element = array(
'#theme' => 'addthis_element',
'#tag' => 'a',
- '#classes' => array(
- 'addthis_button_google_plusone'
+ '#value' => '',
+ '#attributes' => array(
+ 'class' => array('addthis_button_google_plusone')
)
);
}