You are here

social_simple.module in Social simple 8

Same filename and directory in other branches
  1. 2.0.x social_simple.module

Contains social.module..

File

social_simple.module
View source
<?php

/**
 * @file
 * Contains social.module..
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\NodeType;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeTypeInterface;

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

    // Main module help for the social module.
    case 'help.page.social':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Provide simple social share buttons for Twitter, Facebook and Google+') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_entity_extra_field_info().
 */
function social_simple_entity_extra_field_info() {
  $extra = [];

  /** @var \Drupal\node\Entity\NodeType $bundle */
  foreach (NodeType::loadMultiple() as $bundle) {
    if ($bundle
      ->getThirdPartySetting('social_simple', 'share', 0)) {
      $extra['node'][$bundle
        ->Id()]['display']['social_simple_buttons'] = [
        'label' => t('Social simple buttons'),
        'description' => t('Simple Social share buttons'),
        'weight' => 100,
        'visible' => FALSE,
      ];
    }
  }
  return $extra;
}

/**
 * Implements hook_theme().
 */
function social_simple_theme($existing, $type, $theme, $path) {
  return [
    'social_simple_buttons' => [
      'variables' => [
        'links' => [],
        'attributes' => [
          'class' => [
            'links',
          ],
        ],
        'heading' => [],
        'set_active_class' => FALSE,
      ],
    ],
  ];
}

/**
 * Implements hook_preprocess_HOOK().
 */
function social_simple_preprocess_social_simple_buttons(&$variables) {

  // Preprocess the buttons as regular links.
  template_preprocess_links($variables);
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function social_simple_theme_suggestions_social_simple_buttons_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::routeMatch()
    ->getParameter('node')) {
    $suggestions[] = 'social_simple_buttons__' . $node
      ->getType();
  }
}

/**
 * Implements hook_entity_view().
 */
function social_simple_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($display
    ->getComponent('social_simple_buttons')) {

    /** @var \Drupal\node\NodeInterface $entity */

    // Support social simple per node feature.
    if ($entity
      ->hasField('social_share')) {
      if ($entity->social_share->value !== NULL && empty($entity->social_share->value)) {
        return;
      }
    }

    /** @var \Drupal\social_simple\SocialSimpleGenerator $social_simple_generator */
    $social_simple_generator = \Drupal::service('social_simple.generator');

    /** @var \Drupal\node\NodeTypeInterface $node_type */
    $node_type = $entity->type->entity;
    $share = $node_type
      ->getThirdPartySetting('social_simple', 'share', 0);
    $networks = $node_type
      ->getThirdPartySetting('social_simple', 'networks', []);
    if ($share && !empty($networks)) {
      $title = $node_type
        ->getThirdPartySetting('social_simple', 'title', '');
      $build['social_simple_buttons'] = $social_simple_generator
        ->buildSocialLinks($networks, $title, $entity);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function social_simple_form_node_type_form_alter(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\node\NodeTypeInterface $type */
  $type = $form_state
    ->getFormObject()
    ->getEntity();
  $form['social_simple'] = [
    '#type' => 'details',
    '#group' => 'additional_settings',
    '#title' => t('Social simple share'),
    '#access' => \Drupal::currentUser()
      ->hasPermission('administer social simple') || \Drupal::currentUser()
      ->hasPermission('administer nodes'),
  ];
  $form['social_simple']['social_simple_share'] = [
    '#type' => 'checkbox',
    '#title' => t('Display social network share links'),
    '#default_value' => $type
      ->getThirdPartySetting('social_simple', 'share', 0),
  ];
  $form['social_simple']['social_simple_title'] = [
    '#type' => 'textfield',
    '#title' => t('The title used above the share links.'),
    '#default_value' => $type
      ->getThirdPartySetting('social_simple', 'title', ''),
    '#states' => [
      'visible' => [
        ':input[name="social_simple_share"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  /** @var \Drupal\social_simple\SocialSimpleGenerator $social_simple_generator */
  $social_simple_generator = \Drupal::service('social_simple.generator');
  $form['social_simple']['social_simple_networks'] = [
    '#type' => 'checkboxes',
    '#title' => t('Select the social networks availables for this content type'),
    '#options' => $social_simple_generator
      ->getNetworks(),
    '#default_value' => $type
      ->getThirdPartySetting('social_simple', 'networks', []),
    '#states' => [
      'visible' => [
        ':input[name="social_simple_share"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $options = [];
  $valid_field_type = [
    'entity_reference',
  ];
  $fields = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions('node', $type
    ->id());

  /** @var \Drupal\Core\Field\BaseFieldDefinition $field */
  foreach ($fields as $name => $field) {
    if (!empty($field
      ->getTargetBundle()) && in_array($field
      ->getType(), $valid_field_type)) {
      $options[$name] = $field
        ->getLabel();
    }
  }
  $form['social_simple']['social_simple_hashtags'] = [
    '#type' => 'select',
    '#title' => t('Twitter hashtags'),
    '#options' => [
      '0' => t('- None -'),
    ] + $options,
    '#default_value' => $type
      ->getThirdPartySetting('social_simple', 'hashtags', []),
    '#states' => [
      'visible' => [
        ':input[name="social_simple_networks[twitter]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('forward')) {
    $form['social_simple']['social_simple_forward_integration'] = [
      '#type' => 'checkbox',
      '#title' => t('Integrate the <a href="https://www.drupal.org/project/forward">Forward module</a> with the Mail button.'),
      '#default_value' => $type
        ->getThirdPartySetting('social_simple', 'forward_integration', FALSE),
    ];
  }
  $form['#entity_builders'][] = 'social_simple_form_node_type_form_builder';
}

/**
 * Entity builder for the node type form with social simple share option.
 *
 * @see private_content_form_node_type_form_alter()
 */
function social_simple_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
  $type
    ->setThirdPartySetting('social_simple', 'share', $form_state
    ->getValue('social_simple_share'));
  $type
    ->setThirdPartySetting('social_simple', 'networks', array_filter($form_state
    ->getValue('social_simple_networks')));
  $type
    ->setThirdPartySetting('social_simple', 'title', $form_state
    ->getValue('social_simple_title'));
  $type
    ->setThirdPartySetting('social_simple', 'hashtags', $form_state
    ->getValue('social_simple_hashtags'));
  $type
    ->setThirdPartySetting('social_simple', 'forward_integration', $form_state
    ->getValue('social_simple_forward_integration'));
}