The must-have Drupal module Social Sharing

The must-have Drupal module Social Sharing

Introduction

Sending photos, videos, product recommendations, and website links to friends with social networking accounts are all firmly embedded in our lives. It is clear that progress cannot be stopped and social networks have become routine for us. Search systems pay more and more attention to them.

In our practice, it often happens that for a commercial project we create a custom module that becomes successful and popular. This was the case with Bibliography & Citation, and we hope it will be with Social Sharing - a new module from our back-end developer of the ADCI Solutions web studio.

Here’s what he says about his module: “Most of the modules for social sharing of web pages on drupal.org - are Drupal blocks that have these social media by default. You can not easily add any social service that you want to your site.

I developed a fundamentally different module for these purposes on one of the projects. Its name is Social Sharing - that's the best way to call it”. On behalf of our developer, we will tell the story of how the module was created.

Historical data

Тhere is a moment that has become critical for our client in the work of the well-known service for sharing web pages on the social network AddToAny.

The usual Twitter share URL looks like that:

https://twitter.com/intent/tweet?text=Hello%20world

And here is the sharing of the same page, but via AddToAny:

https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.addtoany.com%2Fbuttons%2Fcustomize%2Ftwitter_message&linkname=Twitter%20Message%20-%20AddToAny&template=Reading%3A%20%24%7Btitle%7D%20%24%7Blink%7D%20by%20%40AddToAny&linknote=

At first, we see that the request is going through AddToAny, then it is processed and only then shared on Twitter. The shared content is meant to be public by default, but the client didn't want the traffic to go elsewhere.

We needed something flexible and custom. I made an MVP of such a module for a client and rolled out a modified version of Social Sharing on drupal.org. About 20% of the module that I used on the project remained in Social Sharing - only one basic idea. What's changed is:

  • A custom plugin type was redesigned. This plugin is responsible for social media;
  • The custom field type that was used on the project, and its components (widget and formatter) were redesigned. Now the share URL is dynamic and can be changed for each node;
  • A new block type with social media services was created;
  • I added an extra field with its settings (for which entities will be used and which services are available);
  • The views-field was created;
  • The most relevant feature was developed - supporting of the tokens for sharing URL that allows content to be shared from any field of the node;
  • 25 new services were created for sharing: Blogger, Diaspora, Douban, Evernote, Flipboard, Getpocket, Gmail, Google Bookmarks, Hacker News, InstaPaper, LineMe, LiveJournal, OK.RU, Pinterest, QZone, Reddit, RenRen, Skype, Telegram, Tumblr, Vk, Weibo, WhatsApp, Xing, Yahoo. These services are available out of the box after installing the module.

Some details for you are below.

Why you should choose the Social Sharing module

  1. It's flexible. The module is based on the Drupal plugin system, on which the eighth version of the CMS was built. It means that any developer will be able to add their social network for sharing if it is not used in the current version.
  2. It provides stable functionality. Other modules don't do that - I investigated the ones I found. There was the only one that was made in the same way with the plugins, but it is not practical, and I could not get it to work well. The idea was similar but not completed.
  3. It has a dynamic sharing URL and token support. Many modules allow using sharing not as a block on the page but as a field in the content. You need to add this field to any type of content to do this. Our module can do the same, but with many more opportunities. You can set the very same information that the user wants to share for each page individually: not only the title of the page with the URL but any content from this page.

The setup and operation of the Social Sharing module

Now let's talk more about the configuration of various features.

Extrafield configuration

If you need the same sharing links with the same content for different entities on the site for sharing, then you can use Extrafield. There is a separate configuration page for it, where the user can configure:

  • types of entities for which this field will be available;
  • social networks that will be available;
  • content to be shared.

The disadvantage of this method is that these settings are global for all entities, and if you change them for one type of entity (for example, the list of social networks), they will change wherever they would be used. This field is convenient to use if flexibility is not required, and the list of links will remain unchanged throughout the site.

Entity field configuration

Below, you can see examples of the new field type configuration. In contrast to extrafield this one can be configured for each entity separately. In addition, you may set general default settings and, if necessary, override these settings for each unit of this entity.

Let's discuss the example. We have an entity type Node. This entity can have different bundles (Article, Basic page, Blogpost, etc.). For each bundle, we can add a field such as Social sharing and set custom defaults for each of the bundles. Next, we will create content on our site - add nodes for these bundles. Тhe user can choose: to leave the default sharing settings or redefine them to the current node (see the screenshot below).

The key feature of this method is the ability to use tokens. With the support of tokens, it became possible to share any content that may exist in the node.

Block Configuration

The block is still pretty simple. The user can add a block to any region on the site, select services for sharing, and set the URL for sharing, or leave it all by default.

Displaying the links (Block and entity field)

The picture below is an example of displaying links to share in a block and as a field for the current node. "Why are there no icons?" - you might ask. In my experience, I haven't ever seen any project where standard icons from some service would be used (for example, Fontawesome). If you have a design, then it always has these icons, which are added by the front-end developer during theming. In the short term, we are planning to add various presets of icons.

Adding a social network that the module does not have

If the module does not contain the social network you need, then adding it will be not difficult if you have enough programming skills. You need to create your own custom module and add a new plugin in this module to do this. The plugin will be automatically integrated into the functionality provided by the module, and you can use your social network along with the others. You can read how plugins work in Drupal here.

Example path-to-custom-modules/mymodule/src/Plugin/ShareService/MySocialService.php file.

Conclusion

Some well-known modules have only a block, others have only a field, somewhere they have the field and block, but you cannot change or add services. Seldom, the service is integrated with the Views module and has no token support, which limits the choice of content for share. The Social Sharing module collects the disparate functionality of most modules and improves it significantly in key features, making it a leader amongst all others.

We will be glad if you integrate the module into your projects and leave comments and suggestions for its work.

You might also like

Drupal for e-Learning websites

What a Drupal website for distance learning should be like? We will talk about technologies for students’ assignments, collecting statistics on the work done, functionality, Drupal modules, and distributions for its work.