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;
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | addtoany-generalize-block-3208011-2.patch | 2.09 KB | jaydarnell |
Comments
Comment #2
jaydarnellThis 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.
Comment #3
jaydarnellComment #4
Irisibk commentedPatch #2 fixed this issue for me. Thanks Jay!
Comment #5
codechefmarcI 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!
Comment #7
micropat commentedThanks for the patch Jay, and thanks for testing Iris!
Pushed to dev and will be a wonderful feature update in the next release.
Comment #8
jaydarnellHappy to help! Thank you for all your work on this extremely useful module micropat.
Comment #10
nathan tsai commented@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!
Comment #11
micropat commentedThanks for the ping @nathan-tsai — 8.x.1.16 just released! Thanks again Jay!