You are here

public function SharethisConfigurationForm::buildForm in ShareThis 8.2

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/SharethisConfigurationForm.php, line 114

Class

SharethisConfigurationForm
Provides a settings for sharethis module.

Namespace

Drupal\sharethis\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  global $base_url;
  $my_path = drupal_get_path('module', 'sharethis');

  // First, setup variables we will need.
  // Get the path variables setup.
  // Load the css and js for our module's configuration.
  $config = $this
    ->config('sharethis.settings');
  $current_options_array = $this->sharethisManager
    ->getOptions();

  // Create the variables related to button choice.
  $button_choice = $current_options_array['buttons'];

  // Create the variables related to services chosen.
  $service_string = $current_options_array['services'];
  $service_string_markup = [];
  foreach (explode(",", $service_string) as $string) {
    $key = explode(":", mb_substr($string, 0, -1));
    $key = $key[1];
    $service_string_markup[] = $key;
  }

  // Create the variables for publisher keys.
  $publisher = $current_options_array['publisherID'];

  // Create the variables for teasers.
  $form = [];
  $form['options'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Display'),
  ];
  $form['options']['button_option'] = [
    '#required' => TRUE,
    '#type' => 'radios',
    '#options' => [
      'stbc_large' => $this
        ->t('Large Chicklets'),
      'stbc_' => $this
        ->t('Small Chicklets'),
      'stbc_button' => $this
        ->t('Classic Buttons'),
      'stbc_vcount' => $this
        ->t('Vertical Counters'),
      'stbc_hcount' => $this
        ->t('Horizontal Counters'),
      'stbc_custom' => $this
        ->t('Custom Buttons via CSS'),
    ],
    '#default_value' => $button_choice,
    '#title' => $this
      ->t('Choose a button style:'),
    '#prefix' => '<div class="st_widgetContain"><div class="st_spriteCover"><img id="stb_sprite" class="st_buttonSelectSprite ' . $button_choice . '" src="' . $base_url . '/' . $my_path . '/img/preview_sprite.png" /></div><div class="st_widgetPic"><img class="st_buttonSelectImage" src="' . $base_url . '/' . $my_path . '/img/preview_bg.png" /></div>',
    '#suffix' => '</div>',
  ];
  $form['options']['service_option'] = [
    '#description' => $this
      ->t('<b>Add</b> a service by selecting it on the right and clicking the <i>left arrow</i>.  <b>Remove</b> it by clicking the <i>right arrow</i>.<br /><b>Change the order</b> of services under "Selected Services" by using the <i>up</i> and <i>down</i> arrows.'),
    '#required' => TRUE,
    '#type' => 'textfield',
    '#prefix' => '<div>',
    '#suffix' => '</div><div id="myPicker"></div>',
    '#title' => $this
      ->t('Choose Your Services.'),
    '#default_value' => $service_string,
    '#maxlength' => 1024,
  ];
  $form['options']['option_extras'] = [
    '#title' => $this
      ->t('Extra services'),
    '#description' => $this
      ->t('Select additional services which will be available. These are not officially supported by ShareThis, but are available.'),
    '#type' => 'checkboxes',
    '#options' => [
      'Google Plus One:plusone' => $this
        ->t('Google Plus One'),
      'Facebook Like:fblike' => $this
        ->t('Facebook Like'),
    ],
    '#default_value' => $config
      ->get('option_extras'),
  ];
  $form['options']['callesi'] = [
    '#type' => 'hidden',
    '#default_value' => $current_options_array['callesi'],
  ];
  $form['additional_settings'] = [
    '#type' => 'vertical_tabs',
  ];
  $form['context'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Context'),
    '#group' => 'additional_settings',
    '#description' => $this
      ->t('Configure where the ShareThis widget should appear.'),
  ];
  $form['context']['location'] = [
    '#title' => $this
      ->t('Location'),
    '#type' => 'radios',
    '#options' => [
      'content' => $this
        ->t('Node content'),
      'block' => $this
        ->t('Block'),
      'links' => $this
        ->t('Links area'),
    ],
    '#default_value' => $config
      ->get('location'),
  ];

  // Add an information section for each location type, each dependent on the
  // currently selected location.
  foreach ([
    'links',
    'content',
    'block',
  ] as $location_type) {
    $form['context'][$location_type]['#type'] = 'container';
    $form['context'][$location_type]['#states']['visible'][':input[name="location"]'] = [
      'value' => $location_type,
    ];
  }

  // Add help text for the 'content' location.
  $form['context']['content']['help'] = [
    '#markup' => $this
      ->t('When using the Content location, you must place the ShareThis links in the <a href="@url">Manage Display</a> section of each content type.', [
      '@url' => Url::fromRoute('entity.node_type.collection')
        ->toString(),
    ]),
    '#weight' => 10,
    '#prefix' => '<em>',
    '#suffix' => '</em>',
  ];

  // Add help text for the 'block' location.
  $form['context']['block']['#children'] = 'You must choose which region to display the in from the Blocks administration';
  $entity_bundles = $this->entityTypeBundleInfo
    ->getBundleInfo('node');

  // Add checkboxes for each view mode of each bundle.
  $entity_modes = $this->entityDisplayRepository
    ->getViewModes('node');
  $modes = [];
  foreach ($entity_modes as $mode => $mode_info) {
    $modes[$mode] = $mode_info['label'];
  }

  // Get a list of content types and view modes.
  foreach ($entity_bundles as $bundle => $bundle_info) {
    $form['context']['links'][$bundle . '_options'] = [
      '#title' => $this
        ->t('%label View Modes', [
        '%label' => $bundle_info['label'],
      ]),
      '#description' => $this
        ->t('Select which view modes the ShareThis widget should appear on for %label nodes.', [
        '%label' => $bundle_info['label'],
      ]),
      '#type' => 'checkboxes',
      '#options' => $modes,
      '#default_value' => $config
        ->get('sharethisnodes.' . $bundle) ?: [],
    ];
  }

  // Allow the user to choose which content types will have ShareThis added
  // when using the 'Content' location.
  $content_types = [];
  $enabled_content_types = $current_options_array['node_types'];
  foreach ($entity_bundles as $bundle => $bundle_info) {
    $content_types[$bundle] = $this
      ->t('@label', [
      '@label' => $bundle_info['label'],
    ]);
  }
  $form['context']['content']['node_types'] = [
    '#title' => $this
      ->t('Node Types'),
    '#description' => $this
      ->t('Select which node types the ShareThis widget should appear on.'),
    '#type' => 'checkboxes',
    '#options' => $content_types,
    '#default_value' => $enabled_content_types,
  ];
  $form['context']['comments'] = [
    '#title' => $this
      ->t('Comments'),
    '#type' => 'checkbox',
    '#default_value' => $config
      ->get('comments'),
    '#description' => $this
      ->t('Display ShareThis on comments.'),
    '#access' => $this->moduleHandler
      ->moduleExists('comment'),
  ];
  $sharethis_weight_list = [
    -100,
    -50,
    -25,
    -10,
    0,
    10,
    25,
    50,
    100,
  ];
  $form['context']['weight'] = [
    '#title' => $this
      ->t('Weight'),
    '#description' => $this
      ->t('The weight of the widget determines the location on the page where it will appear.'),
    '#required' => FALSE,
    '#type' => 'select',
    '#options' => array_combine($sharethis_weight_list, $sharethis_weight_list),
    '#default_value' => $config
      ->get('weight'),
  ];
  $form['advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced'),
    '#group' => 'additional_settings',
    '#description' => $this
      ->t('The advanced settings can usually be ignored if you have no need for them.'),
  ];
  $form['advanced']['publisherID'] = [
    '#title' => $this
      ->t('Insert a publisher key (optional).'),
    '#description' => $this
      ->t("When you install the module, we create a random publisher key.  You can register the key with ShareThis by contacting customer support.  Otherwise, you can go to <a href='http://www.sharethis.com/account'>ShareThis</a> and create an account.<br />Your official publisher key can be found under 'My Account'.<br />It allows you to get detailed analytics about sharing done on your site."),
    '#type' => 'textfield',
    '#default_value' => $publisher,
  ];
  $form['advanced']['late_load'] = [
    '#title' => $this
      ->t('Late Load'),
    '#description' => $this
      ->t("You can change the order in which ShareThis widget loads on the user's browser. By default the ShareThis widget loader loads as soon as the browser encounters the JavaScript tag; typically in the tag of your page. ShareThis assets are generally loaded from a CDN closest to the user. However, if you wish to change the default setting so that the widget loads after your web-page has completed loading then you simply tick this option."),
    '#type' => 'checkbox',
    '#default_value' => $config
      ->get('late_load'),
  ];
  $form['advanced']['twitter_suffix'] = [
    '#title' => $this
      ->t('Twitter Suffix'),
    '#description' => $this
      ->t("Optionally append a Twitter handle, or text, so that you get pinged when someone shares an article. Example: <em>via @YourNameHere</em>"),
    '#type' => 'textfield',
    '#default_value' => $config
      ->get('twitter_suffix'),
  ];
  $form['advanced']['twitter_handle'] = [
    '#title' => $this
      ->t('Twitter Handle'),
    '#description' => $this
      ->t('Twitter handle to use when sharing.'),
    '#type' => 'textfield',
    '#default_value' => $config
      ->get('twitter_handle'),
  ];
  $form['advanced']['twitter_recommends'] = [
    '#title' => $this
      ->t('Twitter recommends'),
    '#description' => $this
      ->t('Specify a twitter handle to be recommended to the user.'),
    '#type' => 'textfield',
    '#default_value' => $config
      ->get('twitter_recommends'),
  ];
  $form['advanced']['option_onhover'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display ShareThis widget on hover'),
    '#description' => $this
      ->t('If disabled, the ShareThis widget will be displayed on click instead of hover.'),
    '#default_value' => $config
      ->get('option_onhover'),
  ];
  $form['advanced']['option_neworzero'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display count "0" instead of "New"'),
    '#description' => $this
      ->t('Display a zero (0) instead of "New" in the count for content not yet shared.'),
    '#default_value' => $config
      ->get('option_neworzero'),
  ];
  $form['advanced']['option_shorten'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display short URL'),
    '#description' => $this
      ->t('Display either the full or the shortened URL.'),
    '#default_value' => $config
      ->get('option_shorten'),
  ];
  $form['advanced']['cns'] = [
    '#title' => $this
      ->t('<b>CopyNShare </b><sup>(<a href="http://support.sharethis.com/customer/portal/articles/517332-share-widget-faqs#copynshare" target="_blank">?</a>)</sup>'),
    '#type' => 'checkboxes',
    '#prefix' => '<div id="st_cns_settings">',
    '#suffix' => '</div><div class="st_cns_container">
				<p>CopyNShare is the new ShareThis widget feature that enables you to track the shares that occur when a user copies and pastes your website\'s <u>URL</u> or <u>Content</u>. <br/>
				<u>Site URL</u> - ShareThis adds a special #hashtag at the end of your address bar URL to keep track of where your content is being shared on the web.<br/>
				<u>Site Content</u> - It enables the pasting of "See more: YourURL#SThashtag" after user copies-and-pastes text. When a user copies text within your site, a "See more: yourURL.com#SThashtag" will appear after the pasted text. <br/>
				Please refer the <a href="http://support.sharethis.com/customer/portal/articles/517332-share-widget-faqs#copynshare" target="_blank">CopyNShare FAQ</a> for more details.</p>
			</div>',
    '#options' => [
      'donotcopy' => $this
        ->t("Measure copy & shares of your site's Content"),
      'hashaddress' => $this
        ->t("Measure copy & shares of your site's URLs"),
    ],
    '#default_value' => $config
      ->get('cns'),
  ];
  $form['#attached']['drupalSettings']['sharethis']['service_string_markup'] = $service_string_markup;
  $form['#attached']['library'][] = 'sharethis/drupal.sharethisform';
  $form['#attached']['library'][] = 'sharethis/drupal.sharethispicker';
  $form['#attached']['library'][] = 'sharethis/drupal.sharethispickerexternal';
  return parent::buildForm($form, $form_state);
}