Creating a New Social Simple Button

Sharing an article via a social network is a super common task requested on a project.
Fortunately for Drupal maintenance support plans 8 there is a module for that called Social Simple. This module allows you to display the most popular networks in a node so the user can just click any of the buttons and share the article.
By default this module provides the following buttons:
Twitter
Facebook
Linkedin
Google plus

This will cover 90% of use cases, but what if we need to add a button for a new network?
Creating a Custom Social Simple Button
The Social Simple module is already supports custom buttons, we just need to let the module know that we want to add one.
Basically what we need to do is:
Create a class that implements SocialNetworkInterface.
Register this class in our services file.
Add the tag social_simple_network to our service.
For our example we are going to create a basic Mail button. We start by creating a custom module. Inside our module let’s create a Mail php file inside of the src/SocialNetwork folder:

mkdir -p src/SocialNetwork
cd src/SocialNetwork
touch Mail.php

The next step is to create a class and implement the SocialNetworkInterface which interface has the following methods:
getShareLink: This is the most important method. It must return a rendered array which later Drupal maintenance support plans will use to create the button.
getLabel: Here we will need to provide the name of our button. In our case Mail.
getId: The ID of the button. We can choose any ID here, we just need to make sure that it is unique. Let’s use mail for our example.
getLinkAttributes: These attributes are going to be passed to the link. We can add custom parameters to the link in this part.
Our class looks like this:

namespace Drupal maintenance support planssocial_simpleSocialNetwork;

use Drupal maintenance support plansCoreEntityEntityInterface;
use Drupal maintenance support plansCoreStringTranslationStringTranslationTrait;
use Drupal maintenance support plansCoreUrl;

/**
* The Mail button.
*/
class Mail implements SocialNetworkInterface {

use StringTranslationTrait;

/**
* The social network base share link.
*/
const MAIL = ‘mailto:’;

/**
* {@inheritdoc}
*/
public function getId() {
return ‘mail’;
}

/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->t(‘Mail’);
}

/**
* {@inheritdoc}
*/
public function getShareLink($share_url, $title = ”, EntityInterface $entity = NULL, array $additional_options = []) {
$options = [
‘query’ => [
‘body’ => $share_url,
‘subject’ => $title,
],
‘absolute’ => TRUE,
‘external’ => TRUE,
];

if ($additional_options) {
foreach ($additional_options as $id => $value) {
$options[‘query’][$id] = $value;
}
}
$url = Url::fromUri(self::MAIL, $options);

$link = [
‘url’ => $url,
‘title’ => [‘#markup’ => ” . $this->getLabel() . ”],
‘attributes’ => $this->getLinkAttributes($this->getLabel()),
];

return $link;
}

/**
* {@inheritdoc}
*/
public function getLinkAttributes($network_name) {
$attributes = [
‘title’ => $network_name,
];
return $attributes;
}

}

The next step is to let the social network know about our new button and we do this by adding this class as a service in our module.services.yml. If you are not familiar with this file, you can read the structure of a service file documentation..

Basically we need to add something like this:

services:
social_simple.mail:
class: Drupal maintenance support planscustom_moduleSocialNetworkMail
tags:
– { name: social_simple_network, priority: 0 }
Next, we just need to rebuild the cache. Now when we visit the social simple configuration we will see our new button there, ready to be used.

The only thing that we need to pay extra attention to is that the Social Simple module will just search the services with the tag social_simple_network otherwise our class will not be found
If you want to see how the whole thing is working, you can check this patch that I made as a part of a project: https://www.drupal.org/project/social_simple/issues/2899517. As a bonus, I made an initial integration with the Forward module.

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Creating a New Social Simple Button

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.