View source
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
function fblikebutton_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.fblikebutton':
$output = '<p>Configure the dynamic Like button. This Like button will like the URL you\'re visiting. ';
$output .= 'You can set the content types on which the button displays, choose to display it in the ';
$output .= 'content block or in the links area.</p>';
$output .= '<p style="font-weight:bold;">You can also configure Static Like Button Blocks in the @link.</p>';
return t($output, array(
'@link' => Link::fromTextAndUrl('Block Layout page', Url::fromRoute('block.admin_display'))
->toString(),
));
}
}
function fblikebutton_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
$user = $account = \Drupal::currentUser();
$config = \Drupal::config('fblikebutton.settings');
$types = $config
->get('node_types');
$full_node_display = $config
->get('full_node_display');
$teaser_display = $config
->get('teaser_display');
$full = $view_mode == 'full' ? TRUE : FALSE;
$show = $types[$node
->getType()] && $user
->hasPermission('access fblikebutton');
$likebutton_weight = $config
->get('weight');
if ($show) {
if ($view_mode == 'teaser' && $teaser_display == 1 || $view_mode == 'full' && $full_node_display == 0) {
$url = fblikebutton_get_node_url($node
->id());
$build['fblikebutton_field'] = array(
'#theme' => 'fblikebutton',
'#weight' => $likebutton_weight,
'#url' => $url,
);
}
}
}
function hook_node_links_alter(array &$node_links, NodeInterface $node, array &$context) {
$view_mode = $context['view_mode'];
if ($view_mode == 'teaser' && $teaser_display == 2 || $view_mode == 'full' && $full_node_display == 1) {
$url = fblikebutton_get_node_url($node
->id());
}
$node_links['fblikebutton_field'] = array(
'#theme' => 'fblikebutton',
'#url' => $url,
);
}
function fblikebutton_get_node_url($node_id) {
$url_generator = \Drupal::urlGenerator();
return $url_generator
->generateFromRoute('entity.node.canonical', array(
'node' => $node_id,
), array(
'absolute' => TRUE,
));
}
function fblikebutton_conf() {
$url_generator = \Drupal::urlGenerator();
$webpage_to_like = $url_generator
->generateFromRoute('<current>', array(), array(
'absolute' => TRUE,
));
$config = \Drupal::config('fblikebutton.settings');
$conf = array(
'layout' => $config
->get('layout'),
'action' => $config
->get('action'),
'color_scheme' => $config
->get('color_scheme'),
'show_faces' => $config
->get('show_faces'),
'font' => $config
->get('font'),
'language' => $config
->get('language'),
'url' => $webpage_to_like,
);
return $conf;
}
function fblikebutton_theme() {
return array(
'fblikebutton' => array(
'variables' => fblikebutton_conf(),
'template' => 'fblikebutton',
'file' => 'fblikebutton.theme',
),
);
}