function sharethis_node_view in ShareThis 8.2
Same name and namespace in other branches
- 7.2 sharethis.module \sharethis_node_view()
- 7 sharethis.module \sharethis_node_view()
Implements hook_ENTITY_TYPE_view().
File
- ./
sharethis.module, line 76 - A module that adds one of the ShareThis widget to your website.
Code
function sharethis_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
// Break if node has not yet been saved.
if ($node
->id() === NULL) {
return;
}
$sharethis_manager = \Drupal::service('sharethis.manager');
$sharethis_settings = \Drupal::config('sharethis.settings');
// Don't display if the user is currently searching, or in the RSS feed.
switch ($view_mode) {
case 'search_result':
case 'search_index':
case 'rss':
return;
}
// First get all of the options for the sharethis widget from the database:
$data_options = $sharethis_manager
->getOptions();
// Get the full path to insert into the Share Buttons.
$path = $node
->toUrl()
->setAbsolute()
->toString();
$title = $node
->getTitle();
// Check where we want to display the ShareThis widget.
switch ($sharethis_settings
->get('location')) {
case 'content':
$enabled_types = $data_options['node_types'];
if (isset($enabled_types[$node
->bundle()]) && $enabled_types[$node
->bundle()] === $node
->bundle() && $display
->getcomponent('sharethis')) {
$st_js = $sharethis_manager
->sharethisIncludeJs();
$content = $sharethis_manager
->renderSpans($data_options, $title, $path);
$build['sharethis'] = [
'#theme' => 'sharethis_block',
'#content' => $content,
'#attached' => [
'library' => [
'sharethis/sharethispickerexternalbuttonsws',
'sharethis/sharethispickerexternalbuttons',
'sharethis/sharethis',
],
'drupalSettings' => [
'sharethis' => $st_js,
],
],
'#weight' => $sharethis_settings
->get('weight'),
];
}
break;
case 'links':
$enabled_view_modes = $sharethis_settings
->get('sharethisnodes.' . $node
->bundle());
if (isset($enabled_view_modes[$view_mode]) && $enabled_view_modes[$view_mode]) {
$st_js = $sharethis_manager
->sharethisIncludeJs();
$content = $sharethis_manager
->renderSpans($data_options, $title, $path);
$links['sharethis'] = [
'title' => [
'#theme' => 'sharethis_block',
'#content' => $content,
'#attached' => [
'library' => [
'sharethis/sharethispickerexternalbuttonsws',
'sharethis/sharethispickerexternalbuttons',
'sharethis/sharethis',
],
'drupalSettings' => [
'sharethis' => $st_js,
],
],
],
'attributes' => [
'class' => 'sharethis-buttons',
],
];
$build['links'] = [
'#theme' => 'links',
'#links' => $links,
'#attributes' => [
'class' => [
'links',
'inline',
],
],
// Wrap it in a div.
'#tag' => 'div',
'#type' => 'html_tag',
'#weight' => $sharethis_settings
->get('weight'),
];
}
break;
}
}