You are here

function social_simple_per_node_form_node_form_alter in Social simple 8

Same name and namespace in other branches
  1. 2.0.x modules/social_simple_per_node/social_simple_per_node.module \social_simple_per_node_form_node_form_alter()

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.

File

modules/social_simple_per_node/social_simple_per_node.module, line 55
Contains social.module..

Code

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,
    ];
  }
}