I love this module. The AddToAny field added to content types makes working with the display of these buttons very convenient in most cases. I am however a bit perplexed by the fact that the AddToAny block is restricted to nodes. This is not intuitive and seems like an arbitrary restriction.

It seems quite logical to me that a site builder may need to display these social media buttons site wide (for the site home page, a view page, the contact page, etc) and I cannot for the life of me figure out how to do that. I dug into the code, looking at the AddToAnyBlock.php file (source for this block) and it looks like this block is absolutely restricted to nodes.

<?php

namespace Drupal\addtoany\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;

/**
 * Provides an 'AddToAny' block.
 *
 * @Block(
 *   id = "addtoany_block",
 *   admin_label = @Translation("AddToAny buttons"),
 * )
 */
class AddToAnyBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];
    $node = \Drupal::routeMatch()->getParameter('node');
    if (is_numeric($node)) {
      $node = Node::load($node);
    }

    if ($node instanceof NodeInterface) {
      $data = addtoany_create_entity_data($node);
      $build = [
        '#addtoany_html'              => \Drupal::token()->replace($data['addtoany_html'], ['node' => $node]),
        '#link_url'                   => $data['link_url'],
        '#link_title'                 => $data['link_title'],
        '#button_setting'             => $data['button_setting'],
        '#button_image'               => $data['button_image'],
        '#universal_button_placement' => $data['universal_button_placement'],
        '#buttons_size'               => $data['buttons_size'],
        '#theme'                      => 'addtoany_standard',
        '#cache'                      => [
          'contexts' => ['url'],
        ],
      ];
    }

    return $build;
  }

}

Comments

JayDarnell created an issue. See original summary.

jaydarnell’s picture

StatusFileSize
new2.09 KB

This is my first time digging into the code for this module but considering the fact addtoany_create_entity_data just grabs some node information to then subsequently pass to addtoany_create_data is seems logical we could conditionally skip the previous function and go directly to the later if the page being viewed isn't a node. I've supplied a patch below.

jaydarnell’s picture

Status: Active » Needs review
Irisibk’s picture

Patch #2 fixed this issue for me. Thanks Jay!

codechefmarc’s picture

I just updated to 8.x-1.15 recently and our sharing buttons went away on taxonomy term pages. Patch #2 seems to have fixed it. Thanks!

  • micropat committed 9414337 on 8.x-1.x authored by JayDarnell
    Issue #3208011 by JayDarnell: AddToAny block should work for all pages (...
micropat’s picture

Status: Needs review » Fixed

Thanks for the patch Jay, and thanks for testing Iris!

Pushed to dev and will be a wonderful feature update in the next release.

jaydarnell’s picture

Happy to help! Thank you for all your work on this extremely useful module micropat.

Status: Fixed » Closed (fixed)

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

nathan tsai’s picture

@micropat, could you create a new release soon? Looking forward to this feature without using a patch :)

Thanks again for all your work on this handy module!

micropat’s picture

Thanks for the ping @nathan-tsai — 8.x.1.16 just released! Thanks again Jay!