public function SocialSharingBlock::build in Social media share 8
Builds and returns the renderable array for this block plugin.
If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).
Return value
array A renderable array representing the content of the block.
Overrides BlockPluginInterface::build
See also
\Drupal\block\BlockViewBuilder
File
- src/
Plugin/ Block/ SocialSharingBlock.php, line 77
Class
- SocialSharingBlock
- Provides a 'SocialSharingBlock' block.
Namespace
Drupal\social_media\Plugin\BlockCode
public function build() {
global $base_url;
$library = [
'social_media/basic',
];
$settings = [];
$icon_path = $base_url . '/' . drupal_get_path('module', 'social_media') . '/icons/';
$elements = [];
$social_medias = $this->configFactory
->get('social_media.settings')
->get('social_media');
// Call pre_execute event before doing anything.
$event = new SocialMediaEvent($social_medias);
$this->eventDispatcher
->dispatch('social_media.pre_execute', $event);
$social_medias = $event
->getElement();
$social_medias = $this
->sortSocialMedias($social_medias);
foreach ($social_medias as $name => $social_media) {
// Replace api url with different link.
if ($name == "email" && isset($social_media['enable_forward']) && $social_media['enable_forward']) {
$social_media['api_url'] = str_replace('mailto:', '/social-media-forward', $social_media['api_url']);
$social_media['api_url'] .= '&destination=' . $this->currentPath
->getPath();
if (isset($social_media['show_forward']) && $social_media['show_forward'] == 1) {
$library[] = 'core/drupal.dialog.ajax';
}
}
if ($social_media['enable'] == 1 && !empty($social_media['api_url'])) {
$elements[$name]['text'] = $social_media['text'];
$elements[$name]['api'] = new Attribute([
$social_media['api_event'] => $this->token
->replace($social_media['api_url']),
]);
if (isset($social_media['library']) && !empty($social_media['library'])) {
$library[] = $social_media['library'];
}
if (isset($social_media['attributes']) && !empty($social_media['attributes'])) {
$elements[$name]['attr'] = $this
->socialMediaConvertAttributes($social_media['attributes']);
}
if (isset($social_media['drupalSettings']) && !empty($social_media['drupalSettings'])) {
$settings['social_media'] = $this
->socialMediaConvertDrupalSettings($social_media['drupalSettings']);
}
if (isset($social_media['default_img']) && $social_media['default_img']) {
$elements[$name]['img'] = $icon_path . $name . '.svg';
}
elseif (!empty($social_media['img'])) {
$elements[$name]['img'] = $base_url . '/' . $social_media['img'];
}
if (isset($social_media['enable_forward']) && $social_media['enable_forward']) {
if (isset($social_media['show_forward']) && $social_media['show_forward'] == 1) {
$elements[$name]['forward_dialog'] = $social_media['show_forward'];
}
}
}
}
$build = [];
// Call prerender event before render.
$event = new SocialMediaEvent($elements);
$this->eventDispatcher
->dispatch('social_media.pre_render', $event);
$elements = $event
->getElement();
$build['social_sharing_block'] = [
'#theme' => 'social_media_links',
'#elements' => $elements,
'#attached' => [
'library' => $library,
'drupalSettings' => $settings,
],
];
return $build;
}