You are here

social_simple_per_node.module in Social simple 8

Same filename and directory in other branches
  1. 2.0.x modules/social_simple_per_node/social_simple_per_node.module

Contains social.module..

File

modules/social_simple_per_node/social_simple_per_node.module
View source
<?php

/**
 * @file
 * Contains social.module..
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Implements hook_help().
 */
function social_simple_per_node_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the social module.
    case 'help.page.social_simple_per_node':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Provide the ability to enable/disable simple social share links par node.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_entity_base_field_info().
 */
function social_simple_per_node_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type
    ->id() === 'node') {
    $fields['social_share'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Social share links'))
      ->setName('social_share')
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDescription(t('Stores whether the social share links are displayed or not.'))
      ->setDefaultValue(TRUE);
  }
  return $fields;
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 *
 * This module adds a simple checkbox to the node form labeled social_share.
 * If the checkox is unchecked then the social share inks are not displayed
 * for this node.
 */
function social_simple_per_node_form_node_form_alter(&$form, FormStateInterface &$form_state) {

  /** @var \Drupal\Node\NodeInterface $node */
  $node = $form_state
    ->getFormObject()
    ->getEntity();

  /** @var \Drupal\node\NodeTypeInterface $node_type */
  $node_type = $node->type->entity;
  $share = $node_type
    ->getThirdPartySetting('social_simple', 'share', 0);
  if ($share) {

    // Default value for displaying share social links.
    $shared = $node_type
      ->getThirdPartySetting('social_simple', 'share', 0);

    // If a value has been set on the node, get it as the default value.
    if (isset($node->social_share)) {
      if ($node->social_share->value !== NULL) {
        $shared = $node->social_share->value;
      }
    }
    if (\Drupal::currentUser()
      ->hasPermission('disable social links per node') || \Drupal::currentUser()
      ->hasPermission('administer nodes')) {
      $form['social_simple'] = [
        '#type' => 'details',
        '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
        '#title' => t('Social share'),
        '#description' => t('Display social share links.'),
        '#weight' => 7,
        '#access' => TRUE,
      ];
      $form['social_simple']['social_share'] = [
        '#type' => 'checkbox',
        '#title' => t('Social share links enabled'),
        '#attributes' => [
          'title' => t('When unchecked, social share links are not anymore displayed.'),
        ],
        '#group' => 'social_simple',
        '#default_value' => $shared,
      ];
    }
    else {
      $form['social_share'] = [
        '#type' => 'value',
        '#value' => $shared,
      ];
    }
  }
  else {
    $form['social_share'] = [
      '#type' => 'value',
      '#value' => NULL,
    ];
  }
}