Hey,

is there a way to include the share buttons somewhere in my template, so that i can controle exactly where it appears? I tried

<?php $share = module_invoke('addtoany', 'block_view', 'addtoany_button');
          print render($share['content']); ?>

But the popup stoped working :/

It would be awesome to have a documentation that shows some php code snips for printing this module in the template.

Greets

SG

Comments

sgurlt’s picture

Issue summary: View changes

Add info

sgurlt’s picture

Issue summary: View changes

Add info

gregory claeyssens’s picture

Issue summary: View changes

Try it like this:

<?php global $_addtoany_init;
                $_addtoany_init = TRUE;
                $share = module_invoke('addtoany', 'block_view', 'addtoany_button');
          print render($share['content']); ?>
m.lebedev’s picture

Not working social links in modal. addtoany module is not initialized

Media Crumb’s picture

trying this on a page template doesnt work. Anyway to get this to work on page templates?

JRF’s picture

I've noticed that the buttons don't appear on templates that don't include :
print render($page['content']);
If someone can explain me why there is this dependency ? (and how can I remove it ?)

rooby’s picture

If you are using the built in node functionality you can do this in your node template file:

Put this in your file somewhere above the line that does print render($content);:

<?php
    // We hide the addtoany buttons and links now so that we can render them later.
    hide($content['addtoany']);
?>

Then wherever you want to print the buttons do this:

<?php print render($content['addtoany']); ?>

If you want to print the block somewhere you can use the information at https://www.drupal.org/node/26502

The short version of that is...

To print just the block contents:

<?php
$block = module_invoke('addtoany', 'block_view', 'addtoany_button');
print render($block['content']);
?>

To print the full block with block title, using the proper block template:

<?php
  $block = block_load('addtoany', 'addtoany_button');
  print drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
?>

I would generally use the latter version.

Also, if you just want to print the buttons markup somewhere but not use the block you can apply the patch at #2542488: Make _addtoany_create_button() non-private and split out node specific functionality and do this for the current page:

<?php
  // Print the addtoany buttons for sharing the current page.
  print addtoany_create_buttons();
?>

Or this for a specific page:

<?php
  print addtoany_create_buttons(t('Page title'), '/url/of/page/to/share');
?>

Or an external URL:

<?php
  print addtoany_create_buttons(t('Page title'), 'http://example.com');
?>
rooby’s picture

Version: 7.x-3.0 » 7.x-4.9
rooby’s picture

@JRF re #4:

Because the addtoany buttons are added to the node content and the default for the node template is to print print render($page['content']); to print all content at once.

See my comment #5 for info on how to stop printing it there and print it manually elsewhere.

rooby’s picture

Status: Active » Fixed

I think #5 covers it.

Let me know if it doesn't

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

skribbz14’s picture

My problem turned out to be that you cannot print the addtoany block within the page.tpl.php, but you can in the node.tpl.php. Heads up to anyone trying to figure out why these answers aren't working for them!

abarpetia’s picture

#10: thanks for heads up. You also need to change placement setting on the configuration. For ex: #5 work around will only work if you have "display in content section" check box ticked. If you have "Display in link section" then you need to put this code above print render($content['links']); this.

Does any one got this working in page.tpl.php? I have a requirement to put this button into different region of page.tpl.php.

***UPDATE***
Got this working in page.tpl.php. Following are steps

  • Enable "display in content section" check box.
  • Put this in your file somewhere above the line that does print render($content);:
<?php
    // We hide the addtoany buttons and links now so that we can render them later.
    hide($content['addtoany']);
?>
  • Add Standalone Service button to you page.tpl.php like follow
  • <div class="a2a_kit a2a_kit_size_32 a2a_default_style">
        <a class="a2a_button_facebook"></a>
        <a class="a2a_button_twitter"></a>
        <a class="a2a_button_google_plus"></a>
        <a class="a2a_button_pinterest"></a>
        <a class="a2a_dd" href="https://www.addtoany.com/share"></a>
    </div>
  • clear caches and Done
  • Standalone Service Buttons

    rooby’s picture

    @abarpetia:

    Also see my comment #5 regarding the addtoany_create_buttons() function, which no longer requires a patch to use.

    bobby endarr ramdin’s picture

    TWIG node file
    {{ content.addtoany }}

    halth’s picture

    For anyone landing in here and even maybe trying to accomplish it in a different way, make sure you're rendering $page['content']; to the node's corresponding page template file:

    print render($page['content']);

    Addtoany attaches it's javascripts to the page's (not node's) content element:

    function addtoany_page_alter(&$page) {
      // ...  
    
      $page['content']['#attached']['js'][] = array(
        'data'  => $javascript_header,
        'scope' => 'header',
        'type'  => 'inline',
      );
    
      $page['content']['#attached']['js'][] = array(
        'data'  => $javascript_footer,
        'scope' => 'footer',
        'type'  => 'inline',
      );
    
      // ...
    }